Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control a loop using a button?

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.

like image 294
Daniel Avatar asked Aug 01 '26 11:08

Daniel


2 Answers

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++;
 } 
like image 162
Paul Fournel Avatar answered Aug 03 '26 02:08

Paul Fournel


You could have the loop wait for the button click, and then once it loops 10 times break the loop.

like image 29
Jacqlyn Avatar answered Aug 03 '26 00:08

Jacqlyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!