JavaFX - DatePicker



The Date picker is a control that allows the user to select a date from a graphical calendar. It can be customized to display different date formats, restrict the range of selectable dates, and handle various events related to date selection. The figure below shows a typical datepicker −

JavaFX DatePicker

Creating DatePicker in JavaFX

In JavaFX, the the date picker is represented by a class named DatePicker. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a date picker in JavaFX. Constructors of the DatePicker class are listed below −

  • DatePicker() − It is the default constructor that constructs a date picker without any pre-defined date.

  • DatePicker(LocalDate date) − It creates a new date picker with the specified date.

We can use any of the above mentioned constructors to embed a DatePicker within JavaFX application. The most freuently used is the default constructor of the DatePicker class which does not accept any arguments. Once we instantiate this class, our next step would be setting the initial date value. Following that, instantiate any layout pane to hold the DatePicker. Lastly, set the scene and stage to display the date picker on the screen.

Example

The following JavaFX program demonstrates how to create a datepicker in a JavaFX application. Save this code in a file with the name DatepickerDemo.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.time.LocalDate;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.geometry.Pos;
public class DatepickerDemo extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label label = new Label("Please Enter preferred Date: ");
      // Create a DatePicker and set its initial value
      DatePicker datePicker = new DatePicker();
      datePicker.setValue(LocalDate.of(2020, 1, 1));
      // Create a Label to display the selection
      Label selectLabel = new Label();
      selectLabel.setTextFill(Color.RED);
      // Add a listener to the value property of the DatePicker
      datePicker.valueProperty().addListener((observable, oldValue, newValue) -> {
         // Print the selected date
         selectLabel.setText("You selected: " + newValue);
      });
      // Creating a VBox to hold all controls
      VBox root = new VBox();
      root.setAlignment(Pos.CENTER); 
      root.setSpacing(10);
      root.getChildren().addAll(label, datePicker, selectLabel);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(root, 400, 300);
      stage.setTitle("DatePicker in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls DatepickerDemo.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DatepickerDemo

Output

When we execute the above code, it will generate the following output.

DatePicker Output

Showing week numbers in the Date Picker

By default, the DatePicker does not show week numbers in the calendar. However, if there is a need to display week numbers along with the date, then we can set the setShowWeekNumbers() method to true as shown in the next example. Save this code in a file with the name DatepickerDemo.java.

Example

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.time.LocalDate;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.geometry.Pos;
public class DatepickerDemo extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label label = new Label("Please Enter preferred Date: ");
      // Create a DatePicker and set its initial value
      DatePicker datePicker = new DatePicker();
      datePicker.setValue(LocalDate.of(2024, 1, 1));
      datePicker.setShowWeekNumbers(true);
      // Create a Label to display the selection
      Label selectLabel = new Label();
      selectLabel.setTextFill(Color.RED);
      // Add a listener to the value property of the DatePicker
      datePicker.valueProperty().addListener((observable, oldValue, newValue) -> {
         // Print the selected date
         selectLabel.setText("You selected: " + newValue);
      });
      // Creating a VBox to hold all controls
      VBox root = new VBox();
      root.setAlignment( Pos.BASELINE_CENTER); 
      root.setSpacing(10);
      root.getChildren().addAll(label, datePicker, selectLabel);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(root, 400, 300);
      stage.setTitle("DatePicker in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt using the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls DatepickerDemo.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DatepickerDemo

Output

On executing, the above program generates a JavaFX window displaying a DatePicker with the week numbers as shown below −

DatePicker Output2
Advertisements