Results 1 to 9 of 9
  1. #1
    LeonLanford is offline Member
    Join Date
    Oct 2009
    Posts
    29
    Rep Power
    0

    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.
    Java Code:
    this.removeAll()
    I got this error

    Java Code:
    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
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LeonLanford View Post
    Is the JTabbedPane doesn't allow empty tab state? When I make the JTabbedPane first time, it doesn't have tab right?
    I had something similar with the Java 1.4.2 version; I circumvented the problem by leaving at least one tab open (visible); my tabs are associated with text editors and my workaround was fine so I never looked at it again. We have to wait until Camickr reads this thread; he has the definitive answer, I'm sure about it; I even don't dare to report it as a bug because I'm not sure that what I did was correct or not.

    kind regards,

    Jos

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    The removeAll() method works fine for me using JDK6 on XP.

    If you need more help then post a SSCCE that demonstrates the problem.

  4. #4
    LeonLanford is offline Member
    Join Date
    Oct 2009
    Posts
    29
    Rep Power
    0

    Default

    My code is too long to be pasted here.. I also use 4 class to make single window(caller, jframe, jpanel, jtabbedpane).
    I have to post all the codes?

    I'm using jdk1.6.0_16 in win xp, my IDE is eclipse

    I also tried to remove the tab using iteration
    Java Code:
    	public void clearTab()
    	{
    		int i;
    		
    		for (i = 0; i < this.getTabCount(); i++)
    		this.remove(i);	
    		
    		//this.removeAll();
    	}
    It's just like josah say, I need to leave at least 1 tab to make it working.
    I tried to add temporary tab so I can remove it at the adding tab process, but it still fails..
    Last edited by LeonLanford; 06-28-2010 at 04:23 PM.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    I did not ask for all the code. I asked for the SSCCE. The idea is for you to create a simple program to prove that the method does (or doesn't) work. If it works then you compare you demo code with your real code to see what is different. We are not here to debug your code for you.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Removed, what camickr said.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    LeonLanford is offline Member
    Join Date
    Oct 2009
    Posts
    29
    Rep Power
    0

    Default

    I don't use my customized tab, just use normal JTabbedPane

    Java Code:
    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

    Java Code:
    	public void createMessageBox()
    	{	
    		messageBox = new MessageBox();
    	}
    the messagebox class I created is like this, I removed the code for decorating the box

    Java Code:
    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
    Java Code:
    tabChat.addChangeListener
    Is there something I can do to fix this?

    Thanks

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by camickr View Post
    The removeAll() method works fine for me using JDK6 on XP.

    If you need more help then post a SSCCE that demonstrates the problem.
    I got back to my old code and changed it: now all tabs can be removed and by golly it works. I don't remove them all at the same time, just each one, one by one individually. I could've sworn that it didn't work some time ago (that's why I left at least one tab in). After testing I changed it back again because in my case it doesn't really make sense to close/remove all tabs. For the record: I'm using Java 1.6 build 20.

    kind regards,

    Jos

  9. #9
    LeonLanford is offline Member
    Join Date
    Oct 2009
    Posts
    29
    Rep Power
    0

    Default

    I fixed my problem by just adding this

    Java Code:
    				if (sel > 0)
    				setJudul(pane.getTitleAt(sel))

    Thanks a lot :D

Similar Threads

  1. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  2. SWT tabs with Draw2d
    By garyscarter in forum SWT / JFace
    Replies: 0
    Last Post: 07-07-2009, 03:31 PM
  3. Arrangement of tabs in JTabbedPane
    By swapnilnawale in forum AWT / Swing
    Replies: 0
    Last Post: 03-20-2009, 07:27 PM
  4. Adding Tabs to JTabbedPane
    By blackstone in forum AWT / Swing
    Replies: 2
    Last Post: 11-14-2007, 03:15 PM
  5. resize tabs in jtabbedpane
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 03:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •