JavaFX Tutorial - JavaFX
DatePicker
JavaFX DatePicker enables selection of a day from the given calendar.
The DatePicker control consists of a combo box with a date field and a date
chooser.
JavaFX DatePicker control works with JDK 8 Date-Time API.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/*from ww w. ja v a 2 s.c om*/
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
VBox vbox = new VBox(20);
Scene scene = new Scene(vbox, 400, 400);
[Link](scene);
DatePicker checkInDatePicker = new DatePicker();
[Link]().add(checkInDatePicker);
[Link]();
}
}
The code above generates the following result.
DatePicker Creation
We can create a DatePicker and set a particular date value in the class
constructor.
dateP = new DatePicker([Link](2014, 10, 8));
We can also set a date value using setValue method.
[Link]([Link](2014, 10, 8));
[Link]([Link]());
The following code uses setValue to add more time to the ending DatePicker.
import [Link];
/*from w w w . j ava 2 s.c o m*/
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
VBox vbox = new VBox(20);
Scene scene = new Scene(vbox, 400, 400);
[Link](scene);
DatePicker startDatePicker = new DatePicker();
DatePicker endDatePicker = new DatePicker();
[Link]([Link]());
[Link]([Link]().plusDays(1));
[Link]().add(new Label("Start Date:"));
[Link]().add(startDatePicker);
[Link]().add(new Label("End Date:"));
[Link]().add(endDatePicker);
[Link]();
}
}
The code above generates the following result.
Customizing the Date Picker
We can enable and disable showing the ISO week numbers in the DatePicker
by using its setShowWeekNumbers method.
[Link](true);
By default, the DatePicker uses the date format defined by the system locale
and the ISO calendar system.
import [Link];
import [Link];
/*from www. j av a2 s. c om*/
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
String pattern = "yyyy-MM-dd";
VBox vbox = new VBox(20);
Scene scene = new Scene(vbox, 400, 400);
[Link](scene);
DatePicker checkInDatePicker = new DatePicker();
StringConverter<LocalDate> converter = new StringConverter<LocalDate>() {
DateTimeFormatter dateFormatter = [Link](pattern);
@Override
public String toString(LocalDate date) {
if (date != null) {
return [Link](date);
} else {
return "";
}
}
@Override
public LocalDate fromString(String string) {
if (string != null && ![Link]()) {
return [Link](string, dateFormatter);
} else {
return null;
}
}
};
[Link](converter);
[Link]([Link]());
[Link]().add(checkInDatePicker);
[Link]();
[Link]();
}
}
The code above generates the following result.
DateCell
By default, all the cells in the calendar elements are available for selection. We
can use a day cell factory to disable the cell.
import [Link];
/*from w ww . jav a 2 s .co m*/
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
VBox vbox = new VBox(20);
Scene scene = new Scene(vbox, 400, 400);
[Link](scene);
DatePicker startDatePicker = new DatePicker();
DatePicker endDatePicker = new DatePicker();
[Link]([Link]());
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicke
r, DateCell>() {
@Override
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
[Link](item, empty);
if ([Link]([Link]().plusDays(1))) {
setDisable(true);
setStyle("-fx-background-color: #EEEEEE;");
}
}
};
}
};
[Link](dayCellFactory);
[Link]([Link]().plusDays(1));
[Link]().add(new Label("Start Date:"));
[Link]().add(startDatePicker);
[Link]().add(new Label("End Date:"));
[Link]().add(endDatePicker);
[Link]();
}
}
The code above generates the following result.
Example
Install tooltip for each date cell.
import [Link];
import [Link];
/*from w [Link] v a [Link] m*/
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
VBox vbox = new VBox(20);
Scene scene = new Scene(vbox, 400, 400);
[Link](scene);
final DatePicker startDatePicker = new DatePicker();
DatePicker endDatePicker = new DatePicker();
[Link]([Link]());
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicke
r, DateCell>() {
@Override
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
[Link](item, empty);
long p = [Link]([Link](), item);
setTooltip(new Tooltip("You're about to stay for " + p + " days"));
}
};
}
};
[Link](dayCellFactory);
[Link]([Link]().plusDays(1));
[Link]().add(new Label("Start Date:"));
[Link]().add(startDatePicker);
[Link]().add(new Label("End Date:"));
[Link]().add(endDatePicker);
[Link]();
}
}
The code above generates the following result.