gpt4 book ai didi

java - 删除所有项目后无法单击组合框

转载 作者:行者123 更新时间:2023-11-29 05:24:13 24 4
gpt4 key购买 nike

我正在创建用项目代码列表填充组合框的对话框,当我单击组合框上的特定项目时,它会从数据库中获取信息并使用 jLabels 在同一对话框中显示。有时我想禁用组合框,我会执行 removeAllItems 并使用复选框设置 jLabels 空字符串,然后继续下一步。当我再次启用时,我用相同的对象填充组合框,但是当我点击它时,它不会点击并产生错误。当我为 removeAllItems 编码时,它工作正常。

这是我的代码片段

//我的对话框类

/// Fields 

MsSqlDbProperties searchcomboitem;
MsSqlDbProperties objcombo;
String combos;
Object comboitem ;

///从另一个类调用我的对话框的构造函数

  public myDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
objcombo = new MsSqlDbProperties();
objcombo.populatecombo(this);
}

//组合框项目被点击

      private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
System.out.println("Combo Item Selected");
comboitem = jComboBox2.getSelectedItem();
combos = comboitem.toString();
System.out.println(combos);
objcombo.SearchProject(this, combos);
}

//禁用组合框

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

if (jComboBox2.isEnabled()) {

jComboBox2.setEnabled(false);
this.jComboBox2.removeAllItems();
System.out.println(" Combo Disabled...");

} else {

System.out.println(" Combo Enabled...");
jComboBox2.setEnabled(true);
objcombo.populatecombo(this);

}

}

//MyDB属性类

 /// Assume Connection Made here

/// my Search Project Method


public boolean SearchProject(PurchaseDialog pd , String s) {


boolean resp = false;
searchcon = connector();

try {

String Sqlse = "SELECT * FROM ProjectAccount WHERE ProjectCode = ? ";
PreparedStatement searchcmd = searchcon.prepareStatement(Sqlse);
searchcmd.setString(1,s);

ResultSet rst = searchcmd.executeQuery();

if (rst.next()) {
String add1 = rst.getString("ProjectName");

pd.projectName.setText(add1);
System.out.println("AFTER DISABLE");

System.out.println(add1);
String add2 = rst.getString("ProjectCode");
pd.projectCodelabel.setText(add2);
System.out.println(add2);

String add3 = rst.getString("StartDate");
pd.StartDate.setText(add3);
System.out.println(add3);

String add4 = rst.getString("EndDate");
pd.EndDate.setText(add4);
System.out.println(add4);

}

searchcmd.close();
searchcon.close();

resp = true;

} catch (Exception ex) {

System.out.println("Error " + ex.getMessage());

}

return resp;
}

//我的填充组合框方法

 public boolean populatecombo(PurchaseDialog pdc) {

System.out.println("in populatecombo");
boolean resp = false;
populatecon = connector();

try {
String Sqlsc = "SELECT ProjectCode FROM ProjectAccount";
PreparedStatement combocmd = populatecon.prepareStatement(Sqlsc);
ResultSet rsc = combocmd.executeQuery();

while (rsc.next()) {
pdc.jComboBox2.addItem(rsc.getString("ProjectCode"));

}

combocmd.close();
populatecon.close();

} catch (Exception ex) {
System.out.println("Error " + ex.getMessage());
}

return resp;

}

这是错误列表

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at
ptakip.PurchaseDialog.jComboBox2ActionPerformed(PurchaseDialog.java:493) at
ptakip.PurchaseDialog.access$100(PurchaseDialog.java:14) at
ptakip.PurchaseDialog$2.actionPerformed(PurchaseDialog.java:148) at
javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260) at
javax.swing.JComboBox.contentsChanged(JComboBox.java:1331) at
javax.swing.JComboBox.intervalRemoved(JComboBox.java:1351) at
javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179) at
javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174) at
javax.swing.JComboBox.removeAllItems(JComboBox.java:773) at
ptakip.PurchaseDialog.jCheckBox1ActionPerformed(PurchaseDialog.java:510) at
ptakip.PurchaseDialog.access$300(PurchaseDialog.java:14) at
ptakip.PurchaseDialog$4.actionPerformed(PurchaseDialog.java:165) at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at
javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308) at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3320) 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:4492) 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.pumpEventsForFilter(EventDispatchThread.java:154) at
java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182) at
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) 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.awt.EventQueue.dispatchEvent(EventQueue.java:703) 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)

最佳答案

在删除所有项目后单击 jComboBox2 时,所选项目将为空。请在执行 Activity 之前检查 null 或项目计数。给出示例代码

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt)         
{
System.out.println("Combo Item Selected");
comboitem = jComboBox2.getSelectedItem();
if(null==comboitem)
return ;

combos = comboitem.toString();
System.out.println(combos);
objcombo.SearchProject(this, combos);
}

关于java - 删除所有项目后无法单击组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23341744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com