[英]Add a generic ActionListener to many JButtons or JComponents or JTextFields
[ First of all i have read this answer but i understand mine is different Add actionListener to a lot of JButton ] [首先,我已经阅读了这个答案,但是我理解我的是不同的。 将actionListener添加到很多JButton中 ]
I have a series of JTextField
and i need to do something when their values are updated. 我有一系列的JTextField
并且在更新其值时需要做一些事情。 Usually i need to do myAction(JTextField jt)
with all of them. 通常我需要对所有这些都做myAction(JTextField jt)
。
Right now i use this code to do it, but i have to make an instance of this code for each one of my JTextField
and i want to do it only once. 现在,我使用此代码来执行此操作,但是我必须为我的每个JTextField
编写此代码的实例,并且我只想执行一次。
myJTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//change some value here!
myAction(myJTextField1);
}
});
//repeat for myJTextFields-2-to-9
This what i have tried but it doesn't work because jt
is not accessible. 这是我尝试过的方法,但由于无法访问jt
而无法使用。
void addListener(JTextField jt){
jt.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
//change some value here!
myAction(jt);
}
});
}
I also tried something like jt.addActionListener(new myListener implements ActionListener (jt){
but i don't really know how to something like that. 我也尝试过类似jt.addActionListener(new myListener implements ActionListener (jt){
但我真的不知道该怎么做。
First create a generic ActionListener
which extracts the JTextField
on which the action occurred from the event: 首先创建一个通用的ActionListener
,从事件中提取发生了动作的JTextField
:
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
myAction((JTextField)e.getSource());
}
}
or shorter as lambda 或更短为lambda
ActionListener listener = e -> myAction((JTextField)e.getSource());
and then register it on your textfields: 然后在您的文本字段中注册:
myJTextField1.addActionListener(listener);
myJTextField2.addActionListener(listener);
...
UPDATE: 更新:
If you want to associate each textfield with a string parameter and pass the parameter to your myAction
method, you can transport the parameter in the name field: 如果要将每个文本字段与一个字符串参数相关联,并将该参数传递给myAction
方法,则可以在名称字段中传输该参数:
myJTextField1.setName("param1");
and extract it in the action method: 并将其提取为action方法:
public void actionPerformed(ActionEvent e) {
JTextField tf = (JTextField)e.getSource();
myAction(tf, tf.getName());
}
of course this could also be done in myAction
. 当然,这也可以在myAction
完成。
FYI: Even if answer of @wero is very good, you can change your method to get it worked. 仅供参考:即使@wero的答案非常好,您也可以更改方法以使其生效。
// to be accessible form an anonymous class variable must be declared as final!
void addListener(final JTextField jt){
jt.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
//change some value here!
myAction(jt);
}
});
}
Make your class implements ActionListener
使您的类implements ActionListener
public class myClass extends abcd implements ActionListener {
public void someMethod(){
myJTextField1.addActionListener(this);
myJTextField2.addActionListener(this);
myJTextField3.addActionListener(this);//add more ...
}
@override
public void actionPerformed(ActionEvent event) {
JTextField target = (JTextField)event.getSource();
myAction(target);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.