I'm looking for a way to pass a variable or string or anything into an anonymous actionlistener ( or explicit actionlistener ) for a JButton. Here is what I have:
public class Tool {
...
public static void addDialog() {
JButton addButton = new JButton( "Add" );
JTextField entry = new JTextField( "Entry Text", 20 );
...
addButton.addActionListener( new ActionListener( ) {
public void actionPerformed( ActionEvent e )
{
System.out.println( entry.getText() );
}
});
...
}
}
Right now I just declare entry
to be a global variable, but I hate that way of making this work. Is there a better alternative?
ActionListener
interface.JTextField
argument.class Foo implements ActionListener{
private final JTextField textField;
Foo(final JTextField textField){
super();
this.textField = textField;
}
.
.
.
}
Problem?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With