我想2个jcomboboxes结合起来。 1组合框用于显示费用类别。第二个组合框是从文本文件中读取文件以显示产品的类型。如果我更改第一个组合框,我希望第二个组合框将根据用户在第一个组合框中选择的内容进行更改。JComboBox的另一个变化JComboBox的
是否有任何机会,我仍然可以从文本文件加载其他组合框?该子项不会是数组,而是与之前相同,因为它位于cboperson的代码底部。
编辑的代码:
private JComboBox cboCategory;
private JComboBox cboPerson;
private JComboBox cboItem;
public String itemChange = "groceries.txt";
public ExpenditureTracker() {......
String[] items = {"Select Item", "Groceries", "Bills", "Travelling", "Leasure", "Other"};
mainComboBox = new JComboBox(items);
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
//prevent action events from being fired when the up/down arrow keys are used
//mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
mainComboBox.setBounds(113, 138, 85, 20);
importPanel.add(mainComboBox);
subComboBox = new JComboBox();// Create sub combo box with multiple models
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
subComboBox.addItemListener(this);
subComboBox.setBounds(113, 188, 85, 20);
importPanel.add(subComboBox);
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
subItems.put(items[1], subItems1);
String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
subItems.put(items[2], subItems2);
String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[3], subItems3);
String[] subItems4 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[4], subItems3);
String[] subItems5 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[5], subItems3);
loadDataTocboPerson();
}
private void loadDataToCboPerson() {
Scanner fileReader = new Scanner(getClass().getResourceAsStream(
itemChange));
try {
DefaultComboBoxModel model = new DefaultComboBoxModel();
while (fileReader.hasNextLine()) {
model.addElement(fileReader.nextLine());
}
cboItem.setModel(model);
} finally {
fileReader.close();
}
}
你调用loadDataToCboItem()?你应该在displaySelectedItem()方法中从你的actionPerformed方法中调用它。 – 2011-06-05 21:52:12
请再编辑你的答案。代码格式不正确,不清楚这些方法属于哪个组合框。 – toto2 2011-06-05 21:54:36