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());
开发者_如何学C 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.
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.
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)?
精彩评论