JComboBox动作监听器

我有这个问题。 我有多个JComboBoxes(共5个)。

对于每个comboBox,我添加一个ActionListener,但所有这些都是相同的ActionListener,名为:

ComboBoxActionPerformed(java.awt.event.ActionEvent e) 

当执行该动作时,我会查看事件(e)并执行:

 JComboBox c = ((JComboBox)e.getSource()); //DO WORK relating to c as thats the combobox that triggered. 

但问题是,当我在任何combobox中更改某些内容时,Action总是由我附加actionlistner的最后一个combobox触发。

有谁有任何想法?

然后我切换到ItemListner。 这就是我正在做的事情

 class MyActionListner implements ItemListener { //STUFF @Override public void itemStateChanged(ItemEvent evt) { //DO STUFF } } public JComboBox createCombo() { JComboBox box = new JComboBox(); box.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "val1", "val2","val3" })); RulesActionListner actionL = new RulesActionListner(); box.addItemListener(actionL); return box; } 

并且多次调用createCombo,但无论在我的ItemStateChanged方法中哪个combobox项被更改,它都会从最后创建的combobox中进行更改

createCombo是在运行时调用的,所以我有一个可变数量的combobox。

添加单独的动作侦听器,而不是让每个调用的if语句都有一个动作侦听器。 该部分代码将具有很可能具有导致最后一个combobox被选中的错误的逻辑。 ( else if是等等,也许应该是else声明)。

将其分开将是更多的OO并且将更长期灵活。

@ user650608你的问题对我来说不明确,你的意思是 – 走这条路,还是我错了?,

 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener { private static final long serialVersionUID = 1L; private JComboBox mainComboBox; private JComboBox subComboBox; private Hashtable subItems = new Hashtable(); public ComboBoxTwo() { String[] items = {"Select Item", "Color", "Shape", "Fruit", "Size"}; 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); getContentPane().add(mainComboBox, BorderLayout.WEST); subComboBox = new JComboBox();// Create sub combo box with multiple models subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 subComboBox.addItemListener(this); getContentPane().add(subComboBox, BorderLayout.CENTER); 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 Size", "Big", "Middle", "Small"}; subItems.put(items[4], subItems4); } @Override public void actionPerformed(ActionEvent e) { 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 e) { if (e.getStateChange() == ItemEvent.SELECTED) { if (e.getSource() == mainComboBox) { if (mainComboBox.getSelectedIndex() != 0) { FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this, mainComboBox.getSelectedItem().toString(), "Please wait, Searching for ..... "); } } } } private class FirstDialog extends JDialog { private static final long serialVersionUID = 1L; FirstDialog(final Frame parent, String winTitle, String msgString) { super(parent, winTitle); setModalityType(Dialog.ModalityType.APPLICATION_MODAL); JLabel myLabel = new JLabel(msgString); JButton bNext = new JButton("Stop Processes"); add(myLabel, BorderLayout.CENTER); add(bNext, BorderLayout.SOUTH); bNext.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { setVisible(false); } }); javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); t.setRepeats(false); t.start(); setLocationRelativeTo(parent); setSize(new Dimension(400, 100)); setVisible(true); } } public static void main(String[] args) { JFrame frame = new ComboBoxTwo(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } 

您是否尝试使用ItemListener?

该文档说,每次编辑combobox时都会触发ActionEvent。

此致,Stéphane