Reputation: 91
I'm using JFXDatePicker extends DatePicker and want to change default date when clicking Calendar Icon.
.setvalue()
because it will display that date when
the form was called.I've used this code but didn't work.
birthday = new JFXDatePicker(LocalDate.of(1980, Month.MARCH, 11));
[http://i228.photobucket.com/albums/ee83/ThamVanTam/JFXDatePicker_zpscdgns2b6.png]
Upvotes: 0
Views: 1990
Reputation: 51536
Unfortunately there is no public api to navigate in the popup content. That's supported by an internal class DatePickerContent, which has a method goToDate(LocalDate, boolean)
. If you are allowed (and daring enough :) to use internals, you can do so in a onShown handler (to make certain that skin's onShowing listener doesn't interfere):
datePicker.setOnShown(e -> {
if (datePicker.getValue() == null && datePicker.getSkin() instanceof DatePickerSkin) {
DatePickerSkin skin = (DatePickerSkin) datePicker.getSkin();
DatePickerContent popupContent = (DatePickerContent) skin.getPopupContent();
popupContent.goToDate(LocalDate.now().minusYears(18), true);
}
});
Note that this requires to open com.sun.javafx.scene.control.DatePickerContent
Upvotes: 1
Reputation: 151
You can simplify @KirantosTera's code by listening to the pickers showingProperty
then clearing the editor using Platform.runLater()
JFXDatePicker jfxDatePicker = new JFXDatePicker();
jfxDatePicker.showingProperty().addListener((observableValue, wasFocused, isNowFocus) -> {
if (isNowFocus && jfxDatePicker.getValue() == null) {
jfxDatePicker.setValue(LocalDate.now().minusYears(18));
Platform.runLater(()->{
jfxDatePicker.getEditor().clear();
});
}
});
Upvotes: 3
Reputation: 91
Here is my solution, it's not perfect but still good for my issue:
//Setting datepicker :
birthday.setDayCellFactory(picker -> new DateCell() {
@Override
public void updateItem(LocalDate date, boolean empty) {
super.updateItem(date, empty);
//Setting date 18 years ago
LocalDate today = LocalDate.ofYearDay(LocalDate.now().getYear() - 18, LocalDate.now().getDayOfYear());
//Disable future date
setDisable(empty || date.compareTo(today) > 0);
}
});
//Setting actual value
birthday.setValue(LocalDate.ofYearDay(LocalDate.now().getYear() - 18, LocalDate.now().getDayOfYear()));
//Cover the value by text
String pattern = "dd-MM-yyyy";
formatCalender.format(pattern, birthday);
birthday.getEditor().setText("Date Of Birth");
birthday.setPromptText(null);
Upvotes: 0