tags:

views:

47

answers:

2

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.

+2  A: 

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.

jjnguy
I was under the impression that using the Swing timer would put it in the EDT.
Javamann
thanks for tutorial.
nicky
Indeed, this answer is not correct. Timers are appropriate for this, and using Invoke Later should not be necessary.
jjnguy
A: 

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)?

netzwerg
code is editted ..the tetfield should be updated after 1 sec
nicky