Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to close all tabs in JTabbedPane?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to close all tabs in JTabbedPane?

    Hi, I'm making a small chat program. I'm having problem with JTabbedPane, I want to remove all tabs in the JTabbedPane when the chat window closed(setvisible=false).
    Then if the user click another user name it'll launch again the chat window(setvisible=true).

    I'm having problem with the code so I use the setvisible method instead of making new chat window JFrame.

    The problem is when I can't remove all the tabs.
    this.removeAll()

    I got this error

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    	at java.util.Vector.elementAt(Vector.java:430)
    	at javax.swing.JTabbedPane.getTitleAt(JTabbedPane.java:1091)
    	at epistle.MessageBox$2.stateChanged(MessageBox.java:79)
    	at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:400)
    	at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:253)
    	at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:116)
    	at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:50)
    	at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:599)
    	at javax.swing.JTabbedPane.removeTabAt(JTabbedPane.java:958)
    	at javax.swing.JTabbedPane.remove(JTabbedPane.java:1030)
    	at epistle.TabChat.clearTab(Tampilan.java:878)
    	at epistle.MessageBox.closeChat(MessageBox.java:150)
    	at epistle.MessageBox$1.windowClosing(MessageBox.java:52)
    	at java.awt.AWTEventMulticaster.windowClosing(AWTEventMulticaster.java:332)
    	at java.awt.Window.processWindowEvent(Window.java:1862)
    	at javax.swing.JFrame.processWindowEvent(JFrame.java:279)
    	at java.awt.Window.processEvent(Window.java:1820)
    	at java.awt.Component.dispatchEventImpl(Component.java:4630)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Window.dispatchEventImpl(Window.java:2475)
    	at java.awt.Component.dispatchEvent(Component.java:4460)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    Is the JTabbedPane doesn't allow empty tab state? When I make the JTabbedPane first time, it doesn't have tab right?

    Hope someone can help me,
    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to close all tabs in JTabbedPane?

    Looking at the stack trace I don't see a call to the removeAll method. Are you sure that is where the error is happening?

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to close all tabs in JTabbedPane?

    Quote Originally Posted by Norm View Post
    Looking at the stack trace I don't see a call to the removeAll method. Are you sure that is where the error is happening?
    I put the remove function in this clearTab function
    	public void clearTab()
    	{
    		int i;
     
    		//for (i = 0; i < this.getTabCount(); i++)
    		//this.remove(i);	
     
    		this.removeAll();
    	}

    I tried to remove by iteration too but it's error like that if I remove all the tab..
    Last edited by LeonLanford; June 28th, 2010 at 10:04 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to close all tabs in JTabbedPane?

    I just wrote a small test program and used removeAll() with no problem.
    I must be something else causes the error.

    Can you make and post here a small program to demo the problem?

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to close all tabs in JTabbedPane?

    Quote Originally Posted by Norm View Post
    I just wrote a small test program and used removeAll() with no problem.
    I must be something else causes the error.

    Can you make and post here a small program to demo the problem?
    I don't use my customized tab, just use normal JTabbedPane

    public class Main
    {	
    	public Main(){
    	}
     
    	public static void main(String[] args)
    	{
    		final Tampilan tpl = new Tampilan();
     
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    tpl.showLogin();
                }
            });
     
    	}

    Tampilan is too long, in short it just make the buddy list with JTree in it, it then call a function to make the message box

    	public void createMessageBox()
    	{	
    		messageBox = new MessageBox();
    	}

    the messagebox class I created is like this, I removed the code for decorating the box

    public class MessageBox extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	JTabbedPane tabChat;
    	JFrame openerFrame;
     
    	public MessageBox()
    	{
    		super();
    		this.setSize(450, 350);
    		this.setResizable(true);
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
    		tabChat = new JTabbedPane();
    		tabChat.addChangeListener(new ChangeListener() { 
    			// This method is called whenever the selected tab changes
    			public void stateChanged(ChangeEvent evt) { 
    				JTabbedPane pane = (JTabbedPane)evt.getSource(); 
     
    				// Get current tab 
    				int sel = pane.getSelectedIndex(); 
    				setJudul(pane.getTitleAt(sel));
    		} });
    		this.add(tabChat);
    	}
     
    	public void setJudul(String judul)
    	{
    		this.setTitle(judul);
    	}
     
    	public void closeChat()
    	{
    		this.setVisible(false);
    		tabChat.removeAll();
    	}
     
    	public void openChat(String namaUserLawan)
    	{	
    		tabChat.add(namaUserLawan, new javax.swing.JLabel(""));
    		this.setVisible(true);
    	}
    }

    Btw because of testing the code I found out that removing the listener solves the problem
    tabChat.addChangeListener

    Is there something I can do to fix this?

    Thanks

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to close all tabs in JTabbedPane?

    I fixed my problem by just adding this

    				if (sel > 0)
    				setJudul(pane.getTitleAt(sel))


    Thanks a lot

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to close all tabs in JTabbedPane?

    Look at this part of the error message:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementAt(Vector.java:430)
    at javax.swing.JTabbedPane.getTitleAt(JTabbedPane.jav a:1091)
    at epistle.MessageBox$2.stateChanged(MessageBox.java: 79)
    It says that the listener at line 79 did a getTitleAt() with -1.
    Add code to test the value of the selected index.


    BTW 0 is a valid index.

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to close all tabs in JTabbedPane?

    Quote Originally Posted by Norm View Post
    Look at this part of the error message:

    It says that the listener at line 79 did a getTitleAt() with -1.
    Add code to test the value of the selected index.


    BTW 0 is a valid index.
    Never mind that.. It's already solved, I got problem with the listener(see the code above)
    Thanks

Similar Threads

  1. Do I need to close an audio clip?
    By m2oswald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2010, 01:20 AM
  2. Replies: 3
    Last Post: April 14th, 2010, 07:33 PM
  3. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM
  4. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 07:26 PM
  5. Java program to open and close computer CD/DVD drive
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 28th, 2009, 12:04 PM