我需要綁定來自主控制器中不同fxml的控件。 我有3個名爲MainView.fxml,ChildView1.fxml和ChildView2.fxml的fxml文件。如何綁定來自主控制器的兩個fxml控件
MainView.fxml
<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">
<children>
<VBox prefHeight="280.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<fx:include fx:id="child1" source="ChildView1.fxml" />
<fx:include fx:id="child2" source="ChildView2.fxml" />
</children>
</VBox>
</children>
</AnchorPane>
ChildView1.fxml
<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">
<children>
<HBox layoutX="32.0" layoutY="14.0" prefHeight="51.0" prefWidth="200.0" spacing="10.0">
<children>
<Button fx:id="button1" mnemonicParsing="false" prefHeight="37.0" text="Button1" />
<Button fx:id="button2" mnemonicParsing="false" prefHeight="37.0" text="Button2" />
</children>
</HBox>
</children>
</AnchorPane>
ChildView2.fxml
<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">
<children>
<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" />
</children>
</AnchorPane>
MainController.java
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;
}
}
ChildView1Controller.java
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;
}
}
ChildView2Controller.java
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<Person> tableView;
ObservableList<Person> data ;
public Node getView(){
return view ;
}
@FXML
void initialize() {
System.out.println("ChildView2Controller.initialize()");
//populateTable();
}
public TableView<Person> getTableView(){
return tableView ;
}
}
默認狀態下,Button1的是從ChildView1Controller禁用。
我想在我的其他視圖(ChildView2.fxml)中的錶行被選中時啓用它。當表格行被取消選擇時,再次禁用它。基本上我想綁定按鈕和表,以便在選擇表格行時啓用按鈕。 請幫我用MainViewController代碼綁定按鈕和表格行。
@Crferreira感謝您的輸入。我也添加了我的控制器類。我不知道如何在MainViewConroller中注入childView控制器。我無法從MainViewController訪問childView1Controller或childView2Controller。你能檢查我的代碼在哪裏/哪裏出了問題嗎?如果我嘗試訪問childView1Controller,它會拋出異常。 – Dil
我在最後更新了我的答案。 – Crferreira
@Crferreira多數民衆贊成它。有用。我不知道這樣的事實**「要求是,如果您包含的資源的fx:id是」xxx「,則相應控制器的變量名是xxxController(如此詳細信息 - > detailsController)」** – Dil