java - 如果设置了 HijrahChronology,DatePicker 的 setDayCellFactory 中的 setDisable() 不起作用

标签 java javafx datepicker javafx-8 javafx-datepicker

我正在尝试使用 JavaFX 8 的 DatePicker 禁用不在范围 [今天,今天 + 1 年] 内的日期,类似于 the example in the official tutorial 。这是代码:

样本.fxml:

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.DatePicker?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

    <DatePicker fx:id="dpDate"/>

</GridPane>

Main.java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Controller.java:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.util.Callback;
import javafx.util.StringConverter;

import java.net.URL;
import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;

public class Controller implements Initializable
{
    private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

    @FXML private DatePicker dpDate;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        Callback<DatePicker, DateCell> dayCellFactory = dp -> new DateCell()
        {
            @Override
            public void updateItem(LocalDate item, boolean empty)
            {
                super.updateItem(item, empty);

                if(item.isBefore(LocalDate.now()) || item.isAfter(LocalDate.now().plusYears(1)))
                {
                    setStyle("-fx-background-color: #ffc0cb; -fx-text-fill: darkgray;");
                    setDisable(true);
                }
            }
        };

        StringConverter converter = new StringConverter<LocalDate>()
        {
            @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())
                {
                    LocalDate date = LocalDate.parse(string, dateFormatter);

                    if(date.isBefore(LocalDate.now()) || date.isAfter(LocalDate.now().plusYears(1)))
                    {
                        return dpDate.getValue();
                    }
                    else return date;
                }

                return null;
            }
        };

        dpDate.setDayCellFactory(dayCellFactory);
        dpDate.setConverter(converter);
        dpDate.setPromptText("dd/MM/yyyy");
        dpDate.setValue(LocalDate.now());
        dpDate.setChronology(HijrahChronology.INSTANCE);
    }
}

enter image description here


仅当我删除最后一行 dpDate.setChronology(HijrahChronology.INSTANCE); 时,此示例才有效(即用户无法在范围之外进行选择)并保留默认值 IsoChronology .

enter image description here


我尝试消耗updateItem()内的点击事件:

@Override
public void updateItem(LocalDate item, boolean empty)
{
    super.updateItem(item, empty);

    if(item.isBefore(LocalDate.now()) || item.isAfter(LocalDate.now().plusYears(1)))
    {
        setStyle("-fx-background-color: #ffc0cb; -fx-text-fill: darkgray;");
        setDisable(true);

        addEventFilter(MouseEvent.MOUSE_CLICKED, e -> e.consume());
    }
}

适用于当月(不能选择今天之前的日子)。但如果我转到接下来的几个月,它会阻止用户从允许的范围中选择一天。


这绝对是一个错误。有解决方法吗?如何访问日期选择器弹出窗口?也许我可以将一个监听器附加到更改月份页面的按钮,然后迭代所有的单元格并手动禁用超出范围的日期。

最佳答案

我在 JavaFX JIRA 中提交了错误报告,他们将在下一个更新 8u60 中修复它。以下是 Leif Samuelsson 建议的解决方法:

This is a bug in com.sun.javafx.scene.control.skin.DatePickerHijrahContent. It overrides updateDayCells() and calls setDisable(false) on all cells after super has called updateItem() on the custom cell factory.

A workaround is to wrap the setDisable() call in a Platform.runLater():

Platform.runLater(() -> { setDisable(true); });

关于java - 如果设置了 HijrahChronology,DatePicker 的 setDayCellFactory 中的 setDisable() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28914579/

相关文章:

java - TopCoder 最佳选择算法来自 SRM 489 DIV 2(500 分)

Java 小程序未在浏览器中显示

fonts - 如何在 JavaFX 中获得 Swing-looking 字体渲染?

JavaFX 8 : Populating a TableView in initialize method

jquery - 日期范围选择器的本地化

java - 解码具有嵌套子元素的复杂 xml

java - Hadoop上的支持 vector 机

javascript - 使用 HTML/CSS 图像显示 jquery datepicker

java - 如何向 Canvas 元素添加上下文菜单?

jquery - Vue 2 日期选择器组件