I have a gui class which has three different JButtons
linked to one ActionListener
.
In the ActionListener
class I want an interaction between these three JButtons
in a way that allows the program to "remember" which button was clicked previously.
For example:
In other words if button1 click, now inside the if clause check for button2 click.
Right now getActionCommand()
does not allow me to use nested if statements because it only checks the current button clicked and has no "memory".
public void actionPerformed(ActionEvent e){
if (Integer.parseInt(e.getActionCommand()) == 0){
System.out.println("This is Jug 0");
// wont be able to print the following
if (Integer.parseInt(e.getActionCommand()) == 1){
System.out.println("This is Jug 1 AFTER jug 0"); //I will never be able to print this
}
}
}
From my understanding of what you are looking todo...
So one way of achieving this is to create a instance variable that is a Boolean, so will be set true is the button has been previously clicked and then you could check inside your method if that flag has been set true. Drawback of that approach would be that the flag will always be true once it has been clicked once. You'll have to implement a way to reset this flag.
private boolean hasButtonOneBeenPressed = false; //set this to true when the button has been clicked
public void actionPerformed(ActionEvent e){
if (Integer.parseInt(e.getActionCommand()) == 0 && !hasButtonOneBeenPressed){
System.out.println("This is Jug 0");
// wont be able to print the following
} else if(Integer.parseInt(e.getActionCommand()) == 1 && hasButtonOneBeenPressed){
System.out.println("This is Jug 1 AFTER jug 0");
} else {
//do something else
}
}