我有一个关于将选定日期从DatePicker传递到另一个FXML文件的问题。我已经看到类似线程中的代码,但我似乎无法理解它。
基本上我想从DatePicker(在DiscountController中)中选择一个日期,并能够在另一个FXML文件(在ConfirmController中)中检索所选日期并打印它。这是我到目前为止所做的。
任何人都可以告诉我这里做错了什么/在这里失踪了吗?
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>
ConfirmController文件
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>
答案 0 :(得分:1)
你不能在这里使用单例模式,因为FXMLLoader
本质上是通过调用无参数构造函数创建一个DiscountController
的实例(即不是通过调用DiscountController.getInstance()
)。 (如果你真的想要的话,你可以通过设置加载折扣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。