JavaFX DatePicker Tutorial with Examples
1. JavaFX DatePicker
// Setting a particular date value in the class constructor
checkInDatePicker = new DatePicker(LocalDate.of(1998, 10, 8));
// Setting a particular date value by using the setValue method
checkInDatePicker.setValue(LocalDate.of(1998, 10, 8));
// Setting the minimum date available in the calendar
checkInDatePicker.setValue(LocalDate.MIN);
// Setting the maximum date available in the calendar
checkInDatePicker.setValue(LocalDate.MAX);
// Setting the current date
checkInDatePicker.setValue(LocalDate.now());
setShowWeekNumber
2. DatePicker Example
DatePickerDemo.java
package org.o7planning.javafx.datapicker;
import java.time.LocalDate;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class DatePickerDemo extends Application {
@Override
public void start(Stage stage) {
DatePicker datePicker = new DatePicker();
datePicker.setValue(LocalDate.of(2016, 7, 25));
datePicker.setShowWeekNumbers(true);
FlowPane root = new FlowPane();
root.getChildren().add(datePicker);
root.setPadding(new Insets(10));
stage.setTitle("DatePicker (o7planning.org)");
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Running the example:
3. Customize DatePicker
By default, the days displayed on Date Field are formatted according to Local locale and ISO standards. This means the days will display based on the computer configuration running your application.
On the Java code you can customize the format of 'Date Field' via setConverter(..) method, and use setConverter(null) method if you want to return to the default format.
The following example customize display on "Date Field" in the format of "dd-MM-yyyy":
DatePickerConverterDemo.java
package org.o7planning.javafx.datapicker;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class DatePickerConverterDemo extends Application {
@Override
public void start(Stage stage) {
DatePicker datePicker = new DatePicker();
datePicker.setValue(LocalDate.of(2016, 7, 25));
datePicker.setShowWeekNumbers(true);
// Converter
StringConverter<LocalDate> converter = new StringConverter<LocalDate>() {
DateTimeFormatter dateFormatter =
DateTimeFormatter.ofPattern("dd-MM-yyyy");
@Override
public String toString(LocalDate date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return "";
}
}
@Override
public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateFormatter);
} else {
return null;
}
}
};
datePicker.setConverter(converter);
datePicker.setPromptText("dd-MM-yyyy");
FlowPane root = new FlowPane();
root.getChildren().add(datePicker);
root.setPadding(new Insets(10));
stage.setTitle("DatePicker (o7planning.org)");
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
4. Disable some days
By default, you can interact with all of days on DatePicker. However, you also can customize the DatePicker in order to disable some days by using the setDayCellFactory method.
The following example will disable all Monday, Tuesday, Wednesday in month.
DatePickerConverterDemo.java
package org.o7planning.javafx.datapicker;
import java.time.DayOfWeek;
import java.time.LocalDate;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class DatePickerConverterDemo extends Application {
// Factory to create Cell of DatePicker
private Callback<DatePicker, DateCell> getDayCellFactory() {
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
@Override
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
// Disable Monday, Tueday, Wednesday.
if (item.getDayOfWeek() == DayOfWeek.MONDAY //
|| item.getDayOfWeek() == DayOfWeek.TUESDAY //
|| item.getDayOfWeek() == DayOfWeek.WEDNESDAY) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
}
};
}
};
return dayCellFactory;
}
@Override
public void start(Stage stage) {
DatePicker datePicker = new DatePicker();
datePicker.setValue(LocalDate.of(2016, 7, 25));
datePicker.setShowWeekNumbers(true);
// Factory to create Cell of DatePicker
Callback<DatePicker, DateCell> dayCellFactory= this.getDayCellFactory();
datePicker.setDayCellFactory(dayCellFactory);
FlowPane root = new FlowPane();
root.getChildren().add(datePicker);
root.setPadding(new Insets(10));
stage.setTitle("DatePicker (o7planning.org)");
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
JavaFX Tutorials
- Open a new Window in JavaFX
- JavaFX ChoiceDialog Tutorial with Examples
- JavaFX Alert Dialogs Tutorial with Examples
- JavaFX TextInputDialog Tutorial with Examples
- Install e(fx)clipse for Eclipse (JavaFX Tooling)
- Install JavaFX Scene Builder for Eclipse
- JavaFX Tutorial for Beginners - Hello JavaFX
- JavaFX FlowPane Layout Tutorial with Examples
- JavaFX TilePane Layout Tutorial with Examples
- JavaFX HBox, VBox Layout Tutorial with Examples
- JavaFX BorderPane Layout Tutorial with Examples
- JavaFX AnchorPane Layout Tutorial with Examples
- JavaFX TitledPane Tutorial with Examples
- JavaFX Accordion Tutorial with Examples
- JavaFX ListView Tutorial with Examples
- JavaFX Group Tutorial with Examples
- JavaFX ComboBox Tutorial with Examples
- JavaFX Transformations Tutorial with Examples
- JavaFX Effects Tutorial with Examples
- JavaFX GridPane Layout Tutorial with Examples
- JavaFX StackPane Layout Tutorial with Examples
- JavaFX ScrollPane Tutorial with Examples
- JavaFX WebView and WebEngine Tutorial with Examples
- JavaFX HTMLEditor Tutorial with Examples
- JavaFX TableView Tutorial with Examples
- JavaFX TreeView Tutorial with Examples
- JavaFX TreeTableView Tutorial with Examples
- JavaFX Menu Tutorial with Examples
- JavaFX ContextMenu Tutorial with Examples
- JavaFX Image and ImageView Tutorial with Examples
- JavaFX Label Tutorial with Examples
- JavaFX Hyperlink Tutorial with Examples
- JavaFX Button Tutorial with Examples
- JavaFX ToggleButton Tutorial with Examples
- JavaFX RadioButton Tutorial with Examples
- JavaFX MenuButton and SplitMenuButton Tutorial with Examples
- JavaFX TextField Tutorial with Examples
- JavaFX PasswordField Tutorial with Examples
- JavaFX TextArea Tutorial with Examples
- JavaFX Slider Tutorial with Examples
- JavaFX Spinner Tutorial with Examples
- JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
- JavaFX ChoiceBox Tutorial with Examples
- JavaFX Tooltip Tutorial with Examples
- JavaFX DatePicker Tutorial with Examples
- JavaFX ColorPicker Tutorial with Examples
- JavaFX FileChooser and DirectoryChooser Tutorial with Examples
- JavaFX PieChart Tutorial with Examples
- JavaFX AreaChart and StackedAreaChart Tutorial with Examples
- JavaFX BarChart and StackedBarChart Tutorial with Examples
- JavaFX Line Tutorial with Examples
- JavaFX Rectangle and Ellipse Tutorial with Examples
Show More