scene - javaFX - how to use function or object of a controller from another controller -
i have seen lot of question similar mine, didn't figure out how solve problem, there code:
i have simple main:
mainclassfxgui.java
public class mainclassfxgui extends application { @override public void start(stage stage) throws exception { parent root = fxmlloader.load(getclass().getresource("myprogrammainguitab.fxml")); scene scene = new scene(root); stage.setscene(scene); stage.settitle("assicurazione"); stage.show(); } /** * @param args command line arguments */ public static void main(string[] args) { launch(args); } }
and 3 fxml files, 1 of them contains other two, tag
<fx:include source="mytab1.fxml" />
myprogrammainguitab.fxml
... <tabpane maxheight="1.7976931348623157e308" maxwidth="1.7976931348623157e308" prefheight="5000.0" prefwidth="5000.0" tabclosingpolicy="unavailable"> <tabs> <tab closable="false" text="mytab1"> <content> <fx:include source="mytab1.fxml" /> </content> </tab> <tab closable="false" text="mytab2"> <content> <fx:include source="mytab2.fxml" /> </content> </tab> </tabs> </tabpane> ...
and 3 controller:
myprogrammainguitabcontroller.java
.... @fxml private label leftst; /** * initializes controller class. */ @override public void initialize(url url, resourcebundle rb) { leftst.settext(""); } public void setleftst(string st){ leftst.settext(st); } ...
mytab1controller.java
@fxml private void handlebuttonaction(actionevent event) { system.out.println("you clicked me!"); setleftst("can change label of myprogrammainguitabcontroller?"); }
i tried in way:
mytab1controller.java
private myprogrammainguitabcontroller controller; @fxml private void handlebuttonaction(actionevent event) { system.out.println("you clicked me!"); controller.setleftst("can change label of myprogrammainguitabcontroller?"); } @override public void initialize(url url, resourcebundle rb) { fxmlloader fxmlloader = new fxmlloader(getclass().getresource("myprogrammainguitab.fxml")); try { fxmlloader.load(); } catch (ioexception exception) { throw new runtimeexception(exception); } controller = (myprogrammainguitabcontroller)fxmlloader.getcontroller(); }
but have null pointer exception or if move code instanzialization of object 'controller' in handlebuttonaction function, don't have error, label not change.
maybe can create static scene , static stage in mainclassfxgui.java? don't know how use them...
mainclassfxgui.java
public class mainclassfxgui extends application { static private scene scene; static private stage stage; @override public void start(stage stage) throws exception { parent root = fxmlloader.load(getclass().getresource("myprogrammainguitab.fxml")); this.scene = new scene(root); stage.setscene(scene); stage.settitle("assicurazione"); stage.show(); this.stage = stage; } /** * @param args command line arguments */ public static void main(string[] args) { launch(args); } public static scene getscene(){ return assicurazionefxgui.scene; } public static stage getstage(){ return assicurazionefxgui.stage; } }
can getscene() or getstage()? idea or suggest?
thanks everyone
define stringproperty
in mytab1controller
, usual methods:
public class mytab1controller { private final stringproperty leftst = new simplestringproperty(); public stringproperty leftstproperty() { return leftst ; } public final string getleftst() { return leftstproperty().get(); } public final void setleftst(string leftst) { leftstproperty().set(leftst); } // ... @fxml public void handlebuttonaction() { setleftst(...); } // ... }
now assign fx:id
attribute fx:include
tag:
<tab closable="false" text="mytab1"> <content> <fx:include fx:id="mytab1" source="mytab1.fxml" /> </content> </tab>
this means can inject controller mytab1.fxml
controller fxml included (myprogrammainguitabcontroller
):
public class myprogrammainguitabcontroller { @fxml private mytab1controller mytab1controller ; }
and can observe property , update label when changes:
public class myprogrammainguitabcontroller { @fxml private mytab1controller mytab1controller ; @fxml private label leftst; public void initialize() { leftst.settext(""); mytab1controller.leftstproperty().addlistener((obs, oldvalue, newvalue) -> leftst.settext(newvalue)); } }
note rule here field name controller value of fx:id
attribute word "controller" appended: mytab1
in fx:id
resolves mytab1controller
field controller injected. see nestedcontrollers more information.
Comments
Post a Comment