nicky
nicky

Reputation: 3908

update text field after certain interval

I am updating text field after certain time.

Here is my code:

ActionListener task = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                    tip1.setText(ad1.tip1());
                    tip2.setText(ad1.tip2());
                    tip3.setText(ad1.tip3());
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
          }
    };

        new javax.swing.Timer(1000, task).start();

my application respose very slow using this code.

Upvotes: 0

Views: 1814

Answers (2)

Rahel Lüthy
Rahel Lüthy

Reputation: 7007

The timer code looks unsuspicious. Without knowing further details, the only possible culprit is

update.addActionListener(task);

What is update and how often will the listener/task be fired (in addition to the executions triggered via the timer)?

Upvotes: 0

jjnguy
jjnguy

Reputation: 138884

Edit: This is not a correct solution.

You need to throw it onto the EDT. You are not supposed to change your Swing interface on any thread other than EDT.

try {
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            tip1.setText(ad1.tip1());
            tip2.setText(ad1.tip2());
            tip3.setText(ad1.tip3());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
    });
}

Sun has a few great tutorials on this subject.

Upvotes: 2

Related Questions