从 DatePicker 检索日期并移动到另一个 FXML 文件

Retrieve date from DatePicker and move to another FXML file

我有一个关于将 selected 日期从 DatePicker 传递到另一个 FXML 文件的问题。我在类似的线程中看到过代码,但我似乎无法理解它。

基本上我想 select 来自 DatePicker(在 DiscountController 中)的日期,并能够在另一个 FXML 文件(在 ConfirmController 中)中检索该 selected 日期并打印它。这是我到目前为止所做的。

谁能告诉我我在做什么wrong/missing?

public class DiscountController implements Initializable {

    private final static DiscountController instance = new DiscountController();
    public static DiscountController getInstance() {
        return instance;
    }


    @FXML private DatePicker dp;
    @FXML private Label lblDate;

    public DatePicker getDp() {
        return dp;
    }


    //button to print the selected date in a label. This works fine
    @FXML private void ConfirmBtnAction (ActionEvent event) {
          lblDate.setText(dp.getValue().toString());
          }


  //button to move to next FXML screen
  @FXML private void nextBtnAction(ActionEvent event) {

    try{
     Parent parent = FXMLLoader.load(getClass().getResource("/appl/Confirm.fxml"));
     Stage stage = new Stage();
     Scene scene = new Scene(parent);
     stage.setScene(scene);
     stage.show();


     }
     catch(IOException e){
     }
}
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    

}

fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ivikappl.DiscountController">
  <children>
    <Label contentDisplay="LEFT" layoutX="236.0" layoutY="33.0" prefHeight="42.0" prefWidth="133.0" text="Discount">
      <font>
        <Font size="28.0" />
      </font>
    </Label>
    <Button fx:id="btnNext" layoutX="525.0" layoutY="358.0" mnemonicParsing="false" onAction="#nextBtnAction" prefWidth="61.0" text="Next" />
    <Label layoutX="231.0" layoutY="158.0" prefHeight="21.0" prefWidth="69.0" text="Select Date" />
    <Label layoutX="404.0" layoutY="158.0" prefHeight="21.0" prefWidth="101.0" text="Select discount %" />
    <Label layoutX="54.0" layoutY="158.0" prefHeight="21.0" prefWidth="69.0" text="Product" />
      <DatePicker fx:id="dp" layoutX="189.0" layoutY="117.0" />
      <Label fx:id="lblDate" layoutX="219.0" layoutY="247.0" prefHeight="35.0" prefWidth="95.0" />
      <Button fx:id="btnConfirm" layoutX="241.0" layoutY="200.0" mnemonicParsing="false" onAction="#ConfirmBtnAction" text="Confirm" />
      <TextField layoutX="391.0" layoutY="117.0" />
      <Label fx:id="lblProduct" layoutX="22.0" layoutY="121.0" prefHeight="17.0" prefWidth="149.0" />
        </children>
</AnchorPane>

确认控制器文件

public class ConfirmController implements Initializable  {


//button to fetch selected date from DiscountController and print it 
//Having a problem, here, this one doesn't work
@FXML private void btnFetchAction (ActionEvent event) {
   String A;
    A = DiscountController.getInstance().getDp().getValue().toString();
            System.out.println(A);
      }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        }    

}

和 FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ivikappl.ConfirmController">
   <children>
      <Button layoutX="382.0" layoutY="74.0" mnemonicParsing="false" prefHeight="49.0" prefWidth="115.0" text="Generate barcode" textAlignment="CENTER" />
      <Label layoutX="91.0" layoutY="31.0" prefHeight="30.0" prefWidth="79.0" text="Summary">
         <font>
            <Font name="System Bold" size="12.0" />
         </font>
      </Label>
      <Label layoutX="40.0" layoutY="123.0" prefHeight="20.0" prefWidth="85.0" text="Productname">
         <font>
            <Font name="System Italic" size="12.0" />
         </font>
      </Label>
      <Label fx:id="lblDate" layoutX="40.0" layoutY="79.0" prefHeight="20.0" prefWidth="67.0">
         <font>
            <Font name="System Italic" size="12.0" />
         </font>
      </Label>
      <Label layoutX="40.0" layoutY="166.0" prefHeight="20.0" prefWidth="67.0" text="Discount">
         <font>
            <Font name="System Italic" size="12.0" />
         </font>
      </Label>
      <Button fx:id="btnFetch" layoutX="40.0" layoutY="236.0" mnemonicParsing="false" onAction="#btnFetchAction" text="Fetch" />
   </children>
</AnchorPane>

你不能在这里使用单例模式,因为 FXMLLoader 本质上是通过调用无参数构造函数(即不是通过调用 DiscountController.getInstance())来创建 DiscountController 的实例。 (如果你真的想要,你可以走这条路,通过在加载折扣 FXML 文件的 FXMLLoader 上设置一个控制器工厂,但是控制器是单例确实没有意义。在很多情况下你真的需要它们的多个实例。还有更简单的方法可以实现您想要做的事情。)

ConfirmController 中定义一个可以传递日期的方法:

public class ConfirmController {

    @FXML
    private Label lblDate ;

    public void setDate(LocalDate date) {
        lblDate.setText(date.toString());
    }

    // ... other code as before...
}

然后在加载 FXML 时调用它:

public class DiscountController implements Initializable {

    @FXML private DatePicker dp;

    // ...

    @FXML private void nextBtnAction(ActionEvent event) {

        try{
         FXMLLoader loader = new FXMLLoader(getClass().getResource("/appl/Confirm.fxml"));
         Parent parent = loader.load();
         ConfirmController confirmController = loader.getController();
         confirmController.setDate(dp.getValue());
         Stage stage = new Stage();
         Scene scene = new Scene(parent);
         stage.setScene(scene);
         stage.show();


         }
         catch(IOException e){
         }
    }

    // ...
}

请参阅 Passing Parameters JavaFX FXML 了解其他一些方法。