java.awt.EventQueue.getCurrentEventImpl(Unknown Source) at java.awt.EventQueue.getCurrentEvent(Unknown Source) at java.swing.JComboBox.fireActionEvent(Unknown Source) 线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException ) 在 javax.swing.JComboBox.setSelectedItem(Unknown Source)
从
String[] choices = {"Apple", "Banana", "Custard"};
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");
Action/ItemListener
只能在调用之前添加的情况下生成setSelectedItem
(调试,XxxListener
触发什么),更改为
String[] choices = {"Apple", "Banana", "Custard"};
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");
fruits.addAction / ItemListener(new Action / ItemListener)
和 Java6 中的相同问题
@sanjay 写道,如果我将该动作侦听器添加到组合框中。它给出了同样的错误。但它在没有 Combobox 泛型类型的 java 1.6 中正常工作。
不,我不是在说,你可以从这段代码中生成这个异常
注释代码行 (//)mainComboBox.setSelectedItem("Fruit");
并取消注释 //mainComboBox.setSelectedItem("Shape");
然后这段代码触发了相同的异常,对于 JComboBox 来说是很常见的问题,在 Java6 中也是同样的问题(通过从 JComboBox 定义中删除泛型)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class FruitAndVedg extends JFrame implements ActionListener, ItemListener {
private static final long serialVersionUID = 4L;
private JComboBox<String> mainComboBox;
private JComboBox<String> subComboBox;
private ArrayList item;
private Hashtable<Object, Object> subItems = new Hashtable<>();
public FruitAndVedg() {
item = new ArrayList();
item.add("Select Item");
item.add("Fruit");
item.add("Vedg");
String[] items = {"Select Item", "Color", "Shape", "Fruit"};
mainComboBox = new JComboBox<>(items/*item.toArray()*/);
mainComboBox.setSelectedItem("Fruit");
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
//mainComboBox.setSelectedItem("Shape");
add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox<>();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX");
add(subComboBox, BorderLayout.CENTER);
String[] subItems1 = {"Select Fruit", "Apple", "Plum"};
subItems.put(items, subItems1);
String[] subItems2 = {"Select Vedg", "Carrot", "Peas"};
subItems.put(items, subItems2);
}
@Override
public void actionPerformed(ActionEvent ae) {
String item = (String) mainComboBox.getSelectedItem();
Object o = subItems.get(item);
if (o == null) {
subComboBox.setModel(new DefaultComboBoxModel());
} else {
subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
}
}
@Override
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
if (ie.getSource() == mainComboBox) {
if (mainComboBox.getSelectedIndex() != 0) {
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new FruitAndVedg();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}