I do not understand why this code does not work as expected. When the button is clicked, I expect it will set the JLabel to the text in the text field, wait 1 second, decrements the counter and then change the JLabel to that decremented value. When I run it and click the button it waits until the while loop condition is met, then just sets the JLabel to the final value.
I have tried to do it both in the action event explicitly and in the current form by calling the runTimer on click.
'''
package swinging;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PomGUI
{
public static void main(String[] args)
{
new pGUI();
}
}
class pGUI extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField tInput;
JLabel lHeader, lClock;
JButton bStart;
JButton bStop;
int remain;
public pGUI()
{
lHeader = new JLabel("Personal Pomodoro Timer v5.0");
tInput = new JTextField(5);
lClock = new JLabel("0:00");
bStart = new JButton("Start");
bStop = new JButton("Stop");
lHeader.setFont(new Font("Serif", Font.ITALIC, 24));
add(lHeader);
add(tInput);
add(lClock);
add(bStart);
add(bStop);
bStart.addActionListener(ae -> {
try {
runTimer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
setLayout(new FlowLayout());
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void runTimer() throws InterruptedException
{
remain = Integer.parseInt(tInput.getText());
lClock.setText(Integer.toString(remain));
while(remain > 0 )
{
TimeUnit.SECONDS.sleep(1);
remain--;
lClock.setText(Integer.toString(remain));
}
}
'''