This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTabbedPane remove exception

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c is -1 which means that getTabCount() returned 0 - you don't have any tabs.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers-
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic