wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes JTabbedPane remove exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTabbedPane remove exception" Watch "JTabbedPane remove exception" New topic

JTabbedPane remove exception

Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 191

I am at my wit's end trying to figure out how to avoid the exception thrown below when in this case removing 1 tab in tabbedpane at index 0.
Here is my code. Code gets action map on JTabbedPane.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:371)
at java.util.ArrayList.get(ArrayList.java:384)
at javax.swing.JTabbedPane.getTitleAt(JTabbedPane.java:1112)
at com.program.program.ArchiveTabPane$4.stateChanged(Unknown Source)
at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416)
at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
at javax.swing.JTabbedPane.removeTabAt(JTabbedPane.java:979)
at de.javasoft.swing.JYTabbedPane.removeTabAt(JYTabbedPane.java:460)
at com.program.program.ArchiveTabPane$2$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Rob Spoor

Joined: Oct 27, 2005
Posts: 20194

c is -1 which means that getTabCount() returned 0 - you don't have any tabs.

How To Ask Questions How To Answer Questions
Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 191

if I modify the code to this below I can get a print out of 1 item before I click on the little X button to close the tab. I am doing the testing with just 1 tab in tabbed pane. I add minus 1 to get index 0 as you see in my first post. If I have no code after actionPerformed, the tab does not close upon clicking the little button with X on top. If I test the code with more than one tab, I can close all the tabs except the last one at index 0. Index 0 always throw the exception. I tried removeAll() with 1 tab and at index 0 of course and that throws an exception too.

Paul Balm
Ranch Hand

Joined: Dec 13, 2008
Posts: 63
Ha! It took me a while before I saw it.

Have a close look at the stacktrace in your post.

Your ArchiveTabPanel also listens on changes on the tab panel. When you remove the tab, a ChangeEvent is fired and the stateChanged() method on your ArchiveTabPanel is executed (although it seems to be in a different package?). This method has a logical error: It tries to get the title of tab with index -1.


SCJP 1.4 -- SCJD Java 2 -- OCM JEA 5
Rob Spoor

Joined: Oct 27, 2005
Posts: 20194

Paul is right. The problem doesn't occur on line 9 as I first thought, but in a completely different call to getTitleAt.
Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 191

Thank you Paul Balm. You were right. The problem was the wrong choice of method in changeListener (on 3rd line). It's important to thoroughly follow up on stack traces. Thanks Paul once again for bringing this to my attention.

I agree. Here's the link: http://aspose.com/file-tools
subject: JTabbedPane remove exception
jQuery in Action, 3rd edition