title: How can I get this code for a GUI Countdown Timer to work properly?
body: <p>I'm trying to custom write a program for a GUI Countdown Timer (for a personal project), based on some other codes that I've found on this site. </p>
  
  <p>The idea is that a user will be able to select the total time (variable <code>"remaining"</code>) in mins from a combo box, press <code>"Start"</code>, and the countdown will start.</p>
  
  <p>However, in my current code (below), after selecting the total time from the combo box and pressing <code>"Start"</code>, the label will show the correct start time, but the countdown will not start. If I manually set <code>"remaining"</code> at the top of the code (line 23) to the total time, eg. <code>61000</code> and remove the line <code>"remaining = convertTime();"</code> under my <code>actionPerformed</code> interface, the countdown timer works perfectly fine.</p>
  
  <p>I'm not sure what went wrong.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  import java.text.NumberFormat;
  
  import javax.swing.*;
  
  public class GUITimer extends JFrame implements ItemListener {
  
  JLabel jltime;
  JLabel jl;
  JComboBox jcb;
  JButton jbt;
  JButton jbt2;
  NumberFormat format;
  
  public Timer timer;
  public long initial;
  public long ttime2;
  public String ttime;
  public long remaining;
  
  GUITimer() {
  
      jl=new JLabel("TOTAL TIME (minutes)");
      jl.setHorizontalAlignment((int) CENTER_ALIGNMENT);
  
      jltime=new JLabel("");
      jltime.setHorizontalAlignment((int) CENTER_ALIGNMENT);
      jltime.setForeground(Color.WHITE);
      jltime.setBackground(Color.BLACK);
      jltime.setOpaque(true);
      jltime.setFont(new Font("Arial", Font.BOLD, 450));
  
      jbt=new JButton("START");
      jbt2=new JButton("RESET");
  
      jcb=new JComboBox();
      jcb.addItem("15");
      jcb.addItem("14");
      jcb.addItem("13");
      jcb.addItem("12");
      jcb.addItem("11");
      jcb.addItem("10");
      jcb.addItem("9");
      jcb.addItem("8");
      jcb.addItem("7");
      jcb.addItem("6");
      jcb.addItem("5");
      jcb.addItem("4");
      jcb.addItem("3");
      jcb.addItem("2");
      jcb.addItem("1");
  
      JPanel jp1=new JPanel();
      jp1.setLayout(new GridLayout(1,4,10,0));
      jp1.add(jl);
      jp1.add(jcb);
      jp1.add(jbt);
      jp1.add(jbt2);
  
      getContentPane().add(jp1,BorderLayout.NORTH);
  
      JPanel jp2=new JPanel();
      jp2.setLayout(new GridLayout(1,2,10,10));
      jp2.add(jltime);
  
      getContentPane().add(jp2,BorderLayout.CENTER);
  
      event e = new event();
      jbt.addActionListener(e);
      jbt2.addActionListener(e);
  
      jcb.addItemListener(this);
  }
  
      public static void main(String[] args) {
      GUITimer frame=new GUITimer();
      frame.setSize(500,500);
      frame.setTitle("LARC Moot Countdown Timer");
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  }
  
  //this method will run when user presses the start button
  void updateDisplay() {
  
      Timeclass tc = new Timeclass();
      timer = new Timer(1000, tc);
      initial = System.currentTimeMillis();
      timer.start();
  }
  
  //code for what happens when user presses the start or reset button
  public class event implements ActionListener {
  
      public void actionPerformed(ActionEvent e) {
      String bname=e.getActionCommand();
      if(bname.equals("START"))
      {
          updateDisplay();
      }
      else
      {
          jltime.setText("");
          timer.stop();
          remaining = convertTime();
      }
      }
  }
  
  //code that is invoked by swing timer for every second passed
  public class Timeclass implements ActionListener {
  
      public void actionPerformed(ActionEvent e) {
  
          remaining = convertTime();
          long current = System.currentTimeMillis();
          long elapsed = current - initial; 
          remaining -= elapsed;
          initial = current; 
  
          format = NumberFormat.getNumberInstance();
          format.setMinimumIntegerDigits(2); 
  
          if (remaining &lt; 0) remaining = (long)0;
          int minutes = (int)(remaining/60000);
          int seconds = (int)((remaining%60000)/1000);
          jltime.setText(format.format(minutes) + ":" + format.format(seconds));
  
          if (remaining == 0)
          {
          jltime.setText("Stop");
          timer.stop();
          }
  }
  }
  
      //get the number of minutes chosen by the user and activate convertTime method
      public void itemStateChanged(ItemEvent ie) {
  
          ttime = (String)jcb.getSelectedItem();
          convertTime();
      }
  
      //first need to convert no. of minutes from string to long. 
      //then need to convert that to milliseconds.        
      public long convertTime() {
  
          ttime2 = Long.parseLong(ttime);
          long converted = (ttime2*60000)+1000;
          return converted;
      }
  }
  </code></pre>