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 stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.control.Alert.AlertType;
import
java.time.LocalDate;
public
class
date_picker_1
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating date picker"
);
// create a tile pane
TilePane r =
new
TilePane();
// create a date picker
DatePicker d =
new
DatePicker();
// add button and label
r.getChildren().add(d);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(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 date
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.control.Alert.AlertType;
import
java.time.*;
import
java.time.chrono.*;
public
class
date_picker_2
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating date picker"
);
// create a tile pane
TilePane r =
new
TilePane();
// label to show the date
Label l =
new
Label(
"no date selected"
);
// create a date picker
DatePicker d =
new
DatePicker();
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(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 =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(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