Re: How can I reset the text of button every second?
"jtl.zheng" <jtl.zheng@gmail.com> wrote in message
news:1157075664.227922.149200@p79g2000cwp.googlegroups.com...
I want to set the button's text every second
as: 1 -> 2 -> 3 -> 4 .........
my code is:
------------------------------------
public class Ooop {
public static void doIt() {
JFrame jFrame = new JFrame();
JButton jButton = new JButton("xxx");
jFrame.add(jButton);
jFrame.pack();
jFrame.setVisible(true);
int i=0;
while(true){
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
jButton.setText(String.valueOf(i++));
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
doIt();
}
});
}
}
-------------------------------
I use Thread.sleep() to do it
but it can not work....
It show nothing all the time
just a blank frame, not button at all
why? is it about the Thread the UI using?
Yes, exactly. Your counter has taken over the event dispatch thread which
now cannot update the screen. The Timer class will run the counting and
waiting in a separate thread so that only the actual button update takes
place in the EDT (which is necessary).
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/