JavaFX-从弹出窗口的控制器传递信息给调用控制器

时间:2019-03-03 14:50:35

标签: java javafx

一段时间以来,我一直在努力使某些事情起作用,但是进展并不顺利。我对GUI相对较新,并且正在使用IntelliJ和Gluon SceneBuilder在JavaFX中进行尝试。这就是我想做的:

当我在原始窗口中单击Button时,会出现一个弹出窗口。这本质上是一个新窗口,如果不寻常,请告诉我。在此弹出窗口中,可以输入三个TextField。当我单击弹出窗口上的“确认” Button时,弹出窗口必须关闭并告诉调用它的控制器在TextField中输入的内容。

我希望我提供了足够的信息。我整天都在搜寻,并问了一些经验丰富的程序员,但无济于事。

现在,这是相对代码(对不起,荷兰语):

基本控制器

public class Controller implements Initializable{

    public Controller(){
        people = new ArrayList<>();
        casks = new ArrayList<>();
    }

    public void startNewCask(ActionEvent actionEvent) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        Parent root = loader.load(getClass().getResource("../view/NewCask.fxml"));
        Stage stage = new Stage();
        stage.setTitle("New cask");
        Scene scene = new Scene(root, 300, 275);
        stage.setScene(scene);
        stage.show();
    }
}

弹出窗口控制器

public class NewCaskController {
    @FXML
    private TextField price;
    @FXML
    private TextField volume;
    @FXML
    private TextField type;
    @FXML
    private Button confirm;

    @FXML
    public void handleConfirm(ActionEvent event) {
        Stage stage = (Stage) confirm.getScene().getWindow();
        stage.close();
    }

弹出窗口的JavaFX

<AnchorPane maxHeight="305.0" maxWidth="260.0" minHeight="230.0" minWidth="260.0" prefHeight="246.0" prefWidth="260.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.NewCaskController">
   <children>
      <Label layoutX="12.0" layoutY="14.0" text="Klik op het kruisje om te annuleren." />
      <Label layoutX="12.0" layoutY="35.0" text="Prijs nieuw fust (in euro's zonder €):" />
      <TextField fx:id="price" layoutX="12.0" layoutY="56.0" promptText="Prijs" />
      <Label layoutX="12.0" layoutY="87.0" text="Volume (in milliliter):" />
      <TextField fx:id="volume" layoutX="12.0" layoutY="108.0" promptText="Volume" />
      <Label layoutX="12.0" layoutY="139.0" text="Type" />
      <TextField fx:id="type" layoutX="12.0" layoutY="160.0" promptText="Type" />
      <Button fx:id="confirm" layoutX="12.0" layoutY="201.0" mnemonicParsing="false" onAction="#handleConfirm" text="Bevestig" />
   </children>
</AnchorPane>

原始窗口的JavaFX

<SplitPane dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <Label layoutX="14.0" layoutY="14.0" text="Huidig fust" />
            <Label layoutX="14.0" layoutY="58.0" text="Biersoort: " />
            <ProgressBar fx:id="caskProgress" layoutX="14.0" layoutY="35.0" prefWidth="200.0" progress="0.5" AnchorPane.leftAnchor="12.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="35.0" />
            <Label fx:id="beerTypeLabel" layoutX="81.0" layoutY="58.0" text="404 BIER NIET HIER" />
            <Button layoutX="12.0" layoutY="82.0" mnemonicParsing="false" onAction="#startNewCask" text="Nieuw fust" />
            <Button layoutX="113.0" layoutY="82.0" mnemonicParsing="false" text="Nieuwe gebruiker" />
            <Button layoutX="261.0" layoutY="82.0" mnemonicParsing="false" text="Statistieken" visible="false" />
         </children></AnchorPane>
</items>
   <padding>
      <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
   </padding>
</SplitPane>

1 个答案:

答案 0 :(得分:0)

对于弹出窗口,最好使用Dialog而不是使用全新的JavaFX窗口。感谢@Zephyr引起了我的注意。