JComboBox 动作侦听器
我有这个问题。 我有多个 JComboBox(总共 5 个)。
对于每个组合框,我添加一个 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.
但问题是当我在任何组合框中更改某些内容时操作始终由我附加操作列表的最后一个组合框触发。
有人知道吗?
然后我切换到 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 方法中更改了哪个组合框项目 来自创建的最后一个组合框的
createCombo 在运行时被调用,所以我有可变数量的组合框。
I'm having this problem.
I have multiple JComboBoxes (5 total).
To each comboBox I add an ActionListener, but the same ActionListener for all of them, called:
ComboBoxActionPerformed(java.awt.event.ActionEvent e)
and when that action is performed I look at the event (e) and do:
JComboBox c = ((JComboBox)e.getSource());
//DO WORK relating to c as thats the combobox that triggered.
but the problem is when I change something in any of my comboboxes the Action is always triggered by the last combo box to which I am attaching the actionlistner.
anyone have any idea?
I then switched to ItemListner.
This is what im doing a
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;
}
and createCombo gets called multiple times
but regardless of which combo box item was changed in side my ItemStateChanged method its
comming from the last combo box that was created
createCombo is called at runtime, so i have a variable number of comboboxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加单独的操作侦听器,而不是让一个操作侦听器为每个调用运行 if 语句。该代码部分的逻辑很可能存在导致选择最后一个组合框的错误。 (也许一个
else
语句应该是else if
等)。将其分离出来会更加面向对象,并且从长远来看会更加灵活。
Add separate action listeners instead of having one action listener run through if statements for each call. That section of the code will have logic that most likely has a bug that is causing the last combo box to be selected. (Maybe an
else
statement that should beelse if
, etc.).Separating it out will be more OO and will be more flexible long term.
@user650608 你的问题对我来说不清楚,你的意思是 - 走这条路,还是我错了?
@user650608 your questions isn't clear for me, do you mean - going this way, or am I wrong ?,
您是否尝试使用 ItemListener 来代替?
doc 说每次编辑组合框时都会触发 ActionEvent。
问候,
史蒂芬
Did you try using an ItemListener instead ?
The doc says an ActionEvent is fired every time the combo box is edited.
Regards,
Stéphane