Reputation: 279
I'm using a datepicker object and having some trouble trying to disable specific days of the month.
I'd like to be able to disable every date except the first of the month, and on another datepicker I'd like to be able to disable all days except the first day of every week (monday), how is this done?
Upvotes: 2
Views: 901
Reputation: 16508
You can customize the DatePicker by using the DatePicker.setDayCellFactory
method. In order to disable some days you could do something like:
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 DatePickerDemo extends Application {
// Factory to create Cell of DatePicker
private Callback<DatePicker, DateCell> disableMonday() {
final Callback<DatePicker, DateCell> dayCellFactory = (final DatePicker datePicker) -> new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
// Disable Monday
if (item.getDayOfWeek() == DayOfWeek.MONDAY) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
}
};
return dayCellFactory;
}
private Callback<DatePicker, DateCell> disableNotFirstOfMonth() {
final Callback<DatePicker, DateCell> dayCellFactory = (final DatePicker datePicker) -> new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
// Disable all except first of month
if (item.getDayOfMonth() != 1) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
}
};
return dayCellFactory;
}
@Override
public void start(Stage stage) {
DatePicker datePicker = new DatePicker();
datePicker.setValue(LocalDate.now());
datePicker.setShowWeekNumbers(true);
DatePicker datePicker2 = new DatePicker();
datePicker2.setValue(LocalDate.now());
datePicker2.setShowWeekNumbers(true);
Callback<DatePicker, DateCell> dayCellFactory = this.disableMonday();
Callback<DatePicker, DateCell> dayCellFactory2 = this.disableNotFirstOfMonth();
datePicker.setDayCellFactory(dayCellFactory);
datePicker2.setDayCellFactory(dayCellFactory2);
FlowPane root = new FlowPane();
root.getChildren().add(datePicker);
root.getChildren().add(datePicker2);
root.setPadding(new Insets(20));
stage.setTitle("DatePicker Demo");
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Upvotes: 1