javafx 8 - Copy entite fxml data to different container -
i having 2 vbox(es).
first vbox fx:id vbox1
second vbox fx:id vbox2
in vbox1 having textbox, combobox, buttons , else.
i having 1 button want copy(onclick) entire source/fxml vbox1 vbox2.
is there anyway that?
define content of vbox
es in separate fxml file. can include content in first vbox
directly in "main" fxml <fx:include>
:
<vbox fx:id="vbox1"> <fx:include source="content.fxml"/> </vbox>
and can load copy in button's handler with
@fxml public void handlebuttonaction(actionevent e) throws exception { fxmlloader loader = new fxmlloader(getclass().getresource("content.fxml")); vbox2.getchildren().add(loader.load()); }
complete example (everything in package called application
):
main.fxml:
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.borderpane?> <?import javafx.scene.layout.hbox?> <?import javafx.scene.layout.vbox?> <?import javafx.scene.control.button?> <borderpane xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.maincontroller"> <center> <hbox spacing="5"> <vbox fx:id="vbox1"> <fx:include source="content.fxml"/> </vbox> <vbox fx:id="vbox2"/> </hbox> </center> <bottom> <button text="load" onaction="#load" borderpane.alignment="center"/> </bottom> </borderpane>
maincontroller.java:
package application; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.scene.layout.vbox; public class maincontroller { @fxml private vbox vbox1 ; @fxml private vbox vbox2 ; @fxml private void load() throws exception { fxmlloader loader = new fxmlloader(getclass().getresource("content.fxml")); vbox2.getchildren().add(loader.load()); } }
content.fxml:
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.vbox?> <?import javafx.scene.control.textfield?> <?import javafx.scene.control.combobox?> <?import javafx.collections.fxcollections?> <?import java.lang.string?> <?import javafx.scene.control.button?> <vbox xmlns:fx="http://javafx.com/fxml/1"> <textfield prompttext="text field"/> <combobox> <items> <fxcollections fx:factory="observablearraylist"> <string fx:value="one"/> <string fx:value="two"/> <string fx:value="three"/> </fxcollections> </items> </combobox> <button text="click me"/> </vbox>
main.java:
package application; import javafx.application.application; import javafx.stage.stage; import javafx.scene.scene; import javafx.scene.layout.borderpane; import javafx.fxml.fxmlloader; public class main extends application { @override public void start(stage primarystage) { try { borderpane root = (borderpane)fxmlloader.load(getclass().getresource("main.fxml")); scene scene = new scene(root,400,400); scene.getstylesheets().add(getclass().getresource("application.css").toexternalform()); primarystage.setscene(scene); primarystage.show(); } catch(exception e) { e.printstacktrace(); } } public static void main(string[] args) { launch(args); } }
Comments
Post a Comment