java 如何在 FXML 中为 javafx DatePicker 设置默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31899692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:19:17  来源:igfitidea点击:

How to set default value for javafx DatePicker in FXML?

javadatetimedatepickerjavafx-8

提问by Palcente

Is it possible to initialize default value for DatePicker at FXML level?

是否可以在 FXML 级别初始化 DatePicker 的默认值?

<children>
            <DatePicker fx:id="datePicker" value="2015-07-20"/>
            <Label fx:id="messageLabel" textAlignment="JUSTIFY" />
</children>

Obviously that throws an exception, Is it possibly to call constructor of LocalDate?

显然抛出异常,是否有可能调用LocalDate的构造函数?

For example:

例如:

        <DatePicker fx:id="datePicker" value="LocalDate.of(2015,07,20)"/>

采纳答案by Marcus Bleil

I'm sure you can't populate DatePicker at FXML level, because you can't instantiate an LocalDate object on FXML level because

我确定你不能在 FXML 级别填充 DatePicker,因为你不能在 FXML 级别实例化 LocalDate 对象,因为

  1. LocalDatehas no default constructor
  2. LocalDatehas no static valueOf(String)method
  3. javafx.fxml.JavaFXBuilderFactory#getBuilder(LocalDate.class)returns null, meaning there is no builder for LocalDate
  1. LocalDate没有默认构造函数
  2. LocalDate没有静态valueOf(String)方法
  3. javafx.fxml.JavaFXBuilderFactory#getBuilder(LocalDate.class)返回null,这意味着 LocalDate 没有构建器

回答by Budi Kurniawan

In order to be able to instantiate java.time.LocalDate from an FXML file, you need to create and register a custom builder for the class.

为了能够从 FXML 文件实例化 java.time.LocalDate,您需要为该类创建并注册一个自定义构建器。

  1. Create a builder for LocalDate. A builder must implement javafx.util.Builder and its build method. Here is an example of a builder that takes three parameters (year, month, dayOfMonth):

    package util;
    import java.time.LocalDate;
    import javafx.util.Builder;
    
    public class LocalDateBuilder implements Builder<LocalDate>{
    
        private int year;
        private int month;
        private int dayOfMonth;
    
        @Override
        public LocalDate build() {
            return LocalDate.of(year, month, dayOfMonth);
        }
    
        public int getYear() {
            return year;
        }
        public void setYear(int year) {
            this.year = year;
        }
        public int getMonth() {
            return month;
        }
        public void setMonth(int month) {
            this.month = month;
        }
        public int getDayOfMonth() {
            return dayOfMonth;
        }
        public void setDayOfMonth(int dayOfMonth) {
            this.dayOfMonth = dayOfMonth;
        }
    }
    
  2. Create a FactoryBuilder wrapper that decorates the default FactoryBuilder so that it will return your builder when a LocalDate builder is needed. Here is a sample FactoryBuilderWrapper class.

    package util;
    import java.time.LocalDate;
    import javafx.util.Builder;
    import javafx.util.BuilderFactory;
    
    public class BuilderFactoryWrapper implements BuilderFactory {
    
        private BuilderFactory builderFactory;
        public BuilderFactoryWrapper(BuilderFactory builderFactory) {
            this.builderFactory = builderFactory;
        }
    
        @Override
        public Builder<?> getBuilder(Class<?> klass) {
            if (klass == LocalDate.class) {
                // return our builder
                return new LocalDateBuilder();
            } else {
                // return the default builder
                return builderFactory.getBuilder(klass);
            }
        }
    }
    
  3. In your application's start method, obtain the default FactoryBuilder, wrap it with your wrapper and reassign it to the FXMLLoader:

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("myUI.fxml"));
        BuilderFactory builderFactory = loader.getBuilderFactory();
        // wrap the default BuilderFactory
        loader.setBuilderFactory(new    
                BuilderFactoryWrapper(builderFactory));
    
        Parent root = loader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    
  1. 为 LocalDate 创建一个构建器。构建器必须实现 javafx.util.Builder 及其构建方法。下面是一个带有三个参数(年、月、dayOfMonth)的构建器示例:

    package util;
    import java.time.LocalDate;
    import javafx.util.Builder;
    
    public class LocalDateBuilder implements Builder<LocalDate>{
    
        private int year;
        private int month;
        private int dayOfMonth;
    
        @Override
        public LocalDate build() {
            return LocalDate.of(year, month, dayOfMonth);
        }
    
        public int getYear() {
            return year;
        }
        public void setYear(int year) {
            this.year = year;
        }
        public int getMonth() {
            return month;
        }
        public void setMonth(int month) {
            this.month = month;
        }
        public int getDayOfMonth() {
            return dayOfMonth;
        }
        public void setDayOfMonth(int dayOfMonth) {
            this.dayOfMonth = dayOfMonth;
        }
    }
    
  2. 创建一个装饰默认 FactoryBuilder 的 FactoryBuilder 包装器,以便在需要 LocalDate 构建器时返回您的构建器。这是一个示例 FactoryBuilderWrapper 类。

    package util;
    import java.time.LocalDate;
    import javafx.util.Builder;
    import javafx.util.BuilderFactory;
    
    public class BuilderFactoryWrapper implements BuilderFactory {
    
        private BuilderFactory builderFactory;
        public BuilderFactoryWrapper(BuilderFactory builderFactory) {
            this.builderFactory = builderFactory;
        }
    
        @Override
        public Builder<?> getBuilder(Class<?> klass) {
            if (klass == LocalDate.class) {
                // return our builder
                return new LocalDateBuilder();
            } else {
                // return the default builder
                return builderFactory.getBuilder(klass);
            }
        }
    }
    
  3. 在您的应用程序的 start 方法中,获取默认的 FactoryBuilder,用您的包装器包装它并将其重新分配给 FXMLLoader:

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("myUI.fxml"));
        BuilderFactory builderFactory = loader.getBuilderFactory();
        // wrap the default BuilderFactory
        loader.setBuilderFactory(new    
                BuilderFactoryWrapper(builderFactory));
    
        Parent root = loader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    

The following is FXML code to create a Person with a birthDate property of type LocalDate: (You need to import java.time.LocalDate, but not the builder itself)

以下是创建具有 LocalDate 类型的birthDate 属性的Person 的FXML 代码:(您需要导入java.time.LocalDate,但不是构建器本身)

<?import model.Person?>
<?import java.time.LocalDate?>
...
<Person firstName="Jane" lastName="Citizen">
    <birthDate>
        <LocalDate year="1980" month="12" dayOfMonth="19"/>
    </birthDate>
</Person>

回答by Hussein Saied

This method returns the current date:-

此方法返回当前日期:-

// Date Now  ### "To Date Picker"
    public static final LocalDate NOW_LOCAL_DATE (){
        String date = new SimpleDateFormat("dd-MM-  yyyy").format(Calendar.getInstance().getTime());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        LocalDate localDate = LocalDate.parse(date , formatter);
        return localDate;
    }

Then call it inside initialize method:

然后在 initialize 方法中调用它:

datePicker.setValue(NOW_LOCAL_DATE());

回答by Terry Carter

In your controller you can do something like this if you are implementing Initializable:

在您的控制器中,如果您正在实现 Initializable,您可以执行以下操作:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy", this.locale);
birthDate.setValue(LocalDate.parse(formatter.format(LocalDate.now())));

回答by venki

You can set default value of the date to today through controller. Following line worked for me.

您可以通过控制器将日期的默认值设置为今天。以下线路对我有用。

datePicker.setValue(LocalDate.now());