You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaFX中基于日期列表创建自定义DatePicker并联动ComboBox

JavaFX实现日期选择器禁用指定日期+联动时段下拉框

1. 补全Schedule实体类

根据给出的结构,先完成可直接使用的实体类:

import java.time.LocalDate;
import java.util.List;

public class Schedule {
    private List<LocalDate> dateList;
    private String morningSlot;
    private String eveningSlot;

    // 构造器
    public Schedule(List<LocalDate> dateList, String morningSlot, String eveningSlot) {
        this.dateList = dateList;
        this.morningSlot = morningSlot;
        this.eveningSlot = eveningSlot;
    }

    // Getter方法
    public List<LocalDate> getDateList() {
        return dateList;
    }

    public String getMorningSlot() {
        return morningSlot;
    }

    public String getEveningSlot() {
        return eveningSlot;
    }
}

2. 实现DatePicker禁用非允许日期

通过setDayCellFactory自定义日期单元格逻辑,判断日期是否在dateList内,不在则禁用并灰化:

DatePicker datePicker = new DatePicker();
// 初始化Schedule实例,替换为你的实际业务数据
Schedule schedule = new Schedule(
    List.of(LocalDate.now(), LocalDate.now().plusDays(1), LocalDate.now().plusDays(3)),
    "09:00-12:00",
    "14:00-17:00"
);

datePicker.setDayCellFactory(picker -> new DateCell() {
    @Override
    public void updateItem(LocalDate date, boolean empty) {
        super.updateItem(date, empty);
        // 空单元格或不在允许列表内则禁用
        if (empty || !schedule.getDateList().contains(date)) {
            setDisable(true);
            setStyle("-fx-background-color: #eeeeee;"); // 灰化禁用日期
        } else {
            setDisable(false);
            setStyle(""); // 恢复默认样式
        }
    }
});

3. 联动ComboBox渲染时段选项

监听DatePicker的选中值变化,当选中有效日期时,将对应早晚时段加载到下拉框:

ComboBox<String> slotComboBox = new ComboBox<>();

// 监听日期选择事件,更新下拉框内容
datePicker.valueProperty().addListener((obs, oldDate, newDate) -> {
    slotComboBox.getItems().clear();
    // 仅当选中日期在允许列表内时加载时段
    if (newDate != null && schedule.getDateList().contains(newDate)) {
        slotComboBox.getItems().addAll(schedule.getMorningSlot(), schedule.getEveningSlot());
    }
});

扩展:不同日期对应不同时段的场景

如果业务需求是每个日期有独立的早晚时段,需要调整Schedule结构为单日期对应时段:

// 调整后的Schedule结构
public class Schedule {
    private LocalDate date;
    private String morningSlot;
    private String eveningSlot;

    public Schedule(LocalDate date, String morningSlot, String eveningSlot) {
        this.date = date;
        this.morningSlot = morningSlot;
        this.eveningSlot = eveningSlot;
    }

    // Getter方法
    public LocalDate getDate() { return date; }
    public String getMorningSlot() { return morningSlot; }
    public String getEveningSlot() { return eveningSlot; }
}

此时联动逻辑需先匹配选中日期对应的Schedule:

List<Schedule> scheduleList = List.of(
    new Schedule(LocalDate.now(), "09:00-12:00", "14:00-17:00"),
    new Schedule(LocalDate.now().plusDays(1), "08:30-11:30", "13:30-16:30")
);

datePicker.valueProperty().addListener((obs, oldDate, newDate) -> {
    slotComboBox.getItems().clear();
    if (newDate != null) {
        // 查找对应日期的Schedule
        scheduleList.stream()
            .filter(s -> s.getDate().equals(newDate))
            .findFirst()
            .ifPresent(s -> {
                slotComboBox.getItems().addAll(s.getMorningSlot(), s.getEveningSlot());
            });
    }
});

内容的提问来源于stack exchange,提问作者Akhil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:05:24