- java.lang.Object
-
- javafx.scene.Node
-
- javafx.scene.Parent
-
- javafx.scene.layout.Region
-
- javafx.scene.control.Control
-
- javafx.scene.control.ComboBoxBase<LocalDate>
-
- javafx.scene.control.DatePicker
-
- All Implemented Interfaces:
-
Styleable
,EventTarget
,Skinnable
public class DatePicker extends ComboBoxBase<LocalDate>
DatePicker控件允许用户输入日期作为文本或从日历弹出窗口中选择日期。 该日历基于标准ISO-8601年表或java.time.chrono包中定义的任何其他年表类。value
属性表示当前选定的LocalDate
。 初始日期可以通过constructor
或致电setValue(LocalDate)
进行设置。 默认值为null。final DatePicker datePicker = new DatePicker(); datePicker.setOnAction(new EventHandler() { public void handle(Event t) { LocalDate date = datePicker.getValue(); System.err.println("Selected date: " + date); } });
chronology
属性指定用于解析,显示和选择日期的日历系统。value
属性始终在ISO日历系统中定义,因此,基于不同年表的应用程序可能会使用Chronology
API中提供的转换方法来获取或设置相应的ChronoLocalDate
值。 例如:LocalDate isoDate = datePicker.getValue(); ChronoLocalDate chronoDate = ((isoDate != null) ? datePicker.getChronology().date(isoDate) : null); System.err.println("Selected date: " + chronoDate);
- 从以下版本开始:
- JavaFX 8.0
-
-
Property Summary
Properties Type Property 描述 ObjectProperty<Chronology>
chronology
用于在DatePicker控件中解析,显示和选择日期的日历系统。ObjectProperty<StringConverter<LocalDate>>
converter
将输入文本转换为类型为LocalDate的对象,反之亦然。ObjectProperty<Callback<DatePicker,DateCell>>
dayCellFactory
可以提供自定义单元格工厂来自定义DatePicker弹出窗口中的单个日期单元格。ReadOnlyObjectProperty<TextField>
editor
DatePicker的编辑器。BooleanProperty
showWeekNumbers
DatePicker弹出窗口是否应显示显示周数的列。-
Properties inherited from class javafx.scene.control.ComboBoxBase
armed, editable, onAction, onHidden, onHiding, onShowing, onShown, promptText, showing, value
-
Properties inherited from class javafx.scene.control.Control
contextMenu, skin, tooltip
-
Properties inherited from class javafx.scene.Node
accessibleHelp, accessibleRoleDescription, accessibleRole, accessibleText, blendMode, boundsInLocal, boundsInParent, cacheHint, cache, clip, cursor, depthTest, disabled, disable, effectiveNodeOrientation, effect, eventDispatcher, focused, focusTraversable, hover, id, inputMethodRequests, layoutBounds, layoutX, layoutY, localToParentTransform, localToSceneTransform, managed, mouseTransparent, nodeOrientation, onContextMenuRequested, onDragDetected, onDragDone, onDragDropped, onDragEntered, onDragExited, onDragOver, onInputMethodTextChanged, onKeyPressed, onKeyReleased, onKeyTyped, onMouseClicked, onMouseDragEntered, onMouseDragExited, onMouseDragged, onMouseDragOver, onMouseDragReleased, onMouseEntered, onMouseExited, onMouseMoved, onMousePressed, onMouseReleased, onRotate, onRotationFinished, onRotationStarted, onScrollFinished, onScroll, onScrollStarted, onSwipeDown, onSwipeLeft, onSwipeRight, onSwipeUp, onTouchMoved, onTouchPressed, onTouchReleased, onTouchStationary, onZoomFinished, onZoom, onZoomStarted, opacity, parent, pickOnBounds, pressed, rotate, rotationAxis, scaleX, scaleY, scaleZ, scene, style, translateX, translateY, translateZ, viewOrder, visible
-
Properties inherited from class javafx.scene.Parent
needsLayout
-
Properties inherited from class javafx.scene.layout.Region
background, border, cacheShape, centerShape, height, insets, maxHeight, maxWidth, minHeight, minWidth, opaqueInsets, padding, prefHeight, prefWidth, scaleShape, shape, snapToPixel, width
-
-
Field Summary
-
Fields inherited from class javafx.scene.control.ComboBoxBase
ON_HIDDEN, ON_HIDING, ON_SHOWING, ON_SHOWN
-
Fields inherited from class javafx.scene.Node
BASELINE_OFFSET_SAME_AS_HEIGHT
-
Fields inherited from class javafx.scene.layout.Region
USE_COMPUTED_SIZE, USE_PREF_SIZE
-
-
构造方法摘要
构造方法 Constructor 描述 DatePicker()
创建一个默认的DatePicker实例,其日期值为null
。DatePicker(LocalDate localDate)
创建一个DatePicker实例,并将value
设置为给定的日期。
-
方法摘要
-
Methods inherited from class javafx.scene.control.ComboBoxBase
arm, armedProperty, disarm, editableProperty, executeAccessibleAction, getOnAction, getOnHidden, getOnHiding, getOnShowing, getOnShown, getPromptText, getValue, hide, isArmed, isEditable, isShowing, onActionProperty, onHiddenProperty, onHidingProperty, onShowingProperty, onShownProperty, promptTextProperty, setEditable, setOnAction, setOnHidden, setOnHiding, setOnShowing, setOnShown, setPromptText, setValue, show, showingProperty, valueProperty
-
Methods inherited from class javafx.scene.control.Control
computeMaxHeight, computeMaxWidth, computeMinHeight, computeMinWidth, computePrefHeight, computePrefWidth, contextMenuProperty, getBaselineOffset, getContextMenu, getCssMetaData, getInitialFocusTraversable, getSkin, getTooltip, isResizable, layoutChildren, setContextMenu, setSkin, setTooltip, skinProperty, tooltipProperty
-
Methods inherited from class javafx.scene.Node
accessibleHelpProperty, accessibleRoleDescriptionProperty, accessibleRoleProperty, accessibleTextProperty, addEventFilter, addEventHandler, applyCss, autosize, blendModeProperty, boundsInLocalProperty, boundsInParentProperty, buildEventDispatchChain, cacheHintProperty, cacheProperty, clipProperty, computeAreaInScreen, contains, contains, cursorProperty, depthTestProperty, disabledProperty, disableProperty, effectiveNodeOrientationProperty, effectProperty, eventDispatcherProperty, fireEvent, focusedProperty, focusTraversableProperty, getAccessibleHelp, getAccessibleRole, getAccessibleRoleDescription, getAccessibleText, getBlendMode, getBoundsInLocal, getBoundsInParent, getCacheHint, getClip, getContentBias, getCursor, getDepthTest, getEffect, getEffectiveNodeOrientation, getEventDispatcher, getId, getInitialCursor, getInputMethodRequests, getLayoutBounds, getLayoutX, getLayoutY, getLocalToParentTransform, getLocalToSceneTransform, getNodeOrientation, getOnContextMenuRequested, getOnDragDetected, getOnDragDone, getOnDragDropped, getOnDragEntered, getOnDragExited, getOnDragOver, getOnInputMethodTextChanged, getOnKeyPressed, getOnKeyReleased, getOnKeyTyped, getOnMouseClicked, getOnMouseDragEntered, getOnMouseDragExited, getOnMouseDragged, getOnMouseDragOver, getOnMouseDragReleased, getOnMouseEntered, getOnMouseExited, getOnMouseMoved, getOnMousePressed, getOnMouseReleased, getOnRotate, getOnRotationFinished, getOnRotationStarted, getOnScroll, getOnScrollFinished, getOnScrollStarted, getOnSwipeDown, getOnSwipeLeft, getOnSwipeRight, getOnSwipeUp, getOnTouchMoved, getOnTouchPressed, getOnTouchReleased, getOnTouchStationary, getOnZoom, getOnZoomFinished, getOnZoomStarted, getOpacity, getParent, getProperties, getPseudoClassStates, getRotate, getRotationAxis, getScaleX, getScaleY, getScaleZ, getScene, getStyle, getStyleableParent, getStyleClass, getTransforms, getTranslateX, getTranslateY, getTranslateZ, getTypeSelector, getUserData, getViewOrder, hasProperties, hoverProperty, idProperty, inputMethodRequestsProperty, intersects, intersects, isCache, isDisable, isDisabled, isFocused, isFocusTraversable, isHover, isManaged, isMouseTransparent, isPickOnBounds, isPressed, isVisible, layoutBoundsProperty, layoutXProperty, layoutYProperty, localToParent, localToParent, localToParent, localToParent, localToParent, localToParentTransformProperty, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToSceneTransformProperty, localToScreen, localToScreen, localToScreen, localToScreen, localToScreen, lookupAll, managedProperty, mouseTransparentProperty, nodeOrientationProperty, notifyAccessibleAttributeChanged, onContextMenuRequestedProperty, onDragDetectedProperty, onDragDoneProperty, onDragDroppedProperty, onDragEnteredProperty, onDragExitedProperty, onDragOverProperty, onInputMethodTextChangedProperty, onKeyPressedProperty, onKeyReleasedProperty, onKeyTypedProperty, onMouseClickedProperty, onMouseDragEnteredProperty, onMouseDragExitedProperty, onMouseDraggedProperty, onMouseDragOverProperty, onMouseDragReleasedProperty, onMouseEnteredProperty, onMouseExitedProperty, onMouseMovedProperty, onMousePressedProperty, onMouseReleasedProperty, onRotateProperty, onRotationFinishedProperty, onRotationStartedProperty, onScrollFinishedProperty, onScrollProperty, onScrollStartedProperty, onSwipeDownProperty, onSwipeLeftProperty, onSwipeRightProperty, onSwipeUpProperty, onTouchMovedProperty, onTouchPressedProperty, onTouchReleasedProperty, onTouchStationaryProperty, onZoomFinishedProperty, onZoomProperty, onZoomStartedProperty, opacityProperty, parentProperty, parentToLocal, parentToLocal, parentToLocal, parentToLocal, parentToLocal, pickOnBoundsProperty, pressedProperty, pseudoClassStateChanged, relocate, removeEventFilter, removeEventHandler, requestFocus, resizeRelocate, rotateProperty, rotationAxisProperty, scaleXProperty, scaleYProperty, scaleZProperty, sceneProperty, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, screenToLocal, screenToLocal, screenToLocal, setAccessibleHelp, setAccessibleRole, setAccessibleRoleDescription, setAccessibleText, setBlendMode, setCache, setCacheHint, setClip, setCursor, setDepthTest, setDisable, setDisabled, setEffect, setEventDispatcher, setEventHandler, setFocused, setFocusTraversable, setHover, setId, setInputMethodRequests, setLayoutX, setLayoutY, setManaged, setMouseTransparent, setNodeOrientation, setOnContextMenuRequested, setOnDragDetected, setOnDragDone, setOnDragDropped, setOnDragEntered, setOnDragExited, setOnDragOver, setOnInputMethodTextChanged, setOnKeyPressed, setOnKeyReleased, setOnKeyTyped, setOnMouseClicked, setOnMouseDragEntered, setOnMouseDragExited, setOnMouseDragged, setOnMouseDragOver, setOnMouseDragReleased, setOnMouseEntered, setOnMouseExited, setOnMouseMoved, setOnMousePressed, setOnMouseReleased, setOnRotate, setOnRotationFinished, setOnRotationStarted, setOnScroll, setOnScrollFinished, setOnScrollStarted, setOnSwipeDown, setOnSwipeLeft, setOnSwipeRight, setOnSwipeUp, setOnTouchMoved, setOnTouchPressed, setOnTouchReleased, setOnTouchStationary, setOnZoom, setOnZoomFinished, setOnZoomStarted, setOpacity, setPickOnBounds, setPressed, setRotate, setRotationAxis, setScaleX, setScaleY, setScaleZ, setStyle, setTranslateX, setTranslateY, setTranslateZ, setUserData, setViewOrder, setVisible, snapshot, snapshot, startDragAndDrop, startFullDrag, styleProperty, toBack, toFront, toString, translateXProperty, translateYProperty, translateZProperty, usesMirroring, viewOrderProperty, visibleProperty
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from class javafx.scene.Parent
getChildren, getChildrenUnmodifiable, getManagedChildren, getStylesheets, isNeedsLayout, layout, lookup, needsLayoutProperty, requestLayout, requestParentLayout, setNeedsLayout, updateBounds
-
Methods inherited from class javafx.scene.layout.Region
backgroundProperty, borderProperty, cacheShapeProperty, centerShapeProperty, getBackground, getBorder, getHeight, getInsets, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getOpaqueInsets, getPadding, getPrefHeight, getPrefWidth, getShape, getUserAgentStylesheet, getWidth, heightProperty, insetsProperty, isCacheShape, isCenterShape, isScaleShape, isSnapToPixel, layoutInArea, layoutInArea, layoutInArea, layoutInArea, maxHeight, maxHeightProperty, maxWidth, maxWidthProperty, minHeight, minHeightProperty, minWidth, minWidthProperty, opaqueInsetsProperty, paddingProperty, positionInArea, positionInArea, prefHeight, prefHeightProperty, prefWidth, prefWidthProperty, resize, scaleShapeProperty, setBackground, setBorder, setCacheShape, setCenterShape, setHeight, setMaxHeight, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinWidth, setOpaqueInsets, setPadding, setPrefHeight, setPrefSize, setPrefWidth, setScaleShape, setShape, setSnapToPixel, setWidth, shapeProperty, snappedBottomInset, snappedLeftInset, snappedRightInset, snappedTopInset, snapPosition, snapPositionX, snapPositionY, snapSize, snapSizeX, snapSizeY, snapSpace, snapSpaceX, snapSpaceY, snapToPixelProperty, widthProperty
-
Methods inherited from interface javafx.css.Styleable
getStyleableNode
-
-
-
-
Property Detail
-
dayCellFactory
public final ObjectProperty<Callback<DatePicker,DateCell>> dayCellFactoryProperty
可以提供自定义单元格工厂来自定义DatePicker弹出窗口中的单个日期单元格。 有关电池工厂的更多信息,请参阅DateCell
和Cell
。 例:final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() { public DateCell call(final DatePicker datePicker) { return new DateCell() { @Override public void updateItem(LocalDate item, boolean empty) { super.updateItem(item, empty); if (MonthDay.from(item).equals(MonthDay.of(9, 25))) { setTooltip(new Tooltip("Happy Birthday!")); setStyle("-fx-background-color: #ff4444;"); } if (item.equals(LocalDate.now().plusDays(1))) { // Tomorrow is too soon. setDisable(true); } } }; } }; datePicker.setDayCellFactory(dayCellFactory);
- Default value:
- 空值
- 另请参见:
-
getDayCellFactory()
,setDayCellFactory(Callback)
-
chronology
public final ObjectProperty<Chronology> chronologyProperty
用于在DatePicker控件中解析,显示和选择日期的日历系统。默认值从调用返回到
Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))
。 默认值通常为IsoChronology
,除非通过使用区域设置日历扩展名在Locale
中明确提供。 将值设置为null
将恢复默认年表。
-
showWeekNumbers
public final BooleanProperty showWeekNumbersProperty
DatePicker弹出窗口是否应显示显示周数的列。默认值在资源束中指定,并且取决于当前语言环境的国家/地区。
-
converter
public final ObjectProperty<StringConverter<LocalDate>> converterProperty
将输入文本转换为类型为LocalDate的对象,反之亦然。如果没有由应用程序设置,DatePicker皮肤类将基于
Locale
和chronology
的DateTimeFormatter
设置转换器。 然后,此格式化程序用于解析和显示当前日期值。 将值设置为null
将恢复默认转换器。使用显式格式化程序的示例:
datePicker.setConverter(new StringConverter<LocalDate>() { String pattern = "yyyy-MM-dd"; DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern); { datePicker.setPromptText(pattern.toLowerCase()); } @Override public String toString(LocalDate date) { if (date != null) { return dateFormatter.format(date); } else { return ""; } } @Override public LocalDate fromString(String string) { if (string != null && !string.isEmpty()) { return LocalDate.parse(string, dateFormatter); } else { return null; } } });
包装默认格式化程序并捕获解析异常的示例:
final StringConverter<LocalDate> defaultConverter = datePicker.getConverter(); datePicker.setConverter(new StringConverter<LocalDate>() { @Override public String toString(LocalDate value) { return defaultConverter.toString(value); } @Override public LocalDate fromString(String text) { try { return defaultConverter.fromString(text); } catch (DateTimeParseException ex) { System.err.println("HelloDatePicker: "+ex.getMessage()); throw ex; } } });
解析输入的默认基准年仅包含两年的数字为2000(见
DateTimeFormatter
)。 此默认对于允许输入人的出生日期无效。 以下示例将转换器的fromString()方法修改为允许两位数的年份为过去99年的出生日期。@Override public LocalDate fromString(String text) { if (text != null && !text.isEmpty()) { Locale locale = Locale.getDefault(Locale.Category.FORMAT); Chronology chrono = datePicker.getChronology(); String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null, chrono, locale); String prePattern = pattern.substring(0, pattern.indexOf("y")); String postPattern = pattern.substring(pattern.lastIndexOf("y")+1); int baseYear = LocalDate.now().getYear() - 99; DateTimeFormatter df = new DateTimeFormatterBuilder() .parseLenient() .appendPattern(prePattern) .appendValueReduced(ChronoField.YEAR, 2, 2, baseYear) .appendPattern(postPattern) .toFormatter(); return LocalDate.from(chrono.date(df.parse(text))); } else { return null; } }
-
editor
public final ReadOnlyObjectProperty<TextField> editorProperty
DatePicker的编辑器。- 另请参见:
-
getEditor()
-
-
方法详细信息
-
setDayCellFactory
public final void setDayCellFactory(Callback<DatePicker,DateCell> value)
设置属性dayCellFactory的值。- Property description:
-
可以提供自定义单元格工厂来自定义DatePicker弹出窗口中的单个日期单元格。
有关电池工厂的更多信息,请参阅
DateCell
和Cell
。 例:final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() { public DateCell call(final DatePicker datePicker) { return new DateCell() { @Override public void updateItem(LocalDate item, boolean empty) { super.updateItem(item, empty); if (MonthDay.from(item).equals(MonthDay.of(9, 25))) { setTooltip(new Tooltip("Happy Birthday!")); setStyle("-fx-background-color: #ff4444;"); } if (item.equals(LocalDate.now().plusDays(1))) { // Tomorrow is too soon. setDisable(true); } } }; } }; datePicker.setDayCellFactory(dayCellFactory);
- Default value:
- 空值
-
getDayCellFactory
public final Callback<DatePicker,DateCell> getDayCellFactory()
获取属性dayCellFactory的值。- Property description:
-
可以提供自定义单元格工厂来自定义DatePicker弹出窗口中的单个日期单元格。
有关电池工厂的更多信息,请参阅
DateCell
和Cell
。 例:final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() { public DateCell call(final DatePicker datePicker) { return new DateCell() { @Override public void updateItem(LocalDate item, boolean empty) { super.updateItem(item, empty); if (MonthDay.from(item).equals(MonthDay.of(9, 25))) { setTooltip(new Tooltip("Happy Birthday!")); setStyle("-fx-background-color: #ff4444;"); } if (item.equals(LocalDate.now().plusDays(1))) { // Tomorrow is too soon. setDisable(true); } } }; } }; datePicker.setDayCellFactory(dayCellFactory);
- Default value:
- 空值
-
dayCellFactoryProperty
public final ObjectProperty<Callback<DatePicker,DateCell>> dayCellFactoryProperty()
可以提供自定义单元格工厂来自定义DatePicker弹出窗口中的单个日期单元格。 有关电池工厂的更多信息,请参阅DateCell
和Cell
。 例:final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() { public DateCell call(final DatePicker datePicker) { return new DateCell() { @Override public void updateItem(LocalDate item, boolean empty) { super.updateItem(item, empty); if (MonthDay.from(item).equals(MonthDay.of(9, 25))) { setTooltip(new Tooltip("Happy Birthday!")); setStyle("-fx-background-color: #ff4444;"); } if (item.equals(LocalDate.now().plusDays(1))) { // Tomorrow is too soon. setDisable(true); } } }; } }; datePicker.setDayCellFactory(dayCellFactory);
- Default value:
- 空值
- 另请参见:
-
getDayCellFactory()
,setDayCellFactory(Callback)
-
chronologyProperty
public final ObjectProperty<Chronology> chronologyProperty()
用于在DatePicker控件中解析,显示和选择日期的日历系统。默认值从调用返回到
Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))
。 默认值通常为IsoChronology
,除非通过使用区域设置日历扩展名在Locale
中明确提供。 将值设置为null
将恢复默认年表。
-
getChronology
public final Chronology getChronology()
获取物业年表的价值。- Property description:
-
用于在DatePicker控件中解析,显示和选择日期的日历系统。
默认值从调用返回到
Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))
。 默认值通常为IsoChronology
,除非通过使用区域设置日历扩展名在Locale
中明确提供。 将值设置为null
将恢复默认年表。
-
setChronology
public final void setChronology(Chronology value)
设置属性年表的值。- Property description:
-
用于在DatePicker控件中解析,显示和选择日期的日历系统。
从调用
Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))
返回默认值。 默认值通常为IsoChronology
,除非通过使用区域设置日历扩展名在Locale
中明确提供。 将值设置为null
将恢复默认年表。
-
showWeekNumbersProperty
public final BooleanProperty showWeekNumbersProperty()
DatePicker弹出窗口是否应显示显示周数的列。默认值在资源束中指定,并且取决于当前语言环境的国家/地区。
-
setShowWeekNumbers
public final void setShowWeekNumbers(boolean value)
设置属性showWeekNumbers的值。- Property description:
-
DatePicker弹出窗口是否应显示显示周数的列。
默认值在资源束中指定,并且取决于当前语言环境的国家/地区。
-
isShowWeekNumbers
public final boolean isShowWeekNumbers()
获取属性showWeekNumbers的值。- Property description:
-
DatePicker弹出窗口是否应显示显示周数的列。
默认值在资源束中指定,并且取决于当前语言环境的国家/地区。
-
converterProperty
public final ObjectProperty<StringConverter<LocalDate>> converterProperty()
将输入文本转换为类型为LocalDate的对象,反之亦然。如果没有由应用程序设置,DatePicker皮肤类将基于
Locale
和chronology
的DateTimeFormatter
设置转换器。 然后,此格式化程序用于解析和显示当前日期值。 将值设置为null
将恢复默认转换器。使用显式格式化程序的示例:
datePicker.setConverter(new StringConverter<LocalDate>() { String pattern = "yyyy-MM-dd"; DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern); { datePicker.setPromptText(pattern.toLowerCase()); } @Override public String toString(LocalDate date) { if (date != null) { return dateFormatter.format(date); } else { return ""; } } @Override public LocalDate fromString(String string) { if (string != null && !string.isEmpty()) { return LocalDate.parse(string, dateFormatter); } else { return null; } } });
包装默认格式化程序并捕获解析异常的示例:
final StringConverter<LocalDate> defaultConverter = datePicker.getConverter(); datePicker.setConverter(new StringConverter<LocalDate>() { @Override public String toString(LocalDate value) { return defaultConverter.toString(value); } @Override public LocalDate fromString(String text) { try { return defaultConverter.fromString(text); } catch (DateTimeParseException ex) { System.err.println("HelloDatePicker: "+ex.getMessage()); throw ex; } } });
解析输入的默认基准年仅包含两年的数字为2000(见
DateTimeFormatter
)。 此默认对于允许输入人的出生日期无效。 以下示例将转换器的fromString()方法修改为允许两位数的年份为过去99年的出生日期。@Override public LocalDate fromString(String text) { if (text != null && !text.isEmpty()) { Locale locale = Locale.getDefault(Locale.Category.FORMAT); Chronology chrono = datePicker.getChronology(); String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null, chrono, locale); String prePattern = pattern.substring(0, pattern.indexOf("y")); String postPattern = pattern.substring(pattern.lastIndexOf("y")+1); int baseYear = LocalDate.now().getYear() - 99; DateTimeFormatter df = new DateTimeFormatterBuilder() .parseLenient() .appendPattern(prePattern) .appendValueReduced(ChronoField.YEAR, 2, 2, baseYear) .appendPattern(postPattern) .toFormatter(); return LocalDate.from(chrono.date(df.parse(text))); } else { return null; } }
-
setConverter
public final void setConverter(StringConverter<LocalDate> value)
设置属性转换器的值。- Property description:
-
将输入文本转换为类型为LocalDate的对象,反之亦然。
如果没有由应用程序设置,DatePicker皮肤类将根据当前的
Locale
和chronology
的DateTimeFormatter
设置一个转换器。 然后,此格式化程序用于解析和显示当前日期值。 将值设置为null
将恢复默认转换器。使用显式格式化程序的示例:
datePicker.setConverter(new StringConverter<LocalDate>() { String pattern = "yyyy-MM-dd"; DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern); { datePicker.setPromptText(pattern.toLowerCase()); } @Override public String toString(LocalDate date) { if (date != null) { return dateFormatter.format(date); } else { return ""; } } @Override public LocalDate fromString(String string) { if (string != null && !string.isEmpty()) { return LocalDate.parse(string, dateFormatter); } else { return null; } } });
包装默认格式化程序并捕获解析异常的示例:
final StringConverter<LocalDate> defaultConverter = datePicker.getConverter(); datePicker.setConverter(new StringConverter<LocalDate>() { @Override public String toString(LocalDate value) { return defaultConverter.toString(value); } @Override public LocalDate fromString(String text) { try { return defaultConverter.fromString(text); } catch (DateTimeParseException ex) { System.err.println("HelloDatePicker: "+ex.getMessage()); throw ex; } } });
解析输入的默认基准年仅包含两位数字,为2000(见
DateTimeFormatter
)。 此默认对于允许输入人的出生日期无效。 以下示例将转换器的fromString()方法修改为允许两位数的年份为过去99年的出生日期。@Override public LocalDate fromString(String text) { if (text != null && !text.isEmpty()) { Locale locale = Locale.getDefault(Locale.Category.FORMAT); Chronology chrono = datePicker.getChronology(); String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null, chrono, locale); String prePattern = pattern.substring(0, pattern.indexOf("y")); String postPattern = pattern.substring(pattern.lastIndexOf("y")+1); int baseYear = LocalDate.now().getYear() - 99; DateTimeFormatter df = new DateTimeFormatterBuilder() .parseLenient() .appendPattern(prePattern) .appendValueReduced(ChronoField.YEAR, 2, 2, baseYear) .appendPattern(postPattern) .toFormatter(); return LocalDate.from(chrono.date(df.parse(text))); } else { return null; } }
-
getConverter
public final StringConverter<LocalDate> getConverter()
获取属性转换器的值。- Property description:
-
将输入文本转换为类型为LocalDate的对象,反之亦然。
如果没有由应用程序设置,DatePicker皮肤类将根据当前的
Locale
和chronology
的DateTimeFormatter
设置一个转换器。 然后,此格式化程序用于解析和显示当前日期值。 将值设置为null
将恢复默认转换器。使用显式格式化程序的示例:
datePicker.setConverter(new StringConverter<LocalDate>() { String pattern = "yyyy-MM-dd"; DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern); { datePicker.setPromptText(pattern.toLowerCase()); } @Override public String toString(LocalDate date) { if (date != null) { return dateFormatter.format(date); } else { return ""; } } @Override public LocalDate fromString(String string) { if (string != null && !string.isEmpty()) { return LocalDate.parse(string, dateFormatter); } else { return null; } } });
包装默认格式化程序并捕获解析异常的示例:
final StringConverter<LocalDate> defaultConverter = datePicker.getConverter(); datePicker.setConverter(new StringConverter<LocalDate>() { @Override public String toString(LocalDate value) { return defaultConverter.toString(value); } @Override public LocalDate fromString(String text) { try { return defaultConverter.fromString(text); } catch (DateTimeParseException ex) { System.err.println("HelloDatePicker: "+ex.getMessage()); throw ex; } } });
解析输入的默认基准年仅包含两年的数字为2000(见
DateTimeFormatter
)。 此默认对于允许输入人的出生日期无效。 以下示例将转换器的fromString()方法修改为允许两位数的年份为过去99年的出生日期。@Override public LocalDate fromString(String text) { if (text != null && !text.isEmpty()) { Locale locale = Locale.getDefault(Locale.Category.FORMAT); Chronology chrono = datePicker.getChronology(); String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null, chrono, locale); String prePattern = pattern.substring(0, pattern.indexOf("y")); String postPattern = pattern.substring(pattern.lastIndexOf("y")+1); int baseYear = LocalDate.now().getYear() - 99; DateTimeFormatter df = new DateTimeFormatterBuilder() .parseLenient() .appendPattern(prePattern) .appendValueReduced(ChronoField.YEAR, 2, 2, baseYear) .appendPattern(postPattern) .toFormatter(); return LocalDate.from(chrono.date(df.parse(text))); } else { return null; } }
-
getEditor
public final TextField getEditor()
获取属性编辑器的值。- Property description:
- DatePicker的编辑器。
-
editorProperty
public final ReadOnlyObjectProperty<TextField> editorProperty()
DatePicker的编辑器。- 另请参见:
-
getEditor()
-
createDefaultSkin
protected Skin<?> createDefaultSkin()
为此控件创建一个新的默认皮肤实例。 如果没有通过CSS-fx-skin
提供皮肤或者在具有setSkin(...)
的子类中显式设置,则调用它来创建控件的皮肤。- 重写:
-
createDefaultSkin
在Control
- 结果
- 此控件的默认皮肤的新实例。 如果为null,则控件将没有皮肤,除非由css提供。
-
getClassCssMetaData
public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
- 结果
- 与此类相关联的CssMetaData可能包括其类的CssMetaData。
-
getControlCssMetaData
public List<CssMetaData<? extends Styleable,?>> getControlCssMetaData()
- 重写:
-
getControlCssMetaData
在Control
- 结果
- 不可修改的控件列表css样式属性
- 从以下版本开始:
- JavaFX 8.0
-
queryAccessibleAttribute
public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters)
该方法由辅助技术调用以请求属性的值。子类通常覆盖此方法来实现特定角色所需的属性。
如果未处理特定属性,则必须调用超类实现。- 重写:
-
queryAccessibleAttribute
在ComboBoxBase<LocalDate>
- 参数
-
attribute
- 请求的属性 -
parameters
- 可选参数列表 - 结果
- 请求的属性的值
- 另请参见:
-
AccessibleAttribute
-
-