Java - JTabbedPane - 添加新面板时出现 ArrayIndexOutOfBoundsException

发布于 2024-12-28 03:07:35 字数 1814 浏览 1 评论 0原文

我在使用 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 的实例,包含 JTextFieldsMetaphaseEditor、滚动条、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 JTextAreas and some pictures (ImageIcon).

Does anyone know a solution for this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝情姑娘 2025-01-04 03:07:35

来自 Bugtracker,也许您多次使用某个组件?

在文档中澄清,JTabbedPane 中的 2 个选项卡不能具有
分配给他们的相同组件..原因
java.lang.ArrayIndexOutOfBoundsException: 1 > > 0

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文