下面列出了怎么用javafx.scene.control.DateCell的API类实例代码及写法,或者点击链接到github查看源代码。
protected void forward(int offset, ChronoUnit unit, boolean focusDayCell, boolean withAnimation) {
if (withAnimation) {
if (tempImageTransition == null || tempImageTransition.getStatus() == Status.STOPPED) {
Pane monthContent = (Pane) calendarPlaceHolder.getChildren().get(0);
this.getParent().setManaged(false);
SnapshotParameters snapShotparams = new SnapshotParameters();
snapShotparams.setFill(Color.TRANSPARENT);
WritableImage temp = monthContent.snapshot(snapShotparams,
new WritableImage((int) monthContent.getWidth(),
(int) monthContent.getHeight()));
ImageView tempImage = new ImageView(temp);
calendarPlaceHolder.getChildren().add(calendarPlaceHolder.getChildren().size() - 2, tempImage);
TranslateTransition imageTransition = new TranslateTransition(Duration.millis(160), tempImage);
imageTransition.setToX(-offset * calendarPlaceHolder.getWidth());
imageTransition.setOnFinished((finish) -> calendarPlaceHolder.getChildren().remove(tempImage));
monthContent.setTranslateX(offset * calendarPlaceHolder.getWidth());
TranslateTransition contentTransition = new TranslateTransition(Duration.millis(160), monthContent);
contentTransition.setToX(0);
tempImageTransition = new ParallelTransition(imageTransition, contentTransition);
tempImageTransition.setOnFinished((finish) -> {
calendarPlaceHolder.getChildren().remove(tempImage);
this.getParent().setManaged(true);
});
tempImageTransition.play();
}
}
YearMonth yearMonth = selectedYearMonth.get();
DateCell dateCell = currentFocusedDayCell;
if (dateCell == null || !(dayCellDate(dateCell).getMonth() == yearMonth.getMonth())) {
dateCell = findDayCellOfDate(yearMonth.atDay(1));
}
goToDayCell(dateCell, offset, unit, focusDayCell);
}
private DateCell findDayCellOfDate(LocalDate date) {
for (int i = 0; i < dayCellDates.length; i++) {
if (date.equals(dayCellDates[i])) {
return dayCells.get(i);
}
}
return dayCells.get(dayCells.size() / 2 + 1);
}
@FXML
void initialize() {
LocalDate date = LocalDate.now();
Callback<DatePicker, DateCell> callback = d -> new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
this.getStyleClass().remove("selected");
this.getStyleClass().remove("contains");
LocalDate from = UnitDialog.this.from.getValue();
LocalDate to = UnitDialog.this.to.getValue();
if (from != null && to != null) {
if (item.equals(from) || item.equals(to)) {
this.getStyleClass().add("selected");
} else if ((from.compareTo(to) < 0 && item.compareTo(from) > 0 && item.compareTo(to) < 0)
|| (from.compareTo(to) > 0 && item.compareTo(from) < 0 && item.compareTo(to) > 0)) {
this.getStyleClass().add("contains");
}
}
}
};
this.to.setValue(date);
this.to.setDayCellFactory(callback);
this.from.setValue(date.minusWeeks(2));
this.from.setDayCellFactory(callback);
}
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));
checkInDatePicker.setChronology(ThaiBuddhistChronology.INSTANCE);
checkOutDatePicker.setChronology(HijrahChronology.INSTANCE);
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);
}
private void updateDayCells() {
Locale locale = getLocale();
Chronology chrono = getPrimaryChronology();
// get the index of the first day of the month
int firstDayOfWeek = WeekFields.of(getLocale()).getFirstDayOfWeek().getValue();
int firstOfMonthIndex = selectedYearMonth.get().atDay(1).getDayOfWeek().getValue() - firstDayOfWeek;
firstOfMonthIndex += firstOfMonthIndex < 0 ? daysPerWeek : 0;
YearMonth currentYearMonth = selectedYearMonth.get();
int daysInCurMonth = -1;
for (int i = 0; i < 6 * daysPerWeek; i++) {
DateCell dayCell = dayCells.get(i);
dayCell.getStyleClass().setAll("cell", "date-cell", "day-cell");
dayCell.setPrefSize(40, 42);
dayCell.setDisable(false);
dayCell.setStyle(null);
dayCell.setGraphic(null);
dayCell.setTooltip(null);
dayCell.setTextFill(DEFAULT_COLOR);
dayCell.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
CornerRadii.EMPTY,
Insets.EMPTY)));
try {
if (daysInCurMonth == -1) {
daysInCurMonth = currentYearMonth.lengthOfMonth();
}
int dayIndex = i - firstOfMonthIndex + 1;
LocalDate date = currentYearMonth.atDay(dayIndex);
dayCellDates[i] = date;
// if it's today
if (date.equals(LocalDate.now())) {
dayCell.setTextFill(this.datePicker.getDefaultColor());
dayCell.getStyleClass().add("today");
}
// if it's the current selected value
if (date.equals(datePicker.getValue())) {
dayCell.getStyleClass().add("selected");
dayCell.setTextFill(Color.WHITE);
dayCell.setBackground(
new Background(new BackgroundFill(this.datePicker.getDefaultColor(),
new CornerRadii(40),
Insets.EMPTY)));
}
ChronoLocalDate cDate = chrono.date(date);
String cellText = dayCellFormatter.withLocale(locale)
.withChronology(chrono)
.withDecimalStyle(DecimalStyle.of(locale))
.format(cDate);
dayCell.setText(cellText);
if (i < firstOfMonthIndex) {
dayCell.getStyleClass().add("previous-month");
dayCell.setText("");
} else if (i >= firstOfMonthIndex + daysInCurMonth) {
dayCell.getStyleClass().add("next-month");
dayCell.setText("");
}
// update cell item
dayCell.updateItem(date, false);
} catch (DateTimeException ex) {
// Disable day cell if its date is out of range
dayCell.setText("");
dayCell.setDisable(true);
}
}
}
protected LocalDate dayCellDate(DateCell dateCell) {
assert dayCellDates != null;
return dayCellDates[dayCells.indexOf(dateCell)];
}
private void goToDayCell(DateCell dateCell, int offset, ChronoUnit unit, boolean focusDayCell) {
YearMonth yearMonth = selectedYearMonth.get().plus(offset, unit);
goToDate(dayCellDate(dateCell).plus(offset, unit).withYear(yearMonth.getYear()), focusDayCell);
}
private void selectDayCell(DateCell dateCell) {
datePicker.setValue(dayCellDate(dateCell));
datePicker.hide();
}
protected void createDayCells() {
for (int row = 0; row < 6; row++) {
for (int col = 0; col < daysPerWeek; col++) {
DateCell dayCell = createDayCell();
dayCell.addEventHandler(MouseEvent.MOUSE_CLICKED, click -> {
// allow date selection on mouse primary button click
if (click.getButton() != MouseButton.PRIMARY) {
return;
}
DateCell selectedDayCell = (DateCell) click.getSource();
selectDayCell(selectedDayCell);
currentFocusedDayCell = selectedDayCell;
});
// add mouse hover listener
dayCell.setOnMouseEntered((event) -> {
if (!dayCell.getStyleClass().contains("selected")) {
dayCell.setBackground(new Background(new BackgroundFill(Color.valueOf("#EDEDED"),
new CornerRadii(40),
Insets.EMPTY)));
}
});
dayCell.setOnMouseExited((event) -> {
if (!dayCell.getStyleClass().contains("selected")) {
dayCell.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
CornerRadii.EMPTY,
Insets.EMPTY)));
}
});
dayCell.setAlignment(Pos.BASELINE_CENTER);
dayCell.setBorder(
new Border(new BorderStroke(Color.TRANSPARENT,
BorderStrokeStyle.SOLID,
CornerRadii.EMPTY,
new BorderWidths(5))));
dayCell.setFont(Font.font(ROBOTO, FontWeight.BOLD, 12));
dayCells.add(dayCell);
}
}
dayCellDates = new LocalDate[6 * daysPerWeek];
// position the cells into the grid
updateContentGrid();
}