我在 JTable 中使用自定义 JComboBox 作为单元格编辑器。当用户使用键盘控件进入单元格时,它会尝试打开弹出窗口。这会导致以下错误:
java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1964)
at java.awt.Component.getLocationOnScreen(Component.java:1938)
at javax.swing.JPopupMenu.show(JPopupMenu.java:887)
at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:191)
at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(BasicComboBoxUI.java:859)
at javax.swing.JComboBox.setPopupVisible(JComboBox.java:796)
我看过一些文章指出这是一个已知问题,解决方案是设置:
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
然而,这无济于事。这到底应该做什么?
我读过的所有关于这个问题的线程和文章都对问题的性质非常模糊。
有没有人知道为什么会出现这个问题的性质?我的组合框非常自定义,因此有助于理解问题的根源,以便我可以修复代码。
这是在捕获并调用 setPopupVisible(true) 的组合框上的焦点获得事件时触发的;
public void focusGained(java.awt.event.FocusEvent e)
{
//if focus is gained then make sure we show the popup if it is suppose to be visible
setPopupVisible(true);
//and highlight the selected text if any
comboTextEditor.setCaretPosition(comboTextEditor.getText().length());
comboTextEditor.moveCaretPosition(0);
}
顺便说一句,我在 Java 1.7_40 中得到与 Java 1.6_45 相同的结果
全栈跟踪:
Exception in thread "AWT-EventQueue-1" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1964)
at java.awt.Component.getLocationOnScreen(Component.java:1938)
at javax.swing.JPopupMenu.show(JPopupMenu.java:887)
at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:191)
at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(BasicComboBoxUI.java:859)
at javax.swing.JComboBox.setPopupVisible(JComboBox.java:796)
at com.mbs.generic.view.swing.combobox.AutoCompleteComboBox$1.focusGained(AutoCompleteComboBox.java:185)
at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:203)
at java.awt.Component.processFocusEvent(Component.java:6179)
at java.awt.Component.processEvent(Component.java:6046)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4653)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:901)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:513)
at java.awt.Component.dispatchEventImpl(Component.java:4525)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:648)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:607)
at java.awt.EventQueue$1.run(EventQueue.java:605)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:621)
at java.awt.EventQueue$2.run(EventQueue.java:619)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:618)
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)
谢谢