Note that there are some explanatory texts on larger screens.

plurals
  1. POButton ActionListener
    primarykey
    data
    text
    <p>Ok, so I made a simple program that adds the value to counter each time a button is clicked. Now, I would like to add "Auto" button feature to increase the value of the counter when the "Auto" button is clicked. I'm having problems with it because it won't render each counter value on the screen, instead the value updates when the loop is done.. Here is my code:</p> <pre><code>import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.concurrent.TimeUnit; import javax.swing.JButton; import javax.swing.JFrame; public class Gui extends JFrame{ private static final long serialVersionUID = 1L; private JButton uselesButton; private JButton autoButton; private FlowLayout layout; private long counter = 0; public Gui() { super("Button"); layout = new FlowLayout(FlowLayout.CENTER); this.setLayout(layout); uselesButton = new JButton(String.format("Pressed %d times", counter)); add(uselesButton); uselesButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { counter++; uselesButton.setText(String.format("Pressed %d times", counter)); } }); autoButton = new JButton("Auto"); add(autoButton); autoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for(long i =0; i &lt; 99999999;i++) { try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e1) { System.out.println("ERROR"); } counter = i; uselesButton.setText(String.format("Pressed %d times", counter)); } } }); } } </code></pre> <p>Keep in mind that I'm a beginner... All help appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload