java - JButton does not change a field value -
i facing strange problem simple jframe application has 1 button in order change field value. when try debug application, works fine, when try run it, doesn't work , gets stuck in while loop. graphics class:
public class test extends jframe implements actionlistener { private static final long serialversionuid = 1l; private boolean connectpressed; public jbutton btnconnect; public test() { jpanel p = new jpanel(); p.setlayout(null); // connect button btnconnect = new jbutton("connect"); btnconnect.setbounds(0, 0, 100, 20); p.add(btnconnect); btnconnect.addactionlistener(this); getcontentpane().add(p); setdefaultcloseoperation(exit_on_close); settitle("client"); setsize(200,200); setvisible(true); } public boolean get() { //system.out.println(connectpressed); return connectpressed; } public void set(boolean b) { connectpressed = b; } @override public void actionperformed(actionevent e) { //e.getactioncommand(); connectpressed = true; system.out.println(connectpressed); } }
and here test code application:
public class test2 { public static void main(string[] args) { system.out.println("hello"); test t = new test(); t.set(false); while (true) { if ((t.get())) break; } system.out.println("bye"); } }
can me problem?
when run this:
while (true) { if ((t.get())) break; }
the thread eat cpu, making ui unresponsive; overcome, 1 easy way adding sleep time inside loop:
while (true) { thread.sleep(250); // sleep 250 msecs if (t.get()) break; }
anyway example, not solution; better way registering main class listener, or running loop in separated thread.
Comments
Post a Comment