Javafx datepicker how to set up

I have a simple date picker code that turns off all dates that are earlier than the selected ones, but I also need to turn off other dates (e.g. 10/17/2014 to 10/19/2014). How can I change it so that certain dates are disabled as well?

public class DatePickerSample extends application {

private Stage stage;
private DatePicker checkInDatePicker;
private DatePicker checkOutDatePicker;

public static void main(String[] args) {
    Locale.setDefault(Locale.US);                  
    launch(args);
}

@Override
public void start(Stage stage) {
    this.stage = stage;
    stage.setTitle("DatePickerSample ");
    initUI();
    stage.show();
}

private void initUI() {
    VBox vbox = new VBox(20);
    vbox.setStyle("-fx-padding: 10;");
    Scene scene = new Scene(vbox, 400, 400);
    stage.setScene(scene);
    checkInDatePicker = new DatePicker();
    checkOutDatePicker = new DatePicker();
    checkInDatePicker.setValue(LocalDate.now());
    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);
                        if (item.isBefore(
                                checkInDatePicker.getValue().plusDays(1))
                            ) {
                                setDisable(true);
                                setStyle("-fx-background-color: #ffc0cb;");
                        }
                        long p = ChronoUnit.DAYS.between(
                                checkInDatePicker.getValue(), item
                        );
                        setTooltip(new Tooltip(
                            "You're about to stay for " + p + " days")
                        );
                }
            };
        }
    };
    checkOutDatePicker.setDayCellFactory(dayCellFactory);
    checkOutDatePicker.setValue(checkInDatePicker.getValue().plusDays(1));
    GridPane gridPane = new GridPane();

    gridPane.setHgap(10);
    gridPane.setVgap(10);
    Label checkInlabel = new Label("Check-In Date:");
    gridPane.add(checkInlabel, 0, 0);
    GridPane.setHalignment(checkInlabel, HPos.LEFT);
    gridPane.add(checkInDatePicker, 0, 1);
    Label checkOutlabel = new Label("Check-Out Date:");
    gridPane.add(checkOutlabel, 0, 2);
    GridPane.setHalignment(checkOutlabel, HPos.LEFT);
    gridPane.add(checkOutDatePicker, 0, 3);
    vbox.getChildren().add(gridPane);
}

      

}

+3


source to share


1 answer


If you want to disable multiple date ranges, you can create this POJO:

class DisabledRange {

    private final LocalDate initialDate;
    private final LocalDate endDate;

    public DisabledRange(LocalDate initialDate, LocalDate endDate){
        this.initialDate=initialDate;
        this.endDate = endDate;
    }

    public LocalDate getInitialDate() { return initialDate; }
    public LocalDate getEndDate() { return endDate; }

}

      

And now you can define a set of ranges to disable in your calendar. For example:



private final ObservableList<DisabledRange> rangesToDisable = 
    FXCollections.observableArrayList(
        new DisabledRange(LocalDate.of(2014,10,17), LocalDate.of(2014,10,19)),
        new DisabledRange(LocalDate.of(2014,10,27), LocalDate.of(2014,10,29)));

      

Finally, you just need to check Callback

if item

is in any of these ranges:

@Override
public void updateItem(LocalDate item, boolean empty) {
    super.updateItem(item, empty);

    boolean disable = rangesToDisable.stream()
            .filter(r->r.initialDate.minusDays(1).isBefore(item))
            .filter(r->r.endDate.plusDays(1).isAfter(item))
            .findAny()
            .isPresent();

    if (item.isBefore(checkInDatePicker.getValue().plusDays(1)) || 
            disable) {
            setDisable(true);
            setStyle("-fx-background-color: #ffc0cb;");
    }
    ...
}

      

+4


source







All Articles