JavaFX - DatePicker总是返回null [英] JavaFX - DatePicker always returns null

查看:133
本文介绍了JavaFX - DatePicker总是返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找答案,我发现这个主题:
JavaFX datepicker不更新值
,但不起作用。



我的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:



我已经创建了简单的项目来检查DatePicker的行为:



控制器类:

  

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);
}
}

FXML文件 / p>

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.layout.AnchorPane?>
< AnchorPane maxHeight = - 无限远maxWidth = - 无限minHeight = - 无限远minWidth = - 无限远prefHeight =142.0prefWidth =504.0xmlns =http:// javafx。 com / javafx / 8xmlns:fx =http://javafx.com/fxml/1fx:controller =sample.Controller>
< children>
< DatePicker fx:id =datePickerlayoutX =65.0layoutY =58.0prefHeight =25.0prefWidth =295.0/>
< Button layoutX =387.0layoutY =59.0mnemonicParsing =falseonAction =#pressButtonprefHeight =25.0prefWidth =80.0text =打印结果/>
< / children>
< / AnchorPane>

主类:

 包样本; 

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异常{
父根= 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(); < - 不应该存在
}


DatePicker is not updating after change date.

I was looking for an answer and i found this topic: JavaFX datepicker not updating value , but it doesn't work.

My listener for 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);
    });
}

Additionally I tried get data without listener like:

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

    System.out.println(date);
    System.out.println(date2);
}

Maybe anyone had similar problem and found workaround?

EDIT1:

I have created simple project to check behaviour of DatePicker:

Controller class:

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);
    }
}

FXML file:

<?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>

Main class:

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);
    }
}

But I have still no idea how to make it work.

解决方案

You should not re-initialize the DatePicker inside the initialize(). When FXMLLoader loads the fxml, it instantiates all the fields and injects them into the references which are annotated with @FXML.

If you re-initialize the field, the reference to the original object which is rendered on the view is lost and you are using the reference to the newly created object, therefore the value fetched from this object will always be null.

@Override
public void initialize(URL location, ResourceBundle resources) {
    // this.datePicker = new DatePicker(); <-- Should not exist
}

这篇关于JavaFX - DatePicker总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆