Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind controls across two fxml from the Main controller
    text
    copied!<p>I Need to bind controls from different fxml in the main controller. I have 3 fxml files named MainView.fxml, ChildView1.fxml and ChildView2.fxml.</p> <p><strong>MainView.fxml</strong></p> <pre><code>&lt;AnchorPane fx:id="view" prefHeight="280.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="binding.example2.MainViewController"&gt; &lt;children&gt; &lt;VBox prefHeight="280.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt; &lt;children&gt; &lt;fx:include fx:id="child1" source="ChildView1.fxml" /&gt; &lt;fx:include fx:id="child2" source="ChildView2.fxml" /&gt; &lt;/children&gt; &lt;/VBox&gt; &lt;/children&gt; &lt;/AnchorPane&gt; </code></pre> <p><strong>ChildView1.fxml</strong></p> <pre><code>&lt;AnchorPane fx:id="view" prefHeight="78.0" prefWidth="464.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="binding.example2.ChildView1Controller"&gt; &lt;children&gt; &lt;HBox layoutX="32.0" layoutY="14.0" prefHeight="51.0" prefWidth="200.0" spacing="10.0"&gt; &lt;children&gt; &lt;Button fx:id="button1" mnemonicParsing="false" prefHeight="37.0" text="Button1" /&gt; &lt;Button fx:id="button2" mnemonicParsing="false" prefHeight="37.0" text="Button2" /&gt; &lt;/children&gt; &lt;/HBox&gt; &lt;/children&gt; &lt;/AnchorPane&gt; </code></pre> <p><strong>ChildView2.fxml</strong></p> <pre><code>&lt;AnchorPane fx:id="view" prefHeight="244.0" prefWidth="568.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="binding.example2.ChildView2Controller"&gt; &lt;children&gt; &lt;TableView fx:id="tableView" prefHeight="244.0" prefWidth="568.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /&gt; &lt;/children&gt; &lt;/AnchorPane&gt; </code></pre> <p><strong>MainController.java</strong> </p> <pre><code>package binding.example2; import javafx.fxml.FXML; import javafx.scene.Parent; import javafx.scene.layout.AnchorPane; public class MainViewController { @FXML private Parent view; @FXML private AnchorPane child1; @FXML private AnchorPane child2; @FXML private ChildView1Controller childView1Controller; @FXML private ChildView2Controller childView2Controller; @FXML void initialize() { System.out.println("MainViewController.initialize()"); // childView1Controller.getButton1() ; } public Parent getView() { return view; } } </code></pre> <p><strong>ChildView1Controller.java</strong></p> <pre><code>package binding.example2; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.control.Button; public class ChildView1Controller { @FXML private ResourceBundle resources; @FXML private URL location; @FXML private Button button1; @FXML private Button button2; @FXML private Node view; @FXML void initialize() { System.out.println("ChildView1Controller.initialize()"); button1.setDisable(true); } public Node getView() { return view; } public Button getButton1(){ return button1; } } </code></pre> <p><strong>ChildView2Controller.java</strong></p> <pre><code>package binding.example2; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; public class ChildView2Controller { @FXML private Node view; @FXML private TableView&lt;Person&gt; tableView; ObservableList&lt;Person&gt; data ; public Node getView(){ return view ; } @FXML void initialize() { System.out.println("ChildView2Controller.initialize()"); //populateTable(); } public TableView&lt;Person&gt; getTableView(){ return tableView ; } } </code></pre> <p>By default, button1 is disabled from ChildView1Controller. </p> <p>I want to enable it when a table row in my other view (ChildView2.fxml) is selected. Again disable it when the table row is deselected. Basically i want to bind the button and table so that button is enabled when a table row is selected. Please help me with the MainViewController code to bind the button and the table row. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload