我创建一个包含3个组合框的框架,每个框架依赖于另一个,第3个依赖于第2个,第2个依赖于第1个。 问题是当我在第3次更改第一次获得NullPointer异常时,因为它的操作是第2次更改操作。
我的问题是如何防止第3个jComboBox上的项目更改动作" jCombobox2"当我改变第一个jComboBox" jComboBox0" ?
这是我的代码:
private void jComboBox0ItemItemStateChanged(ItemEvent event) {
jComboBox1.removeAllItems();
ComboItem cat = (ComboItem) jComboBox0.getSelectedItem();
String requete = "from Subcategory where Fk_Category = " + cat.getValue();
Collection subcategories = Subcategory.getListeSubcategory(requete);
for (Iterator i = subcategories.iterator(); i.hasNext();) {
Subcategory item = new Subcategory();
item = (Subcategory) i.next();
System.out.println(item.getId());
jComboBox1.addItem(new ComboItem(item.getNom(), (int) item.getId()));
}
// System.out.println("tbdlat a lkhra ! : "+listCategory.get(0));
}
private void jComboBox1ItemItemStateChanged(ItemEvent event) {
// nda2
jComboBox2.removeAllItems();
ComboItem cat = (ComboItem) jComboBox1.getSelectedItem();
String requete = "from Area where fk_Subcategory = " + cat.getValue()+" group by Nom_Area";
Collection areas = Area.getListeArea(requete);
for (Iterator i = areas.iterator(); i.hasNext();) {
Area item = new Area();
item = (Area) i.next();
System.out.println(item.getId());
jComboBox2.addItem(new ComboItem(item.getNom(), (int) item.getId()));
}
}
private void jComboBox2ItemItemStateChanged(ItemEvent event) {
// i'll do some code here
}
答案 0 :(得分:0)
您的代码的问题似乎是您没有检查是否选择了元素。如果选择了某个项,则应该在监听器内部进行检查,如果不进行检查,则会调用itemStateChange方法两次。
我在下面发布了一个代码,其中包含三个组合框和一个用于所有三个组合的侦听器:
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class MainWindow1 extends JFrame implements ItemListener {
public MainWindow1() {
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
setLayout(new FlowLayout());
JComboBox petList1 = new JComboBox(petStrings);
//petList1.setSelectedIndex(4);
petList1.addItemListener(this);
petList1.setName("petList1");
JComboBox petList2 = new JComboBox(petStrings);
petList2.setSelectedIndex(4);
petList2.addItemListener(this);
petList2.setName("petList2");
JComboBox petList3 = new JComboBox(petStrings);
petList3.setSelectedIndex(4);
petList3.addItemListener(this);
petList3.setName("petList3");
getContentPane().add(petList1);
getContentPane().add(petList2);
getContentPane().add(petList3);
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new MainWindow1();
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList1") && e.getStateChange() == ItemEvent.SELECTED) {
System.out.println( "1" + e.getSource());
} else if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList2") && e.getStateChange() == ItemEvent.SELECTED) {
System.out.println( "2" + e.getSource());
} else if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList3") && e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("3" + e.getSource());
}
}
}