Reputation: 83
i had two classes, one is Main.java another is Button.java
So in Main.java, i need to confirm while Jbutton in Button.java is clicked, then Main.java will do something
in Button.java
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Jsend)
{
String userinput = Jusertxt.getText();
setUserText(userinput);//setUserText is a method for Main.java can get a String from user input.
Jusertxt.setText("");
}
}
what is the statement i need to write in Main.java to check button is clicked in Button.java?
here is some code in Main.java
while(true){
if(//in here i need to ensure button is clicked){
} }
Upvotes: 0
Views: 1108
Reputation: 2849
To receive notification button is pressed, you must register listener in Main.java. With it you can synchronize a local variable to store the status of the button (if it is clicked). This variable can use later to determine whether the button is pressed (once).
Edit Another option is the variable part of Button.java and to implement a public method isClicked()
Upvotes: 1
Reputation: 187
Put a boolean value within the button, use a getter to retrieve the value of the boolean e.g
Example :
//Within main
Button button = new Button(); // I wouldn't recommend using button as a
class name by the way, will get very confusing
if(button.getPressed){
// do stuff
}
Your button code:
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Jsend)
{
String userinput = Jusertxt.getText();
setUserText(userinput);//setUserText is a method for Main.java can get a String from user input.
Jusertxt.setText("");
isPressed = true;
}
}
public boolean getPressed(){
return isPressed;
}
Upvotes: 1