将项目从不同的类添加到 JComboBox
我一直在尝试在将新项目添加到后端数据库时更新 JComboBox。
在实际代码中,有一个单独的类来处理添加对话框,当添加新项目时,它会更新数据库,然后应通过调用主 GUI 类中接受 String 的方法将相同的项目添加到下拉菜单中。 (尝试遵循模型-视图-控制器)。
下面是一个触发错误的最小示例,尽管在实际应用程序中它会默默地失败。
我有一种预感,它与对象的实例有关。另外,为了填充列表,我使用 addItem() 迭代列表,以确保该列表正常工作并且 ComboBox 是可变的。
感谢汤姆的任何帮助
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)
I have been trying to get a JComboBox to update when a new item is added to a backend database.
In the actual code there is a separate class that handles an add dialog, when the new item is added it updates the database and then should add the same item to the dropdown menu by calling a method that accepts a String in the main GUI class. (trying to follow Model-View-Controller).
Below is a minimal example that triggers an error although in the actual application it fails silently.
I have an inkling it has to do with instances of objects. Also to populate the list I'm iterating over a list using addItem() to make sure that is working and the ComboBox is Mutable.
Thanks any help 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 that is thrown
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在 actionPerformed 方法中简单地调用
addItemFail("Item Fail");
,而不是testComboBox.addItemFail("Item Fail");
。编辑:并且您不应该在 addItemFail 方法中再次重新创建同一类的对象(再次这样做有什么意义?)
Instead of
testComboBox.addItemFail("Item Fail");
, you should simply calladdItemFail("Item Fail");
in your actionPerformed method.Edit: And you should not recreate object of same class again in addItemFail method (whats the point of doing it again?)
您的问题是您的类在其自身内部创建了另一个类实例。首先,您创建一个 TestComboBox(TCB),其中包含指向另一个 TestCombobox 的链接,该 TestCombobox 为 Null,因为您尚未将其设置为其他任何位置。
第一个TCB (A) 是在 main() 中创建的带有空 TCB 链接的 t。
这个TCB链接是通过在createAndShowGui()中创建一个新的TCB (B)(它顺便包含另一个空tcb链接)来设置的。这也是吸引所有面板和所有听众的一场。
因此,当您按下面板中的该按钮时,ActionEvent 就会被触发并被 B 拾取,因为它是唯一一个具有侦听器的事件。但随后它尝试访问 B 中为 null 的 TCB 链接,从而导致 nullpointerException。
更令人困惑的是,您的 addItemFail 创建了另一个 TCB,该 TCB 被添加到 B。
我对您的建议是重新思考和重新设计它。目前实在是太混乱了。此外,代码中没有任何注释,这使得代码背后的初衷更加难以理解。您将希望摆脱类内 TCB 创建的所有新创建。
Your problem is that your class creates another class instance of itself inside itself. First you create a TestComboBox(TCB) with a link to another TestCombobox which is Null as you haven't set it to anything else anywhere.
The first TCB (A) is your t with a null TCB link, created in main().
This TCB link is set by creating a new TCB (B) (which incidently contains another null tcb link) in createAndShowGui(). This one is also the one which gets all the panels and also all the listeners.
So when you press that button in the panel the actionevent is fired and picked up by B as it is the only one with the listeners. But then it tries to access the TCB link in B which is null causing the nullpointerexception.
Adding even more confusion is the fact that your addItemFail creates yet another TCB which gets added to B.
My suggestion to you is to rethink and redesign this. It's way too confusing at the moment. Furthermore there are not a single comment in the code which makes the original intent behind the code even harder to understand. You will want to get rid of all new creations of TCB creation inside the class.