javafx 8 - Java: How do I start a standalone application from the current one when both are in the same package? -
this seems should easy, must missing obvious: have 4 standalone applications in same package, us.glenedwards.mypackage,
- myclass1 extends application
- myclass2 extends application
etc...
i need each class act own standalone application. yet want able start other 3 classes 1 i'm in clicking link. android allows me using intents:
intent intent = new intent(this, editdata.class); overridependingtransition(r.layout.edit_data_scrollview, r.layout.state); startactivity(intent);
i've tried starting myclass2 myclass1 using
myclass2.launch("");
but error, "application launch must not called more once". way can work if remove both "extends application" , start() method myclass2, means myclass2 no longer standalone application.
how can start myclass2, myclass3, or myclass4 myclass1 4 of them being standalone applications?
you can make work calling start(...)
directly on new instance of 1 of application
subclasses, kind of feels bit of hack, , contrary intended use of start(...)
method. (just semantically: method called start
in class called application
should executed when application starts, not @ arbitrary point after running.)
you should think of start
method replacement main
method in traditional java application. if had 1 application calling application's main
method, (hopefully) come conclusion had structured things incorrectly.
so recommend refactoring design individual components not application subclasses, plain old regular classes:
public class firstmodule { // can parent subclass: private borderpane view ; public firstmodule() { // create view; load fxml if use fxml view = new borderpane(); // configure view, populate controls, etc... } public parent getview() { return view ; } // other methods needed... }
and, similarly,
public class secondmodule { private gridpane view ; public secondmodule { view = new gridpane(); // etc etc } public parent getview() { return view ; } }
now can things like
firstmodule firstmodule = new firstmodule(); scene scene = new scene(firstmodule.getview()); stage stage = new stage(); stage.setscene(scene); stage.show();
anywhere need them. can create standalone applications each module:
public class firstapplication extends application { @override public void start(stage primarystage) { scene scene = new scene(new firstmodule().getview()); primarystage.setscene(scene); primarystage.show(); } }
or can instantiate them part of bigger application:
public class compositemodule { private hbox view ; public compositemodule() { button first = new button("first module"); first.setonaction(e -> { parent view = new firstmodule().getview(); scene scene = new scene(view); stage stage = new stage(); stage.initowner(first.getscene().getwindow()); stage.setscene(scene); stage.show(); }); button second = new button("second module"); second.setonaction(e -> { parent view = new secondmodule().getview(); scene scene = new scene(view); stage stage = new stage(); stage.initowner(second.getscene().getwindow()); stage.setscene(scene); stage.show(); }); hbox view = new hbox(10, first, second); view.setalignment(pos.center); } public parent getview() { return view ; } }
and
public class compositeapplication extends application { @override public void start(stage primarystage) { scene scene = new scene(new compositemodule().getview(), 360, 150); primarystage.setscene(scene); primarystage.show(); } }
the way think of application
subclasses represent entire running application. consequently makes sense ever instantiate 1 such class once per jvm, should consider these inherently not reusable. move code want reuse different class somewhere.
Comments
Post a Comment