从不同的类向JComboBox添加项

时间:2011-09-19 19:41:17

标签: java swing user-interface jcombobox

我一直在尝试将新项目添加到后端数据库时更新JComboBox。

在实际代码中有一个单独的类来处理添加对话框,当添加新项时它会更新数据库,然后通过调用一个接受主要字符串的方法将相同的项添加到下拉菜单中GUI类。 (试图遵循模型 - 视图 - 控制器)。

下面是一个触发错误的最小示例,尽管在实际应用程序中它会无声地失败。

我知道它与对象的实例有关。另外要填充列表,我使用addItem()迭代列表,以确保它正常工作,并且ComboBox是可变的。

感谢任何帮助Tom

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestComboBox extends JPanel implements ActionListener{

JComboBox moduleList = new JComboBox(new DefaultComboBoxModel());
TestComboBox testComboBox;
JFrame frame;

public void actionPerformed(ActionEvent e){
    if("additem".equals(e.getActionCommand())){
        addItem("Item");
    }
    if("additemfail".equals(e.getActionCommand())){
        testComboBox.addItemFail("Item Fail");
    }
}

public void addItem(String item){
    moduleList.addItem(item);
}

public void addItemFail(String item){
    testComboBox = new TestComboBox();
    moduleList.addItem(item);
}


protected JPanel createPanel(){
    JPanel panel = new JPanel(false);

    String[] getModuleList = {"MODULE 1", "MODULE 2"};
    moduleList = new JComboBox(new DefaultComboBoxModel(getModuleList));
    panel.add(moduleList);

    JButton additem = new JButton("Add Item");
    additem.setActionCommand("additem");
    additem.addActionListener(this);
    panel.add(additem);

    JButton additemfail = new JButton("Add Item Fail");
    additemfail.setActionCommand("additemfail");
    additemfail.addActionListener(this);
    panel.add(additemfail);

    return panel;
}

public void createAndShowGui(){
    testComboBox = new TestComboBox();
    frame = new JFrame("JComboTest");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(testComboBox.createPanel());
    frame.setSize(450, 150);
    frame.setVisible(true);
}

public static void main(String[] args){
    TestComboBox t = new TestComboBox();
    t.createAndShowGui();
}
}

抛出的异常

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TestComboBox.actionPerformed(TestComboBox.java:16)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6268)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6033)
    at java.awt.Container.processEvent(Container.java:2045)
    at java.awt.Component.dispatchEventImpl(Component.java:4629)
    at java.awt.Container.dispatchEventImpl(Container.java:2103)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.java:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

2 个答案:

答案 0 :(得分:1)

您只需在actionPerformed方法中调用testComboBox.addItemFail("Item Fail");,而不是addItemFail("Item Fail");

编辑:你不应该在addItemFail方法中再次重新创建同一个类的对象(重点是什么呢?)

答案 1 :(得分:1)

你的问题是你的类在其自身内部创建了另一个类实例。首先,您创建一个TestComboBox(TCB),其中包含指向 Null 的另一个TestCombobox的链接,因为您尚未将其设置为任何其他位置。

第一个 TCB (A)是你的t,它是一个空的TCB链接,在main()中创建。

TCB 链接是通过在createAndShowGui()中创建新的TCB(B)(,其中包含另一个空tcb链接)来设置的。这个也是获得所有小组和所有听众的那个。

因此,当你按下面板中的那个按钮时,动作事件会被B触发并被B拾取,因为它是唯一一个有听众的动作。但后来它试图访问B中的TCB链接导致nullpointerexception。

更令人困惑的是,你的addItemFail创建了另一个被添加到B的TCB。

我的建议是重新思考并重新设计。这一刻太混乱了。此外,代码中没有一条注释使得代码背后的原始意图更难以理解。您将希望摆脱课堂内TCB创作的所有新创作。