Ridzuan Adris
Ridzuan Adris

Reputation: 1272

How to run Swing.timer concurrently without lag or slow performance

How do we run more than one Swing.timer concurrently?

My application is when i hit my jframe button it will execute two swing timer and run concurrently, but it seem this action will make my application run very slow and lagging.

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

//************first swing timer execution************************ 

   ActionListener taskPerformer = new ActionListener() {
   public void actionPerformed(ActionEvent evt) {

       jLabel1.setText("Hello Girls");

            }
            };

javax.swing.Timer timer = new javax.swing.Timer( 0 , taskPerformer);
timer.setRepeats(true);
timer.start();     // execute first swing timer 


 //************second swing timer execution************************ 

ActionListener taskPerformer1 = new ActionListener() {
   public void actionPerformed(ActionEvent evt) {

       jLabel1.setText("Hello Guys");

            }
            };

javax.swing.Timer timer1 = new javax.swing.Timer( 0 , taskPerformer1);
timer1.setRepeats(true);
timer1.start(); //execute second swing timer
}             

i notice if i run both timer it will freeze my application. how can i run both timer without freeze or lag?

Upvotes: 0

Views: 1444

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

A delay of 0 milliseconds is likely flooding the EDT with Action events (and other updates event generated by the setText call).

You need to provide breathing room for the EDT to not only process your Action events, but also the consequences of these events, for example

Timer

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
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.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TimerExample {

    public static void main(String[] args) {
        new TimerExample();
    }

    public TimerExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private JLabel label;
        private JButton button;

        private Timer hisTimer;
        private Timer herTimer;

        public static final int DELAY = 1000;

        public TestPane() {
            setLayout(new BorderLayout());
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            button = new JButton("Start");

            hisTimer = new Timer(DELAY, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    label.setText("His");
                }
            });
            hisTimer.setInitialDelay(DELAY / 2);
            hisTimer.setCoalesce(true);

            herTimer = new Timer(DELAY, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    label.setText("Hers");
                }
            });
            herTimer.setCoalesce(true);

            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (hisTimer.isRunning() && herTimer.isRunning()) {
                        hisTimer.stop();
                        herTimer.stop();
                        button.setText("Start");
                    } else {
                        hisTimer.start();
                        herTimer.start();
                        button.setText("Stop");
                    }
                }
            });

            add(button, BorderLayout.SOUTH);
            add(label);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

Upvotes: 3

Related Questions