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){
} }
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()
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;
}
Related
I am trying to make a game and I made a Button class using the JButton library.
This code is in the button class:
#Override public void mouseClicked(MouseEvent e) { pressed = true; }
public boolean getPressed(){
return pressed;
}
Then in the main java file:
Button playButton = new Button("PLAY", frame);
frame.add(playButton);
while (true){
if (playButton.getPressed())
System.out.println("test");
}
It never seems to print test even though I checked and the boolean value is being changed
I am unfamiliar with JButton. But it seems that in your if statement in your while loop, there is no condition. Please reply if this helps!
In the below code, I have a label named card with a mouse click event. I only want the click event to implement once. Meaning it will implement the first time I click the label, but not the following times. How do I do this? I imagine I must disable its Listener.
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
i++;
card.setText(cardB[i]);
}
I think we all would do the same.
It's really simple. Just declare a boolean then change its status when you click the first time.
boolean labelClicked = false;
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
if(!labelClicked){
i++;
card.setText(cardB[i]);
labelClicked=true;
}
else{
//doNothing
}
}
I am making a game on Java and i am doing a character selection menu. Within that menu i have the characters and if the user clicks on certain character a JOptionPane.showMessageDialog appears shows the stats of the character. So my question is if the person clicks "ok" which is automatically created when using the function how to i get that to select the character?
JButton chuck = new JButton(new ImageIcon("8bitChuckNorris.jpg"));//this part of program runs this if user picks lizard
chuck.setSize(210,175); //sets size of button
chuck.setLocation(300,317); //sets location of button
chuck.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(null, "\t\tSTATS\nAttack\ndefence\nspecial");
}
});
The simplest method would probably be to provide a method that the ActionListener can call after the
the option pane is closed, for example...
chuck.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(null, "\t\t STATS\nAttack\ndefence\nspecial");
characterSelected("chuck"); // Pass whatever you need to identify the character
}
});
Updated
It would be easier to use JOptionPane.showConfirmDialog as it will actually return a int result representing the option that the user selected (or -1 if they dismissed the dialog without selecting anything)
switch (JOptionPane.showConfirmDialog(null, "\t\t STATS\nAttack\ndefence\nspecial", "Character", JOptionPane.OK_CANCEL_OPTION)) {
case JOptionPane.OK:
// Process selection...
break;
}
For a while I have been trying to figure this out, searching online, finally decided to join just to figure this out. So my dilemma is this.
addActionListner(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//some code
}
});
What goes in some code that could actually pull from a JTextField (and for arguments sake let us call it myText)? Like how could I, if possible, call the ActionListner? so has when the button is clicked it actually performs the actionPeformed of the ActionListener in myText?
Add the action listener to the button and then retrieve myText's contents in the button's action listener. Hope it helps.
Assuming your button is named something like "myButton":
String someString = "";
myButton.addActionListner(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//this will give you myText's contents. Do what you will with them.
someString = myText.getText();
}
});
Now you should be able to do whatever you want with the contents.
I have created chess board using JButton and for pieces I used ASCII values.
I added ActionListener to all the buttons. When i clicked source(first) button actionPerformed event is called and i stored the value of the button in the variable.
But the problem is when i clicked the destination button actionPerformed event is called and value is replaced with source button value.
I want source button value and destination button value in different variables. How it is possible?
public void actionPerformed(ActionEvent ae)
{
JButton o = (JButton) ae.getSource();
value = o.getText();
}
Then you need 2 variables to store the two values and a third variable to tell you which click is happening.
if(isSource){
source = o.getText();
isSource = false;
}else{
destination = o.getText();
isSource = true;
}
This way, on the first click you know the value of the source, and on teh second click you know the value of the destination. Then on the next click it is a source again etc.
You have to manage the state of your application in some way. At the first click, the state of the application is that it waits for information of the source of the move. Then it waits for information of the destination of the move.
You could create an enum in your class like this:
enum ActionState {
SOURCE,
DESTINATION
}
And store the action state and the button variables as members of the class:
ActionState state = SOURCE;
String source;
String destination;
Then actionPerformed would be like this:
public void actionPerformed(ActionEvent ae)
{
JButton o = (JButton) ae.getSource();
String value = o.getText();
if (state == SOURCE){
source = value;
state = DESTINATION;
}
else if (state == DESTINATION){
destination = value;
state = SOURCE;
// you probably want to call something here to perform the move itself.
}
}
Instead of an enum, you could just use a boolean to represent your state. But using an enum allows for more extensibility if you need other states in the future.