我在一个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