【问题标题】:JavaFX DatePicker returning nullJavaFX DatePicker 返回 null
【发布时间】:2016-04-11 05:05:25
【问题描述】:

所以我查看了问题并找到了this one,这与我的问题相似,但没有解决我的问题,因为我没有在我的 java 文件中初始化我的日期选择器。

我创建了一个日期选择器。它在 fxml 文件中的 fx:id 与其在相应控制器中的名称相匹配。出于某种原因,无论我手动键入一个值还是从日历中选择一个日期,datePicker.getValue() 总是返回 null。谁能想到为什么会发生这种情况?

我的代码与我在此处描述的情况完全相同。我有一个名为 lowEndDate 的 DatePicker。我还没有初始化它。单击按钮后,我想获取它的值,但它返回 null。有谁知道为什么会这样?也许我错过了一步。谢谢。

FXML:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<SplitPane dividerPositions="0.22122571001494767" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="395.0" prefWidth="671.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.UserMainController">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="175.0">
         <children>
            <Button fx:id="logOut" layoutX="35.0" layoutY="261.0" mnemonicParsing="false" onAction="#logOutButtonClicked" text="Log Out">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Label layoutX="52.0" layoutY="44.0" text="user:">
               <font>
                  <Font size="19.0" />
               </font>
            </Label>
            <Label fx:id="username" layoutX="16.0" layoutY="76.0" prefHeight="32.0" prefWidth="116.0" textAlignment="CENTER">
               <font>
                  <Font size="25.0" />
               </font>
            </Label>
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="442.0" prefWidth="457.0">
         <children>
            <ListView fx:id="albumListView" layoutY="-2.0" prefHeight="335.0" prefWidth="292.0" />
            <Button fx:id="deleteButton" layoutX="115.0" layoutY="348.0" mnemonicParsing="false" onAction="#deleteButtonClicked" prefHeight="31.0" prefWidth="68.0" text="Delete">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <AnchorPane layoutX="292.0" prefHeight="335.0" prefWidth="169.0">
               <children>
                  <Label fx:id="albums" layoutX="73.0" layoutY="44.0" text="Albums">
                     <font>
                        <Font size="22.0" />
                     </font>
                  </Label>
                  <Label fx:id="addSuccess" layoutX="99.0" layoutY="212.0" text="ADD SUCCESSFUL" textFill="#2bee34" visible="false" />
                  <Label fx:id="addFail" layoutX="81.0" layoutY="212.0" text="ALBUM EXISTS. TRY AGAIN" textFill="#d01313" visible="false" />
                  <AnchorPane fx:id="createAlbumAnchor" layoutY="7.0" prefHeight="335.0" prefWidth="169.0" visible="false">
                     <children>
                        <Label layoutX="46.0" layoutY="39.0" text="Create Album">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                        <Button fx:id="albumCreate" layoutX="46.0" layoutY="229.0" mnemonicParsing="false" onAction="#OKButtonClicked" text="OK">
                           <font>
                              <Font size="15.0" />
                           </font>
                        </Button>
                        <Button fx:id="back" layoutX="121.0" layoutY="229.0" mnemonicParsing="false" onAction="#backButtonClicked" text="Back">
                           <font>
                              <Font size="15.0" />
                           </font>
                        </Button>
                        <TextField fx:id="newAlbumName" layoutX="38.0" layoutY="115.0" promptText="Album name" />
                        <Label fx:id="mustInputText" layoutX="64.0" layoutY="160.0" text="MUST INPUT TEXT" textFill="#cd1515" visible="false" />
                     </children>
                  </AnchorPane>
                  <AnchorPane fx:id="searchAnchor" layoutX="4.0" layoutY="7.0" prefHeight="321.0" prefWidth="212.0">
                     <children>
                        <Label layoutX="74.0" layoutY="35.0" text="Search">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                        <Label layoutX="76.0" layoutY="67.0" text="Date Range" />
                        <Label layoutX="91.0" layoutY="123.0" text="to" />
                        <Label layoutX="91.0" layoutY="185.0" text="Tags" />
                        <TextArea fx:id="tags" layoutX="24.0" layoutY="208.0" prefHeight="71.0" prefWidth="166.0" promptText="ex: healthy,happy,nyc" />
                        <Button fx:id="searchOK" layoutX="48.0" layoutY="282.0" mnemonicParsing="false" onAction="#searchOKButtonClicked" text="OK" />
                        <Button fx:id="searchBack" layoutX="118.0" layoutY="282.0" mnemonicParsing="false" onAction="#searchBackButtonClicked" text="Back" />
                        <DatePicker fx:id="highEndDate" layoutX="20.0" layoutY="92.0" promptText="dd/mm/yyyy" />**
                        <DatePicker fx:id="lowEndDate" layoutX="20.0" layoutY="148.0" promptText="dd/mm/yyyy" />
                     </children>
                  </AnchorPane>
               </children>
            </AnchorPane>
            <Button fx:id="createButton" layoutX="21.0" layoutY="348.0" mnemonicParsing="false" onAction="#createButtonClicked" text="Create">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button fx:id="searchButton" layoutX="212.0" layoutY="348.0" mnemonicParsing="false" onAction="#searchButtonClicked" text="Search">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
         </children></AnchorPane>
  </items>
</SplitPane>

代码:

public void searchOKButtonClicked(ActionEvent e){
        boolean searchComplete = false;
        User currentUser = LoginController.currentUser;
        if((lowEndDate.getValue() == null) && (highEndDate.getValue() == null) && ((tags == null) || tags.getText().trim().isEmpty())){
            Alert alert = new Alert(AlertType.ERROR);
            alert.setContentText("Oops! At least one field is required to search.");
            alert.show();
        }

}

这里的问题是:lowEndDate 和 highEndDate 始终为空,无论输入如何。

【问题讨论】:

  • 也许如果你包含一些代码,人们可以帮助你。您的 fxml 文件,您的动作侦听器,...您是否尝试过与配置文件链接的页面中包含的“检查 DatePicker 行为的简单项目”?
  • @RubioRic:我添加了一些代码,并更改了我的工作以匹配没有结果的简单项目。
  • 你见过这个问题吗? stackoverflow.com/questions/32346893/… 使用 datePicker.getEditor().getText() 检索在 datepicker 中输入的值
  • @RubioRic:是的,我也试过了。没有任何东西被检索。当我尝试打印字符串时,它是空的(没有打印)

标签: java javafx


【解决方案1】:

Sara,我已经使用这个简单的项目测试了你的 fxml 文件

主要

  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("sample2.fxml"));
         primaryStage.setTitle("Hello World");
         primaryStage.setScene(new Scene(root));
         primaryStage.show();
     }

     public static void main(String[] args) {
        launch(args);
     }
  }

控制器

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

   import javafx.event.ActionEvent;
   import javafx.fxml.FXML;
   import javafx.fxml.Initializable;
   import javafx.scene.control.Alert;
   import javafx.scene.control.Alert.AlertType;
   import javafx.scene.control.DatePicker;

   public class Controller implements Initializable {

       @FXML
       DatePicker lowEndDate;

       public void searchOKButtonClicked(ActionEvent e){

            Alert alert = new Alert(AlertType.ERROR);
            alert.setContentText("Oops! At least one field is required to search. " + " " + lowEndDate.getValue());
            alert.show();
       }

       @Override
       public void initialize(URL arg0, ResourceBundle arg1) {
           // TODO Auto-generated method stub

       }
   }

在您的 fxml 中,我更改了两个日期选择器中的命名,我认为 lowEndDate 必须高于 highEndDate

  <DatePicker fx:id="lowEndDate"  layoutX="20.0" layoutY="92.0"  promptText="dd/mm/yyyy" />
  <DatePicker fx:id="highEndDate" layoutX="20.0" layoutY="148.0" promptText="dd/mm/yyyy" />

这是我介绍日期时得到的结果

也许您的控制器的另一部分有错误,但日期选择器似乎工作正常。

【讨论】:

  • 我会审核并通知您!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多