Java Swing - GUIs
TOPIC 05 – TIMERS

 

LESSON NOTE

 

 

TIMERS

 

Timers are very simple objects.  You provide an amount of time and then start them.  In a separate thread, they wait the specified amount of time and then trigger an action event.  They then trigger again after that same amount of time. 

 

If needed, you can easily stop the timer and restart it as needed.

 

EXAMPLE - TIMER COUNTER

 

Here is a simple example of a GUI containing only a text field.  Inside the text field is the number zero.  Every second (1000 milliseconds), the timer triggers an event which leads to the number in the text field getting increased.

 

 

The code is below.  The lines related to the Timer object are highlighted.

 

public class TimerCounter extends JFrame implements ActionListener

{

     public Timer t;

     public JTextField txt;

     public int count;

    

     public TimerCounter()

     {

           count = 0;

           t = new Timer(1000,this);  //1000milliseconds=1second

 

           Container cp = this.getContentPane();

           cp.setLayout(new FlowLayout());

 

           txt = new JTextField(5);

           txt.setText("" + count);

           cp.add(txt);

          

           this.setTitle("Timer Counter");

           this.pack();

           this.setVisible(true);

          

           //start timer (should always be done at the end)

           t.start();

     }

    

     public void actionPerformed(ActionEvent e)

     {

           count++;              //increase count by 1

           txt.setText("" + count); //display count

     }

    

     public static void main(String[] args)

     {

           TimerCounter tc = new TimerCounter();

     }

}

 

EXAMPLE – APPEARING BUTTONS

 

Here is an example of an empty GUI that has a button added to it every 2 seconds.  This is all done by a Timer object that triggers an event every 2000 milliseconds.  And, in the actionPerformed(ActionEvent e) method, we add a button to the GUI and restart the timer.

 

 

The code is below.  Notice that the Container cp is a datafield so that we can access it from the actionPerformed() method lower down. 

 

 

public class AppearingButtons extends JFrame implements ActionListener

{

     public Timer t;

     public int totalButtons;

     public Container cp;

    

     public AppearingButtons()

     {

           totalButtons = 0;

           t = new Timer(2000,this);  //1000milliseconds=1second

 

           cp = this.getContentPane();

           cp.setLayout(new FlowLayout());

 

           this.setTitle("Appearing Buttons");

           this.setSize(500,400);

           this.setVisible(true);

          

           //start timer (should always be done at the end)

           t.start();

     }

    

     public void actionPerformed(ActionEvent e)

     {

           totalButtons++;

           JButton b = new JButton("Button "+totalButtons);

           cp.add(b);

          this.setVisible(true);

     }

    

     public static void main(String[] args)

     {

           AppearingButtons tc = new AppearingButtons();

     }

}

 

EXAMPLE 3 – ALPHABET COUNTDOWN

 

In this example, a counter starts at 15 and gradually goes down to 0. The user has to type the alphabet in in order to win this simple game.  The timer update the countdown.  Also, the time is stopped once the countdown is at zero.

 

 

The code is below.

 

public class AlphabetCountdown extends JFrame implements ActionListener

{

     public Timer t;

     public int timeLeft;

    

     public JLabel lab;

     public JTextArea txta;

     public JLabel lab2;

    

     public AlphabetCountdown()

     {

           timeLeft = 15;

           t = new Timer(1000,this);  //1000milliseconds=1second

 

           Container cp = this.getContentPane();

           cp.setLayout(new FlowLayout());

          

           lab = new JLabel("Please type in the alphabet (no spaces).");

           cp.add(lab);

          

           txta = new JTextArea(10,40);

           cp.add(txta);

          

           lab2 = new JLabel("Time remaining:" + timeLeft);

           cp.add(lab2);

 

           this.setTitle("Countdown");

           this.setSize(500,400);

           this.setVisible(true);

          

           //start timer (should always be done at the end)

           t.start();

     }

    

     public void actionPerformed(ActionEvent e)

     {

           timeLeft--;

           lab2.setText("Time remaining:" + timeLeft);

           if (timeLeft <= 0)

           {

                t.stop();

                txta.setEditable(false);

               

                                          if(txta.getText().toLowerCase().equals("abcdefghijklmnopqrstuvwxyz"))

                {

                    txta.append("\n\nYOU WIN!");

               }

               else

               {

                    txta.append("\n\nYOU LOSE!");

               }            

          }

     }

    

     public static void main(String[] args)

     {

           AlphabetCountdown tc = new AlphabetCountdown();

     }

}