PropertyChangeListener中的JOptionPane.showMessageDialog会导致意外行为(JOptionPane.showMessageDialog inside PropertyChangeListener causes unexpected behavior)
我用JCheckBoxMenuItem创建了一个可检查的菜单项。 我试图做的是当复选框被选中时,应该出现一条消息:
JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Checkbox"); checkbox.addPropertyChangeListener(evt -> { boolean isCheck = ((JCheckBoxMenuItem) evt.getSource()).isSelected(); if(isCheck){ JOptionPane.showMessageDialog(null, "You checked the checkbox", "Information", JOptionPane.INFORMATION_MESSAGE); } });但是,当我尝试选中复选框时,没有任何内容出现。 第二次点击JMenu出现一堆模态,并导致一个ArrayIndexOutOfBoundsException :
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0 at java.util.Vector.elementAt(Vector.java:474) at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:117) at javax.swing.MenuSelectionManager.clearSelectedPath(MenuSelectionManager.java:151) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.cancelPopupMenu(BasicPopupMenuUI.java:917) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:828) at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2425) at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2317) at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2275) at java.awt.Component.dispatchEventImpl(Component.java:4777) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109) ...是什么导致这种情况发生?
I have created a checkable menu item with JCheckBoxMenuItem. What I tried to do is when the checkbox gets checked, a message should appear:
JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Checkbox"); checkbox.addPropertyChangeListener(evt -> { boolean isCheck = ((JCheckBoxMenuItem) evt.getSource()).isSelected(); if(isCheck){ JOptionPane.showMessageDialog(null, "You checked the checkbox", "Information", JOptionPane.INFORMATION_MESSAGE); } });However when I tried to check the checkbox, nothing appears. The second time I click on the JMenu a bunch of modal appears, and causes an ArrayIndexOutOfBoundsException:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0 at java.util.Vector.elementAt(Vector.java:474) at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:117) at javax.swing.MenuSelectionManager.clearSelectedPath(MenuSelectionManager.java:151) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.cancelPopupMenu(BasicPopupMenuUI.java:917) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:828) at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2425) at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2317) at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2275) at java.awt.Component.dispatchEventImpl(Component.java:4777) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109) ...What is causing that to happen?
更多推荐
发布评论