Reputation: 73
This program was written to count from 0 to 1000 but it just goes straight to 1000 without displaying the counting process. I have written similar code using a progress bar and Thread.sleep()
method and it works perfectly.
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class project extends JFrame implements ActionListener {
JButton CountUpButton = new JButton("Count up");
JButton CountDownButton = new JButton("Count Down");
JButton ResetButton = new JButton("Reset");
JTextField NumberField = new JTextField();
int count = 0;
public project(){
setLayout(new GridLayout(1, 4));
setSize(500, 300);
add(NumberField);
add(CountUpButton);
add(CountDownButton);
add(ResetButton);
CountUpButton.addActionListener(this);
CountDownButton.addActionListener(this);
ResetButton.addActionListener(this);
NumberField.setText("0");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent a){
if(CountUpButton.hasFocus()){
count = Integer.parseInt(NumberField.getText());
try{
while(count < 1000){
count = count + 1;
NumberField.setText(Integer.toString(count));
Thread.sleep(100);
}
}catch(InterruptedException r){
r.printStackTrace();
}
}
if(CountDownButton.hasFocus()){
count = Integer.parseInt(NumberField.getText());
try{
while(count > 0){
count -= 1;
NumberField.setText(Integer.toBinaryString(count));
Thread.sleep(100);
}
}catch(InterruptedException r){
r.printStackTrace();
}
}
if(ResetButton.hasFocus()){
NumberField.setText("0");
}
}
public static void main(String args[]){
new project();
}
}
Upvotes: 0
Views: 120
Reputation: 340230
Any long-running task should be run in a separate thread. Your use of Thread.sleep certainly qualifies as long-running.
By running in the Swing user-interface thread, no updates can be rendered in that user-interface until your code completes. Instead your counting should be spawned in another thread. That other thread should periodically update the user interface in a thread-safe manner using the SwingWorker.
Study up on launching threads, executors such as a ScheduledExecutorService
, the Swing event-dispatch thread (EDT), and SwingWorker.
A simpler approach might be the Swing Timer class (Tutorial), not to be confused with java.util.Timer. It will do much of the thread-handling work for you. But I have no experience with it.
Upvotes: 3