Class of Event handlers - java

is it possible in java to have a class where it has EventHandlers for with different functions? for example button1 will log you in, while button2 will log you out, is this possible? Here's the code I made it seems to be not working.
package event.handlers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TheHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent logInEvent) {
System.out.println("Button Login");
}
public void actionPerformed(ActionEvent cancelEvent) {
System.out.println("Cancel Login");
}
}

You either need two implementations of ActionListener, one for each button or the actionPerformed needs to determine the button by the event argument and take the appropriate action. Your code will not compile because the signatures for both methods are the same.

No. You can not have a class implement two methods with the same function signature. How would the compiler know which one to call for different events? The name you give to the arguments has no meaning to the compiler.
As an alternative, you can create multiple anonymous action listeners that simply forward the call to a method that does have a unique name if you want everything to be in the same class.
public class TheHandler {
public TheHandler() {
JButton login, cancel;
//initialize code here
login.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent logInEvent) {
loginPerformed(logInEvent);
}
});
cancel.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent cancelEvent) {
cancelPerformed(cancelEvent);
}
});
}
public void loginPerformed(ActionEvent logInEvent) {
System.out.println("Button Login");
}
public void cancelPerformed(ActionEvent cancelEvent) {
System.out.println("Cancel Login");
}
}

You may use getSource() or getActionCommand() method of ActionEvent.
#Override
public void actionPerformed(ActionEvent logInEvent) {
Object src=logInEvent.getSource();
String cmd=logInEvent.getActionCommand(); //It will return caption of button
if(src==btn1)
{
//
}
//Or
if(cmd.equals("Button1")) { ... }
}

You can not have multiple actionPerformed method in one class. Simple way is to do operation based on source of action like:
(in actionPerformed method)
if(e.getSource() == loginButtton) { // based on button variable if they are in same class and accessible in actionPerformed method
loginMethod()
} else if(e.getSource == logoutButton) {
logoutMethod()
}
or
if(e.getActionCommand().equals("loginButtton")) { // based on caption/text on button
loginMethod()
} else if(e.getActionCommand().equals("logoutButtton")) {
logoutMethod()
}
or you can have different anonymous class for different buttons like
loginButton.addActionListner(new ActionListerner(){
public void actionPerformed(ActionEvent loginEvent) {
loginMethod();
}
});
logoutButton.addActionListner(new ActionListerner(){
public void actionPerformed(ActionEvent cancelEvent) {
logoutMethod();
}
});

The problem there is that your two method signatures are identical. When Java tries to figure out which method to call, it can't tell the difference between the two.
I can think of two ways to do what you want:
Presumably, you are registering the listeners on the buttons like cancelButton.addActionListener(...). So you can either provide each button with its own anonymous inner class:
loginButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent logInEvent) {
System.out.println("Button Login");
}
}
cancelButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent cancelEvent) {
System.out.println("Cancel Login");
}
}
or you can define a single actionPerformed method that checks the source of the call:
public class TheHandler implements ActionListener {
JButton loginButton;
JButton cancelButton;
public TheHandler()
{
...
// Now, technically, this is bad form because you're leaking 'this'.
// But as long as this will only be called after this constructor finishes
// initializing, it's safe.
loginButton.addActionListener(this);
cancelButton.addActionListener(this);
...
}
...
#Override
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == loginButton)
System.out.println("Button Login");
else if(evt.getSource() == cancelButton)
System.out.println("Cancel Login");
}
}
Using anonymous inner classes can sometimes be clearer, because you see the code right next to the addListener call, but it also adds a lot of boilerplate, and if you're working on a very large progect that can take a while to load, reducing the number of classes can sometimes make it load a little faster (each anonymous inner class is another thing for the JVM to load).

Related

Button actionlistener calls another action listener in the same class

I have a submit button (and an action listener) which checks if the number inserted into a Futoshiki puzzle is legal (checking a 2D array for duplications etc)
In another method I have the actual grid with an action listener that gets the numbers and inserts them into the 2D array.
JButton acction listener
JButton isRight = new JButton("Check My Answer");
isRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (!(puzzle.isLegal())) {
JOptionPane.showMessageDialog(FutoshikiFrame.this,
puzzle.getProblems(),
"You made a mistake!",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(FutoshikiFrame.this,
"YOU WIN!",
"YES THATS FINE",
JOptionPane.INFORMATION_MESSAGE);
}
puzzle.printProblems.clear();
}
});
Grid action listener
public void keyReleased(KeyEvent e) {
String getInsertedValue = Emptysquare.getText();
int getInsertedIntValue = Integer.parseInt(getInsertedValue);
setSquareValue(r, c, getInsertedIntValue);
System.out.print(getSquareValue(r, c));
}
Is there a way I can access the keyReleased action listener from the JButton so it basically "submits" the contents of the grid and then checks if its legal?
Sure, just either keep a reference to that action listener and call the keyReleased method with a null value, or refactor out the content of keyReleased to an own method and call this method from both listeners.
Although 2 other methods were given, here is an additional way you could go about it, but I'm not sure which would be best to use.
public class YourClass {
JButton isRight;
public YourClass() {
this.isRight = new JButton();
this.isRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
YourClass.this.isRight.getKeyListeners()[0].keyReleased(null);
//Other action related code
}
});
isRight.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
//Key related code
}
//Other required key listener methods
});
}
}

Creating a class that extends keyListener

In my java project, I want to check the input in each JTextField in a few different classes (with the exact same code)..
Right now I have the same code copied over and over and I was suggested with 2 options:
Create a method and call the method instead.
Create a new class that extends from another class (I don't know which yet) that has the method needed.
The code I'm using now is:
totalAmount.addKeyListener(new KeyAdapter() {
#Override
public void keyTyped(KeyEvent arg0) {
//do something
}
});
And the new class is:
public class Listener extends KeyAdapter {
public void keyTyped(KeyEvent arg0){
//do something
}
}
The problem is that I don't know if I'm extending the right class, and how to use the new class I've written...
Thanks in advance!
To do what you are wanting with your key adapter you would use
totalAmount.addKeyListener(new Listener());
and your code of your key adapter is correct.
public class Listener extends KeyAdapter {
public void keyTyped(KeyEvent arg0){
//do something
}
}
To get the text from a JTextField you could either use this code inside your keyAdapter
System.out.println(totalAmount);
or, preferably you could use a document listener. This would be done by
public class documentListener implements DocumentListener //This is a listener
{
public void changedUpdate(DocumentEvent e){
}
public void removeUpdate(DocumentEvent e){
int lengthMe = e.getDocument().getLength();
System.out.println(e.getDocument().getText(0,lengthMe));
}
public void insertUpdate(DocumentEvent e){
int lengthMe = e.getDocument().getLength();
System.out.println(e.getDocument().getText(0,lengthMe));
}
}
and it would be added to the JTextField with
totalAmount.getDocument().addDocumentListener(new documentListener());

Running a "this" Method inside actionPerformed - JButton?

I have a JButton which I added a actionPerformed, and I tried to write a "this" method and it won't allow it. How can I do this? This is example of what I want to do:
public void methodName(String results) {
this.results = results;
}
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
this.methodName(asdf);
}
Because it's an anonymous class, using this will refer to the anonymous class instance, not your overall class. To get around this, denote that you want to reference your outer class specifically:
Something some = new Something() {
public void overridden() {
YourClass.this.methodName("test");
}
};
Your class in anonymous, so in anonymous context, this does not make any sense. What do you mean by this? If you mean the button, your answer is event.getSource()
In your code, this refers to your ActionListener when you call the method.
If you want to call methodName() from the enclosing class, you have two choices :
remove this:
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
methodName(asdf);
}
store a reference to the enclosing class and use it:
final MyClass enclosingClass = this;
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
enclosingClass.methodName(asdf);
}
You can not use "this" keyword inside inner class to access outer class method. if we use this then it will refer to the inner class.Instead of that just use the method name.see the example.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class TestButton
{
String results = "";
JButton Button = new JButton();
public TestButton(){
Button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
methodName("Test");
this.show();
}
public void show(){
System.out.println("hi");
}
});
}
public void methodName(String results)
{
this.results = results;
}
}

Implementing an inner anonymous action listener

I've been trying to implement this action listener as stated above but keep receiving two errors:
-Cannot instantiate the type ActionListener
-void is an invalid type for the variable incrementAction
I've been looking up similar examples but they all seem to point to the same method of implementing it.
This is where I've got to.
increment.addActionListener(new ActionListener());{
public void incrementAction(ActionEvent e){
this.incrementCount();
this.setTextField();
}
}
The signature of the ActionListener method is:
public void actionPerformed(ActionEvent e)
JButton increment = new JButton();
increment.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent received! ");
}
});

Special method should be called after any AbstractAction-execution

Please excuse the vague question title, but usually I don't do such kind of stuff. So I have the following problem:
I'm designing a popupmenu for a specific app where each menu item is associated with a certain action:
public class CanvasMenu extends JPopupMenu {
public CanvasMenu(){
this.add(new AbstractAction("Do some operation") {
#Override
public void actionPerformed(ActionEvent arg0) {
doSomeStuff1();
cleanup(); // has to be done after every menu operation
}
});
this.add(new AbstractAction("Other operation") {
#Override
public void actionPerformed(ActionEvent e) {
doSomeOtherStuff();
cleanup(); // has to be done after every menu operation
}
});
}
}
I read somewhere that AbstractAction is used for such tasks where you want to add menu items and associate them with some action. In reality, I want not only two such actions, but some dozen of them.
The problem is the cleanup thing. cleanup should be after any of these actions has been chosen. This means, if I continue in the abovely described manner, I will have to write cleanup() for each AbstractAction.
Is there any (easy/elegant/nice) way or pattern to avoid writing cleanup() over and over again? I.e. is it possible to desing something that will only get the action and after executing it automatically call cleanup?
This is one of the patterns:
abstract class ActionWithCleanup extend AbstractAction {
#Override
public final void actionPerformed(ActionEvent arg0) {
myAction();
cleanup(); // has to be done after every menu operation
}
public abstract void myAction();
}
...
this.add(new ActionWithCleanup("Do some operation") {
#Override
public void myAction() {
doSomeStuff1();
}
});

Categories