gpt4 book ai didi

swing - Java JRE 1.7 JCombobox setSelectedItem(Object anObject) 不能正常工作?

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

请看下面的代码片段,

    String[] choices = {"Apple", "Banana", "Custard"};  
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");

它抛出空指针异常。见下文,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.EventQueue.getCurrentEventImpl(Unknown Source)
at java.awt.EventQueue.getCurrentEvent(Unknown Source)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)

同样的问题也发生在 setSelectedIndex() 上。如果 Java JRE 1.7 有任何问题,请为这个问题提出好的解决方法或建议我。

最佳答案

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.awt.EventQueue.getCurrentEventImpl(Unknown Source) at java.awt.EventQueue.getCurrentEvent(Unknown Source) at javax.swing.JComboBox.fireActionEvent(Unknown Source) at javax.swing.JComboBox.setSelectedItem(Unknown Source)

来自

String[] choices = {"Apple", "Banana", "Custard"};  
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");

仅在调用 setSelectedItem 之前添加 Action/ItemListener 的情况下才有可能生成(调试那个,什么是 XxxListener 触发) , 改为

String[] choices = {"Apple", "Banana", "Custard"};  
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");
fruits.addAction / ItemListener(new Action / ItemListener)

Java6 中同样的问题

@sanjay wrote if i add that actionlistener to the combobox. it gives the same error. But it working properly in java 1.6 without that Combobox generic type.

  • 不,我不是在说,你可以从这段代码中生成这个异常

    1. 注释代码行(//)mainComboBox.setSelectedItem("Fruit");

    2. 并取消注释//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);
}
});
}
}

关于swing - Java JRE 1.7 JCombobox<E> setSelectedItem(Object anObject) 不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812592/

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