JavaFX | DatePicker with examples

DatePicker is a part of the JavaFX package, DatePicker allows to select the date from the popup calendar or type the text manually in the text field of date-picker.
Constructor of the DatePicker class are :
- DatePicker():Creates a default DatePicker instance with a null date value set.
- DatePicker(LocalDate l):Creates a DatePicker instance and sets the value to the given date.
Commonly used methods:
| method | explanation |
|---|---|
| getChronology() | Gets the value of the property chronology. |
| getEditor() | returns the text editor of the date picker |
| isShowWeekNumbers() | returns whether the week number is shown or not |
| setChronology(Chronology v) | Sets the value of the property chronology. |
| setShowWeekNumbers(boolean v) | sets the date picker to show week number if true value is passed as argument |
Below programs illustrate the DatePicker Class:
- Program to create a date picker and display it in stage: This program creates a Date Picker indicated by the name d. The DatePicker will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button inside the scene. Finally, the show() method is called to display the final results.
// Java Program to create a date picker// and display it in stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclassdate_picker_1extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating date picker");// create a tile paneTilePane r =newTilePane();// create a date pickerDatePicker d =newDatePicker();// add button and labelr.getChildren().add(d);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Program to create a date picker and create a label to show the date: This program creates a DatePicker indicated by the name d. The Date Picker will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show which date is choosed. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the date picker events. The event handler would be added to the button using setOnAction() function.The setShowWeekNumbers() will set the date picker to show week number of respective weeks.
// Java Program to create a date picker// and create a label to show the dateimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.*;importjava.time.chrono.*;publicclassdate_picker_2extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating date picker");// create a tile paneTilePane r =newTilePane();// label to show the dateLabel l =newLabel("no date selected");// create a date pickerDatePicker d =newDatePicker();// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the date picker valueLocalDate i = d.getValue();// get the selected datel.setText("Date :"+ i);}};// show week numbersd.setShowWeekNumbers(true);// when datePicker is pressedd.setOnAction(event);// add button and labelr.getChildren().add(d);r.getChildren().add(l);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note : The above programs might not run in an online IDE please use an offline IDE.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/DatePicker.html




