使用JTabbedPane时抛出异常 [英] Exception thrown while working with JTabbedPane
问题描述
ChangeListener
来聆听它的更改,以便我可以知道当前选择了哪个选项卡。 所以我的
stateChanged
方法是; public void stateChanged (ChangeEvent e){
currentPageIndex = jTabbedPane.getSelectedIndex();
}
但是,当我添加新的标签到 JTabbedPane
它会在上面的方法中抛出一个 ArrayIndexOutOfBoundsException
,我不知道为什么。有些人提出了类似的情况,这是一个错误 http://bugs.sun .com / bugdatabase / view_bug.do?bug_id = 4873983 ,但是您会看到一些说明的解决方案是从EventDispatchThread处理Swing。
这是什么意思,是否意味着 SwingUtilities.invokeLater
的东西?有人可以告诉我如何可以修改我的 stateChanged
方法以避免异常?
编辑:以下是我的 addTab()
方法。这在程序开始被多次调用,它有时会触发 stateChanged()
方法。而在某些时候, stateChanged()
导致异常。这就是我从调试器到现在可以告诉的。我知道这似乎不清楚,所以如果你以前还没有听到过这样的话,那没关系,谢谢你的努力。但是我认为这可能与上面给出的链接有关。
void addTab(EJournal eJournalModel,int index,String pageName) {
SectionPage newPage = new SectionPage(jTabbedPane.getSize(),controller,eJournalModel.getSections()。get(currentPageIndex));
JScrollPane scrollPane = new JScrollPane(newPage);
scrollPanes.add(index,scrollPane);
sectionPages.add(index,newPage);
jTabbedPane.insertTab(pageName,idleIcon,scrollPane,null,index);
jTabbedPane.updateUI();
}
Edit2:抛出此异常。在我的方法实际执行完毕并且完成之后,一个 actionPerformed()
被触发,然后是一大堆东西然后异常。
线程中的异常AWT-EventQueue-0java.lang.ArrayIndexOutOfBoundsException:0
javax.swing.plaf.basic.BasicTabbedPaneUI.tabForCoordinate(BasicTabbedPaneUI .java:1488)
在javax.swing.plaf.basic.BasicTabbedPaneUI.setRolloverTab(BasicTabbedPaneUI.java:558)
在javax.swing.plaf.basic.BasicTabbedPaneUI.access $ 2000(BasicTabbedPaneUI.java: 37)
在javax.swing.plaf.basic.BasicTabbedPaneUI $ Handler.mouseEntered(BasicTabbedPaneUI.java:3604)
在java.awt.Component.processMouseEvent(Component.java:6272)
java.awt.Component.processEvent(Component.java:6028)中的
:2041)
在java.awt.Comp onent.dispatchEventImpl(Component.java:4630)
在java.awt.Container.dispatchEventImpl(Container.java:2099)
在java.awt.Component.dispatchEvent(Component.java:4460)
在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
在java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4363)
在java.awt.LightweightDispatcher.processMouseEvent(容器.java:4220)
在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
在java.awt.Container.dispatchEventImpl(Container.java:2085)
在java。 awt.Window.dispatchEventImpl(Window.java:2475)
在java.awt.Component.dispatchEvent(Component.java:4460)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.ja va:184)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
在java.awt .EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
您没有发布SSCCE。 5行代码不是SSCCE。
有一件事你不应该使用 updateUI()方法。我不知道为什么我经常在发布的代码中看到这个。
I'm using a JTabbedPane in my application and I listen to its changes with ChangeListener
so that I can know which tab is currently selected.
So my stateChanged
method is;
public void stateChanged(ChangeEvent e) {
currentPageIndex = jTabbedPane.getSelectedIndex();
}
But while I'm adding new tabs to the JTabbedPane
it throws an ArrayIndexOutOfBoundsException
in the method above, I don't know why. Some suggested for a similar case that this is a bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4873983, but as you'll see some says the solution is to work with Swing from EventDispatchThread.
What does it mean, do they mean the SwingUtilities.invokeLater
thing? Can someone show me how I can modify my stateChanged
method accordingly to avoid the exception?
Edit: And below is my addTab()
method. This is called multiple times at the beginning of the program, and it 'sometimes' fires stateChanged()
method. And at some point stateChanged()
causes the exception. That's what I could tell from the debugger up to now. I know it doesn't seem clear, so if you haven't already heard this kind of thing before, that's OK, thanks for your effort. But I think this could be related to the link I gave above.
void addTab(EJournal eJournalModel,int index, String pageName) {
SectionPage newPage = new SectionPage(jTabbedPane.getSize(), controller, eJournalModel.getSections().get(currentPageIndex));
JScrollPane scrollPane = new JScrollPane(newPage);
scrollPanes.add(index, scrollPane);
sectionPages.add(index, newPage);
jTabbedPane.insertTab(pageName, idleIcon, scrollPane, null, index);
jTabbedPane.updateUI();
}
Edit2 : This exception is thrown. After my methods actually finished executing and evertying is finished, an actionPerformed()
is fired, then a whole bunch of stuff and then exception.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at javax.swing.plaf.basic.BasicTabbedPaneUI.tabForCoordinate(BasicTabbedPaneUI.java:1488)
at javax.swing.plaf.basic.BasicTabbedPaneUI.setRolloverTab(BasicTabbedPaneUI.java:558)
at javax.swing.plaf.basic.BasicTabbedPaneUI.access$2000(BasicTabbedPaneUI.java:37)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mouseEntered(BasicTabbedPaneUI.java:3604)
at java.awt.Component.processMouseEvent(Component.java:6272)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4363)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
You did not post a SSCCE. 5 lines of code is NOT a SSCCE.
For one thing you should NOT be using the updateUI() method. I have no idea why I see this so often in posted code.
这篇关于使用JTabbedPane时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!