JavaFX DatePicker允许从给定日历中选择一天。DatePicker控件包含一个带有日期字段和日期选择器的组合框。JavaFX DatePicker控件使用JDK8日期时间API。

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.DatePicker;
  4. import javafx.scene.layout.VBox;
  5. import javafx.stage.Stage;
  6. public class Main extends Application {
  7. public static void main(String[] args) {
  8. launch(args);
  9. }
  10. @Override
  11. public void start(Stage stage) {
  12. VBox vbox = new VBox(20);
  13. Scene scene = new Scene(vbox, 400, 400);
  14. stage.setScene(scene);
  15. DatePicker checkInDatePicker = new DatePicker();
  16. vbox.getChildren().add(checkInDatePicker);
  17. stage.show();
  18. }
  19. }

image.png

DatePicker创建

我们可以创建一个DatePicker并在类构造函数中设置一个特定的日期值。

  1. dateP = new DatePicker(LocalDate.of(2018, 10, 8));

image.png
还可以使用setValue()方法设置日期值。

  1. checkInDatePicker.setValue(LocalDate.of(2014, 10, 8));
  2. checkInDatePicker.setValue(LocalDate.now());

image.png
以下代码使用setValue()向结束DatePicker添加更多时间。

  1. import java.time.LocalDate;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.DatePicker;
  5. import javafx.scene.control.Label;
  6. import javafx.scene.layout.VBox;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. @Override
  13. public void start(Stage stage) {
  14. VBox vbox = new VBox(20);
  15. Scene scene = new Scene(vbox, 400, 400);
  16. stage.setScene(scene);
  17. DatePicker startDatePicker = new DatePicker();
  18. DatePicker endDatePicker = new DatePicker();
  19. startDatePicker.setValue(LocalDate.now());
  20. endDatePicker.setValue(startDatePicker.getValue().plusDays(1));
  21. vbox.getChildren().add(new Label("Start Date:"));
  22. vbox.getChildren().add(startDatePicker);
  23. vbox.getChildren().add(new Label("End Date:"));
  24. vbox.getChildren().add(endDatePicker);
  25. stage.show();
  26. }
  27. }

image.png

自定义日期选择器

可以通过使用setShowWeekNumbers()方法来启用和禁用在DatePicker中显示ISO周数。

  1. dateP.setShowWeekNumbers(true);

image.png
默认情况下,DatePicker使用系统区域设置和ISO日历系统定义的日期格式

  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import javafx.application.Application;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.DatePicker;
  6. import javafx.scene.layout.VBox;
  7. import javafx.stage.Stage;
  8. import javafx.util.StringConverter;
  9. public class Main extends Application {
  10. public static void main(String[] args) {
  11. launch(args);
  12. }
  13. @Override
  14. public void start(Stage stage) {
  15. String pattern = "yyyy-MM-dd";
  16. VBox vbox = new VBox(20);
  17. Scene scene = new Scene(vbox, 400, 400);
  18. stage.setScene(scene);
  19. DatePicker checkInDatePicker = new DatePicker();
  20. StringConverter<LocalDate> converter = new StringConverter<LocalDate>() {
  21. DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
  22. @Override
  23. public String toString(LocalDate date) {
  24. if (date != null) {
  25. return dateFormatter.format(date);
  26. } else {
  27. return "";
  28. }
  29. }
  30. @Override
  31. public LocalDate fromString(String string) {
  32. if (string != null && !string.isEmpty()) {
  33. return LocalDate.parse(string, dateFormatter);
  34. } else {
  35. return null;
  36. }
  37. }
  38. };
  39. checkInDatePicker.setConverter(converter);
  40. checkInDatePicker.setPromptText(pattern.toLowerCase());
  41. vbox.getChildren().add(checkInDatePicker);
  42. checkInDatePicker.requestFocus();
  43. stage.show();
  44. }
  45. }

image.png

DateCell

默认情况下,日历元素中的所有单元格都可供选择。可以使用日期单元工厂禁用单元格。

  1. import java.time.LocalDate;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.DateCell;
  5. import javafx.scene.control.DatePicker;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.layout.VBox;
  8. import javafx.stage.Stage;
  9. import javafx.util.Callback;
  10. public class Main extends Application {
  11. public static void main(String[] args) {
  12. launch(args);
  13. }
  14. @Override
  15. public void start(Stage stage) {
  16. VBox vbox = new VBox(20);
  17. Scene scene = new Scene(vbox, 400, 400);
  18. stage.setScene(scene);
  19. DatePicker startDatePicker = new DatePicker();
  20. DatePicker endDatePicker = new DatePicker();
  21. startDatePicker.setValue(LocalDate.now());
  22. final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
  23. @Override
  24. public DateCell call(final DatePicker datePicker) {
  25. return new DateCell() {
  26. @Override
  27. public void updateItem(LocalDate item, boolean empty) {
  28. super.updateItem(item, empty);
  29. if (item.isBefore(startDatePicker.getValue().plusDays(1))) {
  30. setDisable(true);
  31. setStyle("-fx-background-color: #EEEEEE;");
  32. }
  33. }
  34. };
  35. }
  36. };
  37. endDatePicker.setDayCellFactory(dayCellFactory);
  38. endDatePicker.setValue(startDatePicker.getValue().plusDays(1));
  39. vbox.getChildren().add(new Label("Start Date:"));
  40. vbox.getChildren().add(startDatePicker);
  41. vbox.getChildren().add(new Label("End Date:"));
  42. vbox.getChildren().add(endDatePicker);
  43. stage.show();
  44. }
  45. }

image.png

示例

为每个日期单元格安装工具提示。

  1. import java.time.LocalDate;
  2. import java.time.temporal.ChronoUnit;
  3. import javafx.application.Application;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.DateCell;
  6. import javafx.scene.control.DatePicker;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.control.Tooltip;
  9. import javafx.scene.layout.VBox;
  10. import javafx.stage.Stage;
  11. import javafx.util.Callback;
  12. public class Main extends Application {
  13. public static void main(String[] args) {
  14. launch(args);
  15. }
  16. @Override
  17. public void start(Stage stage) {
  18. VBox vbox = new VBox(20);
  19. Scene scene = new Scene(vbox, 400, 400);
  20. stage.setScene(scene);
  21. final DatePicker startDatePicker = new DatePicker();
  22. DatePicker endDatePicker = new DatePicker();
  23. startDatePicker.setValue(LocalDate.now());
  24. final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
  25. @Override
  26. public DateCell call(final DatePicker datePicker) {
  27. return new DateCell() {
  28. @Override
  29. public void updateItem(LocalDate item, boolean empty) {
  30. super.updateItem(item, empty);
  31. long p = ChronoUnit.DAYS.between(startDatePicker.getValue(), item);
  32. setTooltip(new Tooltip("You're about to stay for " + p + " days"));
  33. }
  34. };
  35. }
  36. };
  37. endDatePicker.setDayCellFactory(dayCellFactory);
  38. endDatePicker.setValue(startDatePicker.getValue().plusDays(1));
  39. vbox.getChildren().add(new Label("Start Date:"));
  40. vbox.getChildren().add(startDatePicker);
  41. vbox.getChildren().add(new Label("End Date:"));
  42. vbox.getChildren().add(endDatePicker);
  43. stage.show();
  44. }
  45. }

image.png