I've tried to write a litte java-program that should work as a timer.
The problem is (probably) that I need to use Thread.sleep(), which throws InterruptedException. So if I start the program, I am able to enter the seconds an minutes, but when pressing "OK", nothing happens (it also doesn't show any warnings or errors in the terminal).
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
public class Timer {
public static void main(String[] args) throws InterruptedException {
SwingUtilities.invokeLater(Timer::new);
}
private JButton button;
private JTextField minutes;
private JTextField seconds;
Timer() {
JFrame frame = new JFrame();
frame.setSize(300, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JPanel subpanel1 = new JPanel(new GridLayout(1, 2));
/*
* The following lines ensure that the user can
* only enter numbers.
*/
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setAllowsInvalid(false);
formatter.setCommitsOnValidEdit(true);
minutes = new JFormattedTextField(formatter);
seconds = new JFormattedTextField(formatter);
minutes.setText("0");
seconds.setText("0");
JPanel subpanel2 = new JPanel();
/*
* When the user presses the OK-button, the program will
* start to count down.
*/
button = new JButton("OK");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
timer();
}
catch(InterruptedException e) {
}
}
});
JButton button2 = new JButton("Reset");
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
minutes.setText("0");
seconds.setText("0");
button.setEnabled(true);
}
});
subpanel1.add(minutes);
subpanel1.add(seconds);
subpanel2.add(button);
subpanel2.add(button2);
panel.add(subpanel1, BorderLayout.CENTER);
panel.add(subpanel2, BorderLayout.PAGE_END);
frame.add(panel);
frame.setVisible(true);
}
//Method for the timer
void timer() throws InterruptedException {
int min = Integer.valueOf(minutes.getText());
int sec = Integer.valueOf(seconds.getText());
button.setEnabled(false);
while(min > 0 || sec > 0) {
Thread.sleep(1000);
if(sec >= 1) {
sec = sec - 1;
seconds.setText(String.valueOf(sec));
}
else if(sec == 0 || min > 0) {
sec = 59;
min = min - 1;
seconds.setText(String.valueOf(sec));
minutes.setText(String.valueOf(min));
}
}
}
}
What can I do to make it work?
I know replaced Thread.sleep() with the following:
Timer timer = new Timer(delay, taskPerformer);
timer.setRepeats( false );
timer.start();
Now an error occurs: The constructor cannot applied to given types (it wants no arguments). What did I do wrong? (The variables delay and taskPerformer are defined, and I did not import java.util.Timer)
Thread.sleepis not permiited in Swing. Use Timer instead. Please also read about concurrency in swing.Timer).Thread.sleep(…). The Timer is only created once. A Timer will only fire an event at your specified time interval. In the ActionListener you then do something. So in your case in the ActionListener for the button you would set a variable to contain the total seconds for the minutes/seconds you want the Timer to run and update the JLabels to contain the minutes and seconds left.. You would then set the Timer to fire in 1000ms (i.e. one second)