What is the efficient way to pass value from other class to Main class, in JAVA? -
i have in main class:
public void start(stage primarystage) throws exception { thread timecheckthread = new thread() { @override public void run() { try{ while (!done) { system.out.println("running!!"); system.out.println(); currentdate = localdate.now(); if (!olddate.equals(currentdate)) { index++; savedata(); system.out.println("saved!"); } currentthread().sleep(5000); } }catch(interruptedexception e){ e.printstacktrace(); } } }; timecheckthread.start(); stage = primarystage; stage.initstyle(stagestyle.transparent); fxmlloader loader = new fxmlloader(getclass().getresource("sample.fxml")); parent root = (parent) loader.load(); stackpane stackpane = new stackpane(root); stackpane.setstyle("-fx-background-color: rgba(0,0,0,0);"); scene scene = new scene(stackpane, 312, 212); scene.setfill(color.transparent); stage.setscene(scene); controller controller = loader.<controller>getcontroller(); controller.registerstage(stage); stage.show(); } //launch 'start' public static void main(string[] args) { launch(args); }
and in controller class:
public void exitbuttonclicked() { platform.exit(); }
i want pass boolean method exitbuttonclicked
main class can end loop of while(!done)
. efficient approach accomplish this? is, having setter
done
in main method, , invoking setter
instance of main
class in controller
class, method?
there 2 issues. controller needs access variables, , have 5s sleep. 1 way it, without passing around instance of main class make static utility method.
class main extends application{ static volatile boolean done = false; static thread timecheckthread; static void applicationisexiting(){ done = true; timecheckthread.interrupt(); } }
then when create time check thread assign static variable:
timecheckthread = new thread() { ...
the method in controller have call utility method.
public void exitbuttonclicked() { platform.exit(); main.applicationisexiting(); }
Comments
Post a Comment