java - JavaFX 8 initialize method -
so trying chat type program using javafx gui. have class acts server loop , keep adding client connections it.
public void serverconnection() { // portnumber saved constructor try (serversocket socket = new serversocket(this.portnumber)) { // loop bool set true while (loop) { // class extends thread , has own overwritten start method new myclass(socket.accept()).start(); } } catch (ioexception e) { system.exit(404); } }
so problem (i assuming) was, loop keeps looping until program closes. since calling within javafx's initialize method
public void initialize(url url, resourcebundle rb) { // constructor, nothing here needed post myclass z = new myclass(45234); // problem here, since has loop, z.serverconnection(); // gui wont load till after done // serverconnection done after program ends }
the problem is, apparently, gui not load until after initialize has finished, not finish until program closes. after google searching, not find fix this. need way call method this, after initialize method has finished. client side class similar this, methods connect on activated on events when clicking login button. serverside one, trying start without interaction user. there way call method or make work after initialize method has ran?
you might want run loop in thread, like
thread t = new thread(z::serverconnection) t.start()
if @ end of initialization()
method, run then.
this start thread runs forever; might want add feature interrupting thread when program supposed terminated.
remember changing in gui need sumbit task via platform.runlater()
. because gui may modified within 1 thread. in order modify anything, have wrap in runnable , submit execution.
you can in way:
platform.runlater(new runnable() { @override public void run() { dowhateverneedstobedone(); } });
in java 8, can of following, depending on extent of work done:
platform.runlater(() -> { dowhateverneedstobedone(); }); platform.runlater(() -> dowhateverneedstobedone()); platform.runlater(this::dowhateverneedstobedone);
the latter works if dowhateverneedstobedone()
method of this
.
Comments
Post a Comment