2016-04-11 55 views
1

所以我查看了问题,发现this one,这与我的问题类似,但没有解决我的问题,因为我没有在我的java文件中初始化我的日期选择器。JavaFX DatePicker返回null

我创建了一个日期选择器。其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总是空,无论输入的。

+0

也许如果你包含一些代码,人们可以帮助你。您的fxml文件,您的动作侦听器,...您是否尝试过使用您的配置文件链接的页面中包含的“简单项目来检查DatePicker的行为”? – RubioRic

+0

@RubioRic:我附加了一些代码,并且改变了我的工作以匹配没有结果的简单项目。 – Sara

+0

你见过这个问题吗? http://stackoverflow.com/questions/32346893/javafx-datepicker-not-updating-value那里在日期选择器中输入的值使用datePicker.getEditor()检索。getText() – RubioRic

回答

1

萨拉,我测试过用这个简单的项目

主要的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,我已经改变了命名在两datepickers,我认为lowEndDate必须高于EndDate

<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" /> 

这是我得到的结果,当我介绍了一个日期

enter image description here

enter image description here

也许你在你的控制器的另一部分有错误,但datepickers似乎很好地工作。

+0

我会检查这个并让你知道! – Sara