java - What's the omission/error out here? -
there should error/omission in next code can't see because i've never used threads. can please find i'm missing?
import java.awt.borderlayout; import java.awt.button; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.timer; import java.util.timertask; import javax.swing.jdialog; import javax.swing.jlabel; public class javaswingtest extends jdialog { private jlabel m_countlabel; private timer m_timer = new timer(); private class incrementcounttask extends timertask { @override public void run() { m_countlabel.settext(long.tostring(system.currenttimemillis() / 1000)); } } private javaswingtest() { createui(); m_timer.schedule(new incrementcounttask(), 1000, 1000); } private void createui() { button button1 = new button("action1"); button1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { dolongoperation(); } }); add(button1, borderlayout.north); m_countlabel = new jlabel(long.tostring(system.currenttimemillis() / 1000)); add(m_countlabel, borderlayout.center); } /** * simulates operation takes time complete. */ private void dolongoperation() { try { thread.sleep(3000); } catch (interruptedexception e) { // ignored test } } /** * @param args */ public static void main(string[] args) { new javaswingtest().setvisible(true); } }
swing, ui toolkits, not thread-safe. settext() call should forwarded event thread.
to initiate gui work timer thread, use swingutilities.invokelater() or invokeandwait().
Comments
Post a Comment