JAVA Class 工作不正常
JAVA Class not working properly
我有一个 loads/reloads JComboBox 的方法,我正在使用一个名为 hiddenItem 的 class 将隐藏值附加到显示值。起初加载 JComboBox 工作正常,但在 Box 重新加载后我得到错误,JComboBox 中的值不是 hiddenItem,那么为什么在重新加载后它不添加 hiddenItem?也许我需要以某种方式重置或清理 hiddenItem class?谢谢!
hiddenItem.java:
public class hiddenItem {
String displayValue;
Integer hiddenValue;
//Constructor
public hiddenItem(String d, Integer h) {
displayValue = d;
hiddenValue = h;
}
//Accessor
public Integer get_value() {
return hiddenValue;
}
//Override ToString method
@Override
public String toString() {
return displayValue;
}
}
Load/reload方法:
public static void fill_combos() throws SQLException, ParserConfigurationException, XPathExpressionException, SAXException, IOException, PropertyVetoException {
Darbai.onload = true;
ResultSet rs = null;
darb_combo.removeAllItems();
ats_combo.removeAllItems();
darb_combo.addItem(new hiddenItem("ALL", -1));
ats_combo.addItem(new hiddenItem("ALL", -1));
rs = select_data(GET_DARBUOTOJAI, null);
while (rs.next()) {
darb_combo.addItem(new hiddenItem(rs.getString("name") + " " + rs.getString("surname"), rs.getInt("id")));
ats_combo.addItem(new hiddenItem(rs.getString("name") + " " + rs.getString("surname"), rs.getInt("id")));
}
rs.close();
darb_combo.setSelectedIndex(0);
ats_combo.setSelectedIndex(0);
klientas_combo.removeAllItems();
klientas_combo.addItem(new hiddenItem("ALL", -1));
rs = select_data(GET_KLIENTAI, null);
while (rs.next()) {
klientas_combo.addItem(new hiddenItem(rs.getString("customer"), rs.getInt("id")));
}
rs.close();
klientas_combo.setSelectedIndex(0);
busena_combo.removeAllItems();
busena_combo.addItem(new hiddenItem("ALL", -1));
busena_combo.addItem(new hiddenItem("Reikia atlikti", 0));
busena_combo.addItem(new hiddenItem("Skubus", 1));
busena_combo.addItem(new hiddenItem("Vykdoma", 2));
busena_combo.addItem(new hiddenItem("Atlikta", 3));
busena_combo.addItem(new hiddenItem("Užaktuota", 4));
busena_combo.addItem(new hiddenItem("Garantinis", 5));
busena_combo.addItem(new hiddenItem("Objektinis", 6));
AutoCompleteDecorator.decorate(darb_combo);
AutoCompleteDecorator.decorate(ats_combo);
AutoCompleteDecorator.decorate(klientas_combo);
Darbai.onload = false;
}
下面是我如何获得隐藏值:
if (!Darbai.onload) {
if (e.getStateChange() == ItemEvent.SELECTED) {
ResultSet rs = null;
hiddenItem item = (hiddenItem) e.getItem(); // LINE 805
Integer id = item.get_value();
String val = item.displayValue;
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to darbai.hiddenItem
at darbai.pagrindinis.itemStateChanged(pagrindinis.java:805)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
at org.jdesktop.swingx.autocomplete.ComboBoxAdaptor.setSelectedItem(ComboBoxAdaptor.java:104)
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:317)
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.remove(AutoCompleteDocument.java:243)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1672)
at javax.swing.plaf.metal.MetalComboBoxEditor.setText(MetalComboBoxEditor.java:61)
at javax.swing.plaf.basic.BasicComboBoxEditor.setItem(BasicComboBoxEditor.java:87)
at org.jdesktop.swingx.autocomplete.AutoCompleteComboBoxEditor.setItem(AutoCompleteComboBoxEditor.java:78)
at javax.swing.JComboBox.configureEditor(JComboBox.java:1403)
at javax.swing.plaf.basic.BasicComboBoxUI$Handler.contentsChanged(BasicComboBoxUI.java:1864)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:834)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at javax.swing.plaf.basic.BasicComboPopup.processMouseEvent(BasicComboPopup.java:498)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
编辑组合定义(NetBeans 自动):
public static javax.swing.JComboBox ats_combo;
public static javax.swing.JComboBox busena_combo;
public static javax.swing.JComboBox darb_combo;
public static javax.swing.JComboBox klientas_combo;
darb_combo = new javax.swing.JComboBox();
ats_combo = new javax.swing.JComboBox();
klientas_combo = new javax.swing.JComboBox();
busena_combo = new javax.swing.JComboBox();
darb_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
ats_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
klientas_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
busena_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
加载您的代码工作正常,但在重新加载时组合框具有默认值,正如您列出的那样是一个字符串值数组
new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }
所以也加载带有隐藏项对象数组的组合框作为默认值。
您已经指出的错误发生在:
hiddenItem item = (hiddenItem) e.getItem(); // LINE 805
因为getItem returns 一个你定义的String默认对象。
使用以下代码代替 combos
的默认值
darb_combo.setModel(new javax.swing.DefaultComboBoxModel(new hiddenItem[] { new hiddenItem("Item 1", 1), new hiddenItem("Item 2", 2), new hiddenItem("Item 3",3), new hiddenItem("Item 4",4 }));
附带说明,由于 hiddenItem 是 class,请遵循约定并将其重命名为 HiddenItem
我有一个 loads/reloads JComboBox 的方法,我正在使用一个名为 hiddenItem 的 class 将隐藏值附加到显示值。起初加载 JComboBox 工作正常,但在 Box 重新加载后我得到错误,JComboBox 中的值不是 hiddenItem,那么为什么在重新加载后它不添加 hiddenItem?也许我需要以某种方式重置或清理 hiddenItem class?谢谢!
hiddenItem.java:
public class hiddenItem {
String displayValue;
Integer hiddenValue;
//Constructor
public hiddenItem(String d, Integer h) {
displayValue = d;
hiddenValue = h;
}
//Accessor
public Integer get_value() {
return hiddenValue;
}
//Override ToString method
@Override
public String toString() {
return displayValue;
}
}
Load/reload方法:
public static void fill_combos() throws SQLException, ParserConfigurationException, XPathExpressionException, SAXException, IOException, PropertyVetoException {
Darbai.onload = true;
ResultSet rs = null;
darb_combo.removeAllItems();
ats_combo.removeAllItems();
darb_combo.addItem(new hiddenItem("ALL", -1));
ats_combo.addItem(new hiddenItem("ALL", -1));
rs = select_data(GET_DARBUOTOJAI, null);
while (rs.next()) {
darb_combo.addItem(new hiddenItem(rs.getString("name") + " " + rs.getString("surname"), rs.getInt("id")));
ats_combo.addItem(new hiddenItem(rs.getString("name") + " " + rs.getString("surname"), rs.getInt("id")));
}
rs.close();
darb_combo.setSelectedIndex(0);
ats_combo.setSelectedIndex(0);
klientas_combo.removeAllItems();
klientas_combo.addItem(new hiddenItem("ALL", -1));
rs = select_data(GET_KLIENTAI, null);
while (rs.next()) {
klientas_combo.addItem(new hiddenItem(rs.getString("customer"), rs.getInt("id")));
}
rs.close();
klientas_combo.setSelectedIndex(0);
busena_combo.removeAllItems();
busena_combo.addItem(new hiddenItem("ALL", -1));
busena_combo.addItem(new hiddenItem("Reikia atlikti", 0));
busena_combo.addItem(new hiddenItem("Skubus", 1));
busena_combo.addItem(new hiddenItem("Vykdoma", 2));
busena_combo.addItem(new hiddenItem("Atlikta", 3));
busena_combo.addItem(new hiddenItem("Užaktuota", 4));
busena_combo.addItem(new hiddenItem("Garantinis", 5));
busena_combo.addItem(new hiddenItem("Objektinis", 6));
AutoCompleteDecorator.decorate(darb_combo);
AutoCompleteDecorator.decorate(ats_combo);
AutoCompleteDecorator.decorate(klientas_combo);
Darbai.onload = false;
}
下面是我如何获得隐藏值:
if (!Darbai.onload) {
if (e.getStateChange() == ItemEvent.SELECTED) {
ResultSet rs = null;
hiddenItem item = (hiddenItem) e.getItem(); // LINE 805
Integer id = item.get_value();
String val = item.displayValue;
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to darbai.hiddenItem
at darbai.pagrindinis.itemStateChanged(pagrindinis.java:805)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
at org.jdesktop.swingx.autocomplete.ComboBoxAdaptor.setSelectedItem(ComboBoxAdaptor.java:104)
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:317)
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.remove(AutoCompleteDocument.java:243)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1672)
at javax.swing.plaf.metal.MetalComboBoxEditor.setText(MetalComboBoxEditor.java:61)
at javax.swing.plaf.basic.BasicComboBoxEditor.setItem(BasicComboBoxEditor.java:87)
at org.jdesktop.swingx.autocomplete.AutoCompleteComboBoxEditor.setItem(AutoCompleteComboBoxEditor.java:78)
at javax.swing.JComboBox.configureEditor(JComboBox.java:1403)
at javax.swing.plaf.basic.BasicComboBoxUI$Handler.contentsChanged(BasicComboBoxUI.java:1864)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:834)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at javax.swing.plaf.basic.BasicComboPopup.processMouseEvent(BasicComboPopup.java:498)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
编辑组合定义(NetBeans 自动):
public static javax.swing.JComboBox ats_combo;
public static javax.swing.JComboBox busena_combo;
public static javax.swing.JComboBox darb_combo;
public static javax.swing.JComboBox klientas_combo;
darb_combo = new javax.swing.JComboBox();
ats_combo = new javax.swing.JComboBox();
klientas_combo = new javax.swing.JComboBox();
busena_combo = new javax.swing.JComboBox();
darb_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
ats_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
klientas_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
busena_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
加载您的代码工作正常,但在重新加载时组合框具有默认值,正如您列出的那样是一个字符串值数组
new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }
所以也加载带有隐藏项对象数组的组合框作为默认值。
您已经指出的错误发生在:
hiddenItem item = (hiddenItem) e.getItem(); // LINE 805
因为getItem returns 一个你定义的String默认对象。 使用以下代码代替 combos
的默认值darb_combo.setModel(new javax.swing.DefaultComboBoxModel(new hiddenItem[] { new hiddenItem("Item 1", 1), new hiddenItem("Item 2", 2), new hiddenItem("Item 3",3), new hiddenItem("Item 4",4 }));
附带说明,由于 hiddenItem 是 class,请遵循约定并将其重命名为 HiddenItem