To keep the DatePicker open while selecting multiple dates, you can create a custom DatePicker skin that allows multiple date selection and hack the default behavior of DatePicker closing upon selecting a date.
Here's a sample code that illustrates how this can be done:
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Skin;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class MultipleDatePicker extends Application {
@Override
public void start(Stage primaryStage) {
VBox root = new VBox();
Scene scene = new Scene(root, 300, 250);
DatePicker datePicker = new MultipleDatePicker();
root.getChildren().add(datePicker);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class MultipleDatePicker extends DatePicker {
// Property to store selected multiple dates
private final ObjectProperty<List<LocalDate>> selectedDates;
public MultipleDatePicker() {
super();
selectedDates = new SimpleObjectProperty<>(new ArrayList<>());
this.setSkin(new MultipleDateSkin(this));
this.selectedDates.setValue(FXCollections.observableArrayList());
}
public final ObjectProperty<List<LocalDate>> selectedDatesProperty() {
return selectedDates;
}
public final void setSelectedDates(List<LocalDate> value) {
selectedDates.set(value);
}
public final List<LocalDate> getSelectedDates() {
return selectedDates.get();
}
}
public static class MultipleDateSkin extends DatePickerSkin {
private DatePicker datePicker;
public MultipleDateSkin(DatePicker control) {
super(control);
datePicker = control;
updateDayCellFactory();
}
private void updateDayCellFactory() {
datePicker.setDayCellFactory(new Callback<DatePicker, DateCell>() {
@Override
public DateCell call(DatePicker param) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
LocalDate value = datePicker.getValue();
if (value != null && datePicker.getSelectedDates().contains(item)) {
getStyleClass().add("selected");
} else {
getStyleClass().remove("selected");
}
}
};
}
});
}
@Override
protected void handleControlPropertyChanged(String p) {
super.handleControlPropertyChanged(p);
if ("DAY_CELL_FACTORY".equals(p)) {
return;
}
if ("SHOW_WEEK_NUMBERS".equals(p)) {
return;
}
if (p.equals("CALENDAR_VALUE")) {
LocalDate date = datePicker.getValue();
if (date != null) {
boolean isSelected = datePicker.getSelectedDates().contains(date);
if (isSelected) {
datePicker.getSelectedDates().remove(date);
} else {
datePicker.getSelectedDates().add(date);
}
datePicker.setValue(null);
}
}
}
@Override
public Node getPopupContent() {
Node popupContent = super.getPopupContent();
if (popupContent instanceof VBox) {
VBox datePane = (VBox) popupContent;
datePane.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> event.consume());
}
return popupContent;
}
}
}
In this example, we create a custom MultipleDatePicker that extends DatePicker and MultipleDateSkin that extends DatePickerSkin. We've implemented the code for selection and deselection of dates and made it so that it consumes the mouse press events with the event.consume() function, keeping the DatePicker open when selecting multiple dates.
To enable multiple date selection and style the selected dates, go to your project's styles.css file and add:
.date-picker-popup .day-cell.selected {
-fx-background-color: #3f51b5;
-fx-text-fill: white;
-fx-start-margin: -1;
-fx-end-margin: -1;
}
You can move the styling part to a separate CSS file or just add it to the default one according to your project structure. Remember to load the CSS file in your JavaFX application.