Swing learning 17: Java Swing Timer

The Timer component can trigger one or more actionevents at a specified time interval. The process of setting a Timer includes creating a Timer object, registering one or more action listeners on the object, and starting the Timer using the start() method.

For example, the following code creates and starts a Timer that fires an action event once per second, specified by the first parameter of the Timer constructor. The second parameter of the Timer constructor specifies the listener that receives the Timer action event.

int delay=1000;    //Time interval in milliseconds
ActionListener taskPerformer=new ActionListener()
    public void afrfcionPerformed(ActionEvent evt)
        //Specific tasks
new Timer(delay,taskPerformer).start();

When you create a Timer class, you need to specify a delay parameter and an ActionListener. The delay parameter sets the delay (in milliseconds) between the initial delay and the event trigger. After the Timer starts, it waits for the initial delay before triggering the first ActionEvent to the registered listener. After the first event, it continues to fire the event every time it exceeds the inter event delay until it is stopped.

After you create the Timer class, you can change the initial delay and the delay between events individually, and you can add other actionlisteners. If you want the Timer to fire and then stop only the first time, you can call setRepeats(false) on the Timer. The common methods of Timer class are shown in Table 1.

Example 1
Write an example to simulate the progress bar of software installation. In this instance, Timer class is used to simulate the actual process of software installation. JProgressBar class displays the corresponding progress information. The main implementation process is as follows.

(1) Create a JProgressBarDemo1 class and implement the ActionListener interface and ChangeListener interface.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JProgressBarDemo1 implements ActionListener,ChangeListener
    JFrame frame=null;
    JProgressBar progressbar;
    JLabel label;
    Timer timer;
    JButton b;
    public static void main(String[] agrs)
        new JProgressBarDemo1();    //Create an instanced object

(2) Set the progress bar and timer in the construction method. The main codes are as follows:

public JProgressBarDemo1()
    frame=new JFrame("Software installation");
    Container contentPane=frame.getContentPane();
    label=new JLabel(" ",JLabel.CENTER);    //Create a text label that displays progress information
    progressbar=new JProgressBar();    //Create a progress bar
    progressbar.addChangeListener(this);    //Add event listener
    //Set the geometry of the progress bar
    progressbar.setPreferredSize(new Dimension(300,20));
    //Add start button
    JPanel panel=new JPanel();
    b=new JButton("install");
    //Add event listener
    timer=new Timer(100, this);    //Create a timer with an interval of 100 milliseconds
    //Add components to frame

(3) Implement the actionPerformed() method in the ActionListener interface, where you can control the start of the timer and the change of the progress bar value. The specific code is as follows:

//Implementation of event listener interface
public void actionPerformed(ActionEvent e)
        int value=progressbar.getValue();

(4) Implement the stateChange() method in the ChangeListener interface. The specific code is as follows:

public void stateChanged(ChangeEvent e1)    //Implementation of event listener interface
    int value=progressbar.getValue();
        label.setText("Currently completed progress:"+Integer.toString(value)+" %");

(5) Run the program. By default, the progress bar is 0%, as shown in Figure 1.

When you click the Install button, the progress changes all the time, as shown in Figure 2.

Thanks for watching

162 original articles published, 30 praised, 10000 visitors+
Private letter follow

Tags: Java

Posted on Fri, 13 Mar 2020 04:07:28 -0400 by tanvirtonu