JavaFX-DatePicker始终返回null

JavaFX-DatePicker始终返回null,java,javafx,datepicker,javafx-8,Java,Javafx,Datepicker,Javafx 8,日期选择器在更改日期后未更新 我在寻找答案,我发现了这个话题: ,但它不起作用 DatePicker的我的侦听器: public void datePickerListener() { this.datePicker = new DatePicker(); this.datePicker.setShowWeekNumbers(false); this.datePicker.setOnAction(event -> { LocalDate date =

日期选择器在更改日期后未更新

我在寻找答案,我发现了这个话题: ,但它不起作用

DatePicker的我的侦听器:

public void datePickerListener() {
    this.datePicker = new DatePicker();
    this.datePicker.setShowWeekNumbers(false);
    this.datePicker.setOnAction(event -> {
        LocalDate date = this.datePicker.getValue();
        System.out.println("Selected date: " + date);
    });
}
此外,我尝试在没有侦听器的情况下获取数据,如:

public void searchAvailableAppointments() {
    LocalDate date;
    String date2;
    date = this.datePicker.getValue();
    date2 = this.datePicker.getEditor().getText();

    System.out.println(date);
    System.out.println(date2);
}
也许有人有类似的问题,并找到了解决办法

EDIT1:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;

import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    DatePicker datePicker;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.datePicker = new DatePicker();
    }

    public void pressButton() {
        LocalDate date;
        date = datePicker.getValue();
        System.out.println(date);
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="142.0" prefWidth="504.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
   <DatePicker fx:id="datePicker" layoutX="65.0" layoutY="58.0" prefHeight="25.0" prefWidth="295.0" />
      <Button layoutX="387.0" layoutY="59.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="25.0" prefWidth="80.0" text="Print result" />
</children>
</AnchorPane>
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
我创建了一个简单的项目来检查DatePicker的行为:

public void datePickerListener() {
    this.datePicker = new DatePicker();
    this.datePicker.setShowWeekNumbers(false);
    this.datePicker.setOnAction(event -> {
        LocalDate date = this.datePicker.getValue();
        System.out.println("Selected date: " + date);
    });
}
控制器类:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;

import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    DatePicker datePicker;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.datePicker = new DatePicker();
    }

    public void pressButton() {
        LocalDate date;
        date = datePicker.getValue();
        System.out.println(date);
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="142.0" prefWidth="504.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
   <DatePicker fx:id="datePicker" layoutX="65.0" layoutY="58.0" prefHeight="25.0" prefWidth="295.0" />
      <Button layoutX="387.0" layoutY="59.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="25.0" prefWidth="80.0" text="Print result" />
</children>
</AnchorPane>
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
FXML文件:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;

import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    DatePicker datePicker;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.datePicker = new DatePicker();
    }

    public void pressButton() {
        LocalDate date;
        date = datePicker.getValue();
        System.out.println(date);
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="142.0" prefWidth="504.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
   <DatePicker fx:id="datePicker" layoutX="65.0" layoutY="58.0" prefHeight="25.0" prefWidth="295.0" />
      <Button layoutX="387.0" layoutY="59.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="25.0" prefWidth="80.0" text="Print result" />
</children>
</AnchorPane>
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

但是我仍然不知道如何使它工作。

您不应该在
initialize()
中重新初始化日期选择器。当FXMLLoader加载fxml时,它会实例化所有字段并将它们注入到引用中,这些引用用
@fxml
注释

如果重新初始化该字段,对视图上呈现的原始对象的引用将丢失,并且您正在使用对新创建对象的引用,因此从该对象获取的值将始终为
null

@Override
public void initialize(URL location, ResourceBundle resources) {
    // this.datePicker = new DatePicker(); <-- Should not exist
}
@覆盖
公共void初始化(URL位置、ResourceBundle资源){

//this.datePicker=new datePicker();你说得完全正确!我没想到问题的解决这么简单,非常感谢!)