我有一个奇怪的问题。我正在研究一个很长的程序,在那个程序中,我按照以下方式创建了一个JComboBox:
String[] s = {"aa", "bb", "cc"};
JComboBox try1 = new JComboBox(s);
try1.insertItemAt("", 0);
try1.setSelectedIndex(0);
AutoCompleteDecorator.decorate(try1);
当我运行程序时,如果用户在组合框的文本区域内双击,我会收到以下运行时错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.text.PlainDocument cannot be cast to javax.swing.text.StyledDocument
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.getParagraphElement(AutoCompleteDocument.java:309)
at javax.swing.text.Utilities.getParagraphElement(Utilities.java:850)
at javax.swing.text.Utilities.getWordStart(Utilities.java:631)
at javax.swing.text.DefaultEditorKit$BeginWordAction.actionPerformed(DefaultEditorKit.java:1750)
at javax.swing.text.DefaultEditorKit$SelectWordAction.actionPerformed(DefaultEditorKit.java:2163)
at javax.swing.text.DefaultCaret.selectWord(DefaultCaret.java:390)
at javax.swing.text.DefaultCaret.mousePressed(DefaultCaret.java:495)
at com.apple.laf.AquaCaret.mousePressed(AquaCaret.java:140)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.text.PlainDocument cannot be cast to javax.swing.text.StyledDocument
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.getParagraphElement(AutoCompleteDocument.java:309)
at javax.swing.text.Utilities.getParagraphElement(Utilities.java:850)
at javax.swing.text.Utilities.getWordStart(Utilities.java:631)
at javax.swing.text.DefaultEditorKit$BeginWordAction.actionPerformed(DefaultEditorKit.java:1750)
at javax.swing.text.DefaultEditorKit$SelectWordAction.actionPerformed(DefaultEditorKit.java:2163)
at javax.swing.text.DefaultCaret.selectWord(DefaultCaret.java:390)
at javax.swing.text.DefaultCaret.mouseClicked(DefaultCaret.java:415)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
但是,如果我创建一个非常简单的程序,只包含相同的JComboBox,我没有同样的问题。我发现这很奇怪,因为到目前为止我的程序中没有使用JComboBox。有人可以告诉我可能导致这种行为的原因。感谢。
答案 0 :(得分:0)
问题是由AutoCompleteDecorator
引起的。我停用它并使用AutoCompletion.enable(employeeDelete)
。我感谢所有帮助我的评论。