Palcente
Palcente

Reputation: 635

How to set default value for javafx DatePicker in FXML?

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

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

For example:

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

Upvotes: 4

Views: 20542

Answers (5)

venki
venki

Reputation: 61

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

datePicker.setValue(LocalDate.now());

Upvotes: 6

Terry Carter
Terry Carter

Reputation: 300

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

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

Upvotes: 1

Hussein Ahmed
Hussein Ahmed

Reputation: 750

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:

datePicker.setValue(NOW_LOCAL_DATE());

Upvotes: 2

Budi Kurniawan
Budi Kurniawan

Reputation: 41

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.

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

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)

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

Upvotes: 4

Marcus Bleil
Marcus Bleil

Reputation: 149

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

  1. LocalDate has no default constructor
  2. LocalDate has no static valueOf(String) method
  3. javafx.fxml.JavaFXBuilderFactory#getBuilder(LocalDate.class) returns null, meaning there is no builder for LocalDate

Upvotes: 3

Related Questions