0

I have a weird problem using a JTabbedPane and adding a new tab to it.

public void addTab(String title, AbstractTab tab) {
    int i = tabPanel.getTabCount();
    System.out.println("Tab count:" + i);
    tab.validate();
    tabPanel.insertTab(title, null, tab, title, i);
    tabPanel.validate();
    tabPanel.setSelectedIndex(i);
}

Running this code causes the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.JTabbedPane.getTitleAt(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.updateHtmlViews(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.componentAdded(Unknown Source)
at java.awt.Container.processContainerEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at javax.swing.JTabbedPane.insertTab(Unknown Source)
at View.Swing.Global.**EditorPanel.addTab(EditorPanel.java:383)**
at View.Swing.Developer.DeveloperContentPanel.showNote(DeveloperContentPanel.java:176)
at View.Swing.Developer.DeveloperTreeViewer.clickedTreeElement(DeveloperTreeViewer.java:62)
at View.Swing.Global.TreeNavigation.mouseClicked(TreeNavigation.java:556)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
...

The System.out.println(..); prints

Tab count 1 

The weird thing is, that this method works once, and another time it throws this exception. The added tabs are instances of the class AbstractTab, containing JTextFields, a MetaphaseEditor, scrollbars, JLabels and JTextAreas and some pictures (ImageIcon).

Does anyone know a solution for this problem?

2
  • 3
    You may not have enough information here to allow us to give an answer. Consider creating and posting an SSCCE -- a small compilable and runnable program that we can run within need of outside resources and that demonstrates your problem. Also, I think that the word you're looking for is "weird". Commented Jan 21, 2012 at 12:21
  • 3
    Why don't you use addTab() and setSelectedComponent()? Then you don't need to care about the index. Commented Jan 21, 2012 at 12:32

1 Answer 1

2

from Bugtracker, maybe you use a component more than once?

Clarify in documentation that no 2 tabs in a JTabbedPane can have the same component assigned to them.. causes java.lang.ArrayIndexOutOfBoundsException: 1 > 0

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the good idea :-) though when doing so, the actual stacktrace is a bit different from the one shown above (IndexOOB in JTabbedPane.checkIndex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.