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 application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating date picker");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// create a date picker       ÂDatePicker d =newDatePicker();       Â// add button and label       Âr.getChildren().add(d);       Â// create a scene       ÂScene sc =newScene(r,200,200);       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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 application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating date picker");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// label to show the date       ÂLabel l =newLabel("no date selected");       Â// create a date picker       ÂDatePicker d =newDatePicker();       Â// action event       ÂEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {           Âpublicvoidhandle(ActionEvent e)           Â{               Â// get the date picker value               ÂLocalDate i = d.getValue();               Â// get the selected date               Âl.setText("Date :"+ i);           Â}       Â};       Â// show week numbers       Âd.setShowWeekNumbers(true);       Â// when datePicker is pressed       Âd.setOnAction(event);       Â// add button and label       Âr.getChildren().add(d);       Âr.getChildren().add(l);       Â// create a scene       ÂScene sc =newScene(r,200,200);       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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

