I am a beginner in java, I want to know is there any possible way to control a loop by clicking a button? I am creating a GUI, and it's supposed to run 10 times in the loop. Is there a way that I could have a button on the screen so that when the user presses, then it goes to the next iteration? Because currently everything just runs and executes once.
In your java class, you should define an attribute and each time you click on the button you add 1 to this attribute and do the action.
define an attribute in your class;
public int i = 0;
and create a button to be clicked on:
private void clickMeButtonActionPerformed(java.awt.event.ActionEvent evt) {
// code your action here:
this.i++;
}
You could have the loop wait for the button click, and then once it loops 10 times break the loop.
You can use javaFX it's gonna replace javaswing very soon anyways plus it's cooler.
import javafx.scene.control.button
Button button = new Button("control");
int i = 0;
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
i++;
label.setText("i increased");
}
});
Related
Hi so I'm making a Simongame and I need to wait for the player to push a series of button (sending integers to a list) and compare it to another list, but I didn't find any wait for event type of function. So how can I make my game loop wait for the player to push a certain number of button before trying to compare it?
start.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Game = 1;
While(Game == 1)
//Game adding random values to a list
//4 Buttons Action event adding values to another list with 4 different values and a button to validate the values put into the list
//HERE I need the loop to wait for buttons to be pushed and validated by another button before trying to compare the two list
//Comparing the two lists , printing a message if they are not the same or returning in the loop and add a new value to the randomly generated list
}
}
});
You don't. I'm serious, JavaFX is built around event handling.
What you're trying to do is poll for data, but you don't need to. You can add a Click event handler using
myButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
//TODO all your events and stuff here
}
});
Inside of the handler for the ActionEvent, you can use code to do something. There is another way of handling events for buttons as well, if you want to differentiate between right-clicks and left-clicks, dragging the mouse over or out, etc. This is through the myButton.addEventHandler(EventType.EVENT), myEventHandler);.
This question already has an answer here:
How would I programmatically click a button in JavaFX from another method?
(1 answer)
Closed 5 years ago.
I have a data entry form with 3 buttons: "Save/Next", "Clear" and "Exit". I want the Save/Next button to perform its own function, then finish by setting the focus to the Clear button and causing it to execute, reset all the text fields to be ready for the next set of data. I was hoping to avoid copying all the clearing code lines into the event handler for the Save button.
Is there an easy way to programmatically create the event that the Clear button eventHandler recognizes (ENTER key in this case), thus executing without user input?
btnClear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
barcodeText1.clear();
barcodeText2.clear();
//
// more clearing statements here...
//
statusContent.setText(stStatusTextScan1);
barcodeText1.requestFocus(); //return focus to first field
}// end handle
});//end btnClear
// Save Button Event Handler ..............................................
btnSave.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//
// Do some Saving stuff...
//
// Record saved. Now reset the form for the next record...
btnClear.requestFocus();
// Put something here that will mimick pressing ENTER key...
Event e = new Event(<KeyEvent>);
}// end handle
});// end btnSave
Berger: Good find. btnClear.fire() in your link worked like a charm for me. There is an art to finding the best search term combinations which I have not yet mastered. Thanks.
I know this sounds very basic, but I never really learned how to do this.
I know about for loops where you can just use for example:
for(int i=0; i<=10; i++)
System.out.println(i);
But that just prints out numbers from 0 to 10 with a gap of 1...which isn't what I'm looking for.
I'm looking for some code where the program starts at a value and adds 1 (or another number) whenever the a button is clicked.
I already have the code for the button and everything, but I have an empty ActionListener as I don't know what to place inside it.
In the ActionListener actionPerformed method write the following code:
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button_name)
{
count_variable += 1;
}
}
Just add something like this.
int counter = 0;
JButton button = new JButton(" Click me ");
//Add action listener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
counter++;
}
});
This will make counter++ execute whenever the button is pressed. If you want another number, say 10, then just replace counter++ with counter+=10
**disclaimer**
Make sure that counter is accessible inside of ActionPerformed. You can do this by making it a field variable of an encapsulating class, making it a mutable object, and many other ways.
I am trying to create a swing app that is a quiz. I need the jLabel to change on a button click but when I click the button, the app locks up. Can someone point me in the right direction?
My button click code is below:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String[] questions = {"test0","test1","test2","test3","test4","test5","test6"};
String[] answers = {"","","","","","",""};
int i = 0;
do {
jLabel2.setText(questions[i]);
index.setText(String.valueOf(i));
if (txtAnswer.getText().toLowerCase().equals(answers[i].toLowerCase())) {
i++;
jLabel2.setText(questions[i]);
}
else {
add(lblWrong);
}
}
while(i < 7);
}
I am getting a warning that the evt parameter has not been used, could this be a problem?
Thank you
You don't want the do while loop. It's trapping you in the button press method as if you get an answer wrong you keep entering the else and can't leave it, stopping the app from working. Replace it with an if statement checking if i < 7.
In the else condition of your loop, you don't add 1 to i at all - therefore you can potentially end up in the situation where it's never incremented, thus it will be an infinite loop (locking your program up.)
I'm new in Java swing, and I have a problem. I made for-loop for creating buttons and now I want automatically give them names or some kind of marks for future recognition (I will need name of clicked button to make it a variable).
How can I give them names in my loop? Thank you.
Here is code of my for-loop:
for (int aa=1; aa<65; aa++)
{
JButton button = new SquareButton("");
gui.add(button);
button.addActionListener((ActionListener) button);
}
I will need name of clicked button to make it a variable).
You don't need a variable to work with the clicked button. Instead you get a reference to the button that was clicked from the ActionListener code:
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
// do processing on the clicked button.
}