Java - JTabbedPane - 添加新面板时出现 ArrayIndexOutOfBoundsException
我在使用 JTabbedPane 并向其添加新选项卡时遇到了一个奇怪的问题。
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);
}
运行此代码会导致以下异常:
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)
...
System.out.println(..);
打印
Tab count 1
奇怪的是,此方法工作一次,另一次抛出此异常。添加的选项卡是类 AbstractTab
的实例,包含 JTextFields
、MetaphaseEditor
、滚动条、JLabels
和 >JTextArea
和一些图片 (ImageIcon
)。
有谁知道这个问题的解决方案?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Bugtracker,也许您多次使用某个组件?
from Bugtracker, maybe you use a component more than once?