I\'m trying to figure out what i am doing wrong with action listeners. I\'m following multiple tutorials and yet netbeans and eclipse are giving me errors when im trying to
First, exend JFrame properly with a super() and a constructor then add actionlisteners to the frame and add the buttons.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Calc extends JFrame implements ActionListener {
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
public Calc()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
button1.addActionListener(this);
button2.addActionListener(this);
calcFrame.add(button1);
calcFrame.add(button2);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button1)
{
\\button1 code here
} else if(source == button2)
{
\\button2 code here
}
}
public static void main(String[] args)
{
JFrame calcFrame = new JFrame();
calcFrame.setVisible(true);
}
}
I use "e.getActionCommand().contains(CharSecuence s)", since I´m coming from an MVC context, and the Button is declared in the View class, but the actionPerformed call occurs in the controller.
public View() {
....
buttonPlus = new Button("+");
buttonMinus = new Button("-");
....
}
public void addController(ActionListener controller) {
buttonPlus.addActionListener(controller);
buttonMinus.addActionListener(controller);
}
My controller class implements ActionListener, and so, when overriding actionPerformed:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().contains("+")) {
//do some action on the model
} else if (e.getActionCommand().contains("-")) {
//do some other action on the model
}
}
I hope this other answer is also useful.
The problem is that button1 is a local variable. You could do it by just change the way you add the actionListener.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//button is pressed
System.out.println("You clicked the button");
}});
Or you make button1
a global variable.
There is no this
pointer in a static method. (I don't believe this code will even compile.)
You shouldn't be doing these things in a static method like main()
; set things up in a constructor. I didn't compile or run this to see if it actually works, but give it a try.
public class Calc extends JFrame implements ActionListener {
private Button button1;
public Calc()
{
super();
this.setSize(100, 100);
this.setVisible(true);
this.button1 = new JButton("1");
this.button1.addActionListener(this);
this.add(button1);
}
public static void main(String[] args) {
Calc calc = new Calc();
calc.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1)
}
}
It's a pretty old question. I suppose nobody cares about it any more. But I'd like to post my approach in case it would be helpful to someone.
Using my approach, you can write the button click event handler in the 'classical way', just like how you did it in VB or MFC ;)
Suppose we have a class for a frame window which contains 2 buttons:
class MainWindow {
Jbutton searchButton;
Jbutton filterButton;
}
You can use my 'router' class to route the event back to your MainWindow class:
class MainWindow {
JButton searchButton;
Jbutton filterButton;
ButtonClickRouter buttonRouter = new ButtonClickRouter(this);
void initWindowContent() {
// create your components here...
// setup button listeners
searchButton.addActionListener(buttonRouter);
filterButton.addActionListener(buttonRouter);
}
void on_searchButton() {
// TODO your handler goes here...
}
void on_filterButton() {
// TODO your handler goes here...
}
}
Do you like it? :)
If you like this way and hate the Java's anonymous subclass way, then you are as old as I am. The problem of 'addActionListener(new ActionListener {...})' is that it squeezes all button handlers into one outer method which makes the programme look wired. (in case you have a number of buttons in one window)
Finally, the router class is at below. You can copy it into your programme without the need for any update.
Just one thing to mention: the button fields and the event handler methods must be accessible to this router class! To simply put, if you copy this router class in the same package of your programme, your button fields and methods must be package-accessible. Otherwise, they must be public.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ButtonClickRouter implements ActionListener {
private Object target;
ButtonClickRouter(Object target) {
this.target = target;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
// get source button
Object sourceButton = actionEvent.getSource();
// find the corresponding field of the button in the host class
Field fieldOfSourceButton = null;
for (Field field : target.getClass().getDeclaredFields()) {
try {
if (field.get(target).equals(sourceButton)) {
fieldOfSourceButton = field;
break;
}
} catch (IllegalAccessException e) {
}
}
if (fieldOfSourceButton == null)
return;
// make the expected method name for the source button
// rule: suppose the button field is 'searchButton', then the method
// is expected to be 'void on_searchButton()'
String methodName = "on_" + fieldOfSourceButton.getName();
// find such a method
Method expectedHanderMethod = null;
for (Method method : target.getClass().getDeclaredMethods()) {
if (method.getName().equals(methodName)) {
expectedHanderMethod = method;
break;
}
}
if (expectedHanderMethod == null)
return;
// fire
try {
expectedHanderMethod.invoke(target);
} catch (IllegalAccessException | InvocationTargetException e) { }
}
}
I'm a beginner in Java (not in programming), so maybe there are anything inappropriate in the above code. Review it before using it, please.
The first problem is that button1
is a local variable of the main
method, so the actionPerformed
method doesn't have access to it.
The second problem is that the ActionListener
interface is implemented by the class calc
, but no instance of this class is created in the main
method.
The usual way to do what you want is to create an instance of calc
and make button1
a field of the calc
class.