How to return String form actionPerformed?

I am trying to use buttons to control the actions performed in a window. To do this, I try to return the lines to give commands through the program. So far, I have managed to get this code to work:

@Override public void actionPerformed(ActionEvent e) { setCommand(e.getActionCommand()); } public void setCommand(String g){ command = g; getCommand(); } public String getCommand() { return command; } 

However, when I do this in another class, nothing happens:

 System.out.print(button.getCommand()); 

Am I doing something wrong or just using the wrong solution?

+4
source share
2 answers

As you can see, actionPerformed() has a return type of void ... and its method, which you overload from the ActionListener interface ... so you cannot return String from it.

+2
source

You cannot return something from the actionPerfomed() method, but instead, you can call other methods that will do everything you need to do this when you click this button.

If you want to notify other business logic that a certain button is pressed, I would suggest you use the Observer template.

+1
source

All Articles