KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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: 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 < 99999999;i++) { try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e1) { System.out.println("ERROR"); } counter = i; uselesButton.setText(String.format("Pressed %d times", counter)); } } });
Tags (comma-separated)
Save Edits
Cancel