- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请看下面的代码片段,
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.
不,我不是在说,你可以从这段代码中生成这个异常
注释代码行(//)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);
}
});
}
}
关于swing - Java JRE 1.7 JCombobox<E> setSelectedItem(Object anObject) 不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812592/
我在使用 JComboBox 的 setSelectedItem 方法时遇到问题。它只是不起作用。这是我的代码;我的站类和分区类都还可以。所以他们不包括在内。无论我尝试什么,我都无法将项目设置为组合框
我创建了一个包含三个项目的组合框。我正在尝试按索引和值设置所选项目。 当我执行 setSelectedIndex () 时,代码运行良好。 我正在尝试按值设置所选项目。所以我尝试创建另一个具有相同值(
我在设置自定义 JComboBox 的精确值时遇到问题。如果我从以下类的 initialize() 方法调用 setSelectedItem(),它不会选择特定值。 扩展的 JComboBox 类是:
设置所选项目的原始代码是: public void setSelectedItem(Object anObject) { Object oldSelection = selectedItemR
我的 jTable 第 8 列是美国州列。这是我的代码,用于使用适当的项目设置 jComboBox 以显示状态。 当我使用 Buffered Reader 填充 Jtable 时,下面的代码可以很好地
我试图在填充组合框之后在我的 JPanel 类的构造函数中设置 JComboBox 的 setSelectedItem。 我为文本框设置了值,但我不明白为什么 setSelectedItem 似乎不起
我的代码很简单......我想当我按下按钮时将面板中所有标签的文本设置为“”(无)/正在工作/并将所有组合框设置为值1。//这不起作用。它仅将 ArrayList amout 中的第一个组合框(七个组
我有一个JComboBox JComboBox tableChoose = new JComboBox(); tableChoose.addItem("Bill"); tableChoose.addI
我在 JDialog 中有一个以 JComboBox 开头的表单: myJComboBox = new JComboBox(itemOfBox); myJpanel.add(myJComboBox);
我有一个 JTable,我将三个 JComboBox 添加到三个不同的列。现在我想为我的每一行设置所选项目。问题是,我需要每一行的 ID 才能做到这一点。所以我尝试了不同的监听器,最好的结果是使用 F
我有一个包含多个组合框的程序,每个组合框都有自己的 Action 监听器。从任一组合框中选择一项将更改一个或多个其他组合框中的项目。我遇到的问题是,为一个组合框调用 setSelectedItem()
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 7 年前。 我无法理解为什么这段代码不起作用
本文整理了Java中org.pentaho.ui.xul.components.XulMenuList.setSelectedItem()方法的一些代码示例,展示了XulMenuList.setSel
我有一个在 JComboBox 中值更改事件时调用的方法 public void actionPerformed( ActionEvent e ) { Object source = e.ge
请看下面的代码片段, String[] choices = {"Apple", "Banana", "Custard"}; JComboBox fruits = new JCombo
我是一名优秀的程序员,十分优秀!