fartagaintuxedo
fartagaintuxedo

Reputation: 749

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 ]

I have a series of JTextField and i need to do something when their values are updated. Usually i need to do myAction(JTextField jt) with all of them.

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.

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.

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.

Upvotes: 1

Views: 455

Answers (3)

wero
wero

Reputation: 33000

First create a generic ActionListener which extracts the JTextField on which the action occurred from the event:

ActionListener listener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        myAction((JTextField)e.getSource());
    }
}

or shorter as 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:

myJTextField1.setName("param1");

and extract it in the action method:

    public void actionPerformed(ActionEvent e) {
        JTextField tf = (JTextField)e.getSource();
        myAction(tf, tf.getName());
    }

of course this could also be done in myAction.

Upvotes: 3

Yazan
Yazan

Reputation: 6082

Make your class 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);
    }
}

Upvotes: 1

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

FYI: Even if answer of @wero is very good, you can change your method to get it worked.

// 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);
        }
    });
}

Upvotes: 2

Related Questions