简体   繁体   中英

Java - JTabbedPane - ArrayIndexOutOfBoundsException when adding a new panel

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 JTextArea s and some pictures ( ImageIcon ).

Does anyone know a solution for this problem?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM