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?
addTab()andsetSelectedComponent()? Then you don't need to care about the index.