我在一個FXML中有一個按鈕,在另一個FXML中有兩個文本字段。這兩個FXML是獨立的,我的意思是它們不是嵌套的。我想要在控制檯/輸出中打印文本(位於兩個文本字段中),只要點擊按鈕即可。下面是fxmls及其控制器:如何從另一個FXML控制器獲取一個Java FXML控制器中的數據(這些控制器不是嵌套控制器)?
Button.fxml
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="textboxandbuttonbinding.ButtonController">
<children>
<Button fx:id="button" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
ButtonController.java
public class ButtonController implements Initializable {
@FXML
private void handleButtonAction(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Text.fxml
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="textboxandbuttonbinding.Sample1111Controller">
<children>
<TextField fx:id="textField1" layoutX="186.0" layoutY="133.0" prefWidth="200.0" promptText="text 1" />
<TextField fx:id="textField2" layoutX="186.0" layoutY="200.0" prefWidth="200.0" promptText="text2" />
</children>
</AnchorPane>
TextController.java
public class TextController implements Initializable {
@FXML
private TextField textField1;
@FXML
private TextField textField2;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
我該如何實現此功能?我已經考慮到這兩個FXML在兩個不同的窗口同時加載。
您可以使用此解決方案:HTTP://計算器。 com/questions/14187963 /傳遞參數-javafx-fxml或這個解決方案http://stackoverflow.com/questions/14511016/how-can-i-use-a-variable-from-another-controller-in-javafx或即使這個解決方案http:// stackoverflow.com/questions/9717852/how-to-pass-object-created-in-fxml-controller1-to-controller2-of-inner-fxml-cont/10718683#10718683 –
您提供的解決方案主要是將數據設置爲另一控制器。我想要的是從另一個FXML獲取數據,特別是從TextField獲取數據。我認爲SnakeDoc給出的解決方案應該可行,但我不確定它爲什麼不起作用。請幫助我完全卡在這裏。 – BungBung