I am new to java and javafx and have a problem that I could not solve. I need to dynamically add new user controls to a javafx scene. Next, I need the interaction between the main control and the added controls. I found already useful information on the Internet, but could not collect it.
So, I am creating a small example to explain:
main class:
public class Test_TwoController extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("Fxml1.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
Main fxml file:
<AnchorPane id="fxml1_anchorpane_id" fx:id="fxml1_anchorpane" prefHeight="206.0" prefWidth="406.0" xmlns:fx="http://javafx.com/fxml" fx:controller="test_twocontroller.Fxml1Controller"> <children> <HBox id="fxml1_hbox_id" fx:id="fxml1_hbox" prefHeight="200.0" prefWidth="400.0"> <children> <Button id="fxml1_button_id" fx:id="fxml1_button" mnemonicParsing="false" onAction="#button_action" prefHeight="200.0" prefWidth="200.0" text="Button" /> </children> </HBox> </children> </AnchorPane>
and its controller:
public class Fxml1Controller implements Initializable { @FXML HBox hbox; @FXML Button button; @Override public void initialize(URL url, ResourceBundle rb) { } public void button_action(ActionEvent event) throws IOException {
And now the control dynamically adds:
Its fxml:
<AnchorPane id="fxml2_anchorpane_id" fx:id="fxml2_anchorpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml" fx:controller="test_twocontroller.Fxml2Controller"> <children> <TabPane id="fxml2_tabpane_id" fx:id="fxml2_tabpane" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE"> <tabs> <Tab id="fxml2_tab1_id" fx:id="fxml2_tab1" text="tab1"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> </content> </Tab> <Tab id="fxml2_tab2_id" fx:id="fxml2_tab2" onSelectionChanged="#onSelectionChanged" text="tab2"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> </content> </Tab> </tabs> </TabPane> </children> </AnchorPane>
and controller:
public class Fxml2Controller { @FXML TabPane tabpane; @FXML Tab tab1; @FXML Tab tab2; public Fxml2Controller() throws IOException { Parent root = FXMLLoader.load(getClass().getResource("Fxml2.fxml")); Scene scene = new Scene(root); Stage stage = new Stage(); stage.setScene(scene); } public void onSelectionChanged(Event e) throws IOException { FXMLLoader loader = new FXMLLoader();
Usage: fxml2 should be added to the hbox fxml1. Then after clicking the button in fxml1, the fxml2 tabs should change. Perhaps you look at this image http://s13.postimage.org/uyrmgylo7/two_controlls.png
So my questions are:
- How can I add one or more fxml2 controllers to hbox fxml1?
- How can I access one control from another or exchange data between controls? See the onSelectionChanged () method in Fxml2Controller for details.
Thanks in advance, solarisx
source share