- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有几个用于为约会调度程序应用程序选择日期和时间的ComboBox
,但是当我自定义CellFactory
时,它只能正确禁用正确的项目第一次单击并展开时。基本上,它应该禁用允许您选择 LocalDateTime.now()
之前的日期和时间的选择。以下视频显示了异常行为: https://www.youtube.com/watch?v=niGoIvVx9Y0
这是我的细胞工厂的代码:
startHour.setCellFactory((final ListView<String> param) -> {
return new ComboBoxListCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty || item == null) {
setText(null);
setGraphic(null);
} else {
LocalDateTime selected = createDateTime(startDate.getValue(), item,
startMinute.getValue(), startMeridian.getValue());
if(selected.isBefore(LocalDateTime.now())) {
System.out.print(item + ", ");
setDisable(true);
setStyle(COMBOCELL_DISABLED_COLOR);
}
}
}
};
});
startMinute.setCellFactory((final ListView<String> listView) -> {
return new ComboBoxListCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty || item == null) {
setText(null);
setGraphic(null);
} else {
LocalDateTime selected = createDateTime(startDate.getValue(), startHour.getValue(),
item, startMeridian.getValue());
if(selected.isBefore(LocalDateTime.now())) {
setDisable(true);
setStyle(COMBOCELL_DISABLED_COLOR);
}
}
}
};
});
startMeridian.setCellFactory((final ListView<String> listView) -> {
return new ComboBoxListCell<String>() {
@Override public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty || item == null) {
setText(null);
setGraphic(null);
} else {
LocalDateTime selected = createDateTime(startDate.getValue(), startHour.getValue(),
startMinute.getValue(), item);
if(selected.isBefore(LocalDateTime.now())) {
setDisable(true);
setStyle(COMBOCELL_DISABLED_COLOR);
}
}
}
};
});
所选的LocalDateTime
是使用以下方法计算的:
private LocalDateTime createDateTime(LocalDate date, String hour, String minute, String meridian) {
String dateTimeStr = date + " " + hour + " " + minute + " " + meridian;
return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd h m a"));
}
在 startHour#setCellFactory
中,我插入了一个 println
语句,显示所选项目在 LocalDateTime 之前发生时转换为
,这是我得到的输出:LocalDateTime
。 now()
1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 12, 12, 1, 9, 12, 1, 9, 12, 2, 1, 7, 9,
当前时间为上午 9:48,所选时间为上午 10:00,因此似乎满足预期条件。然而,正如视频所示,第一次扩展 ComboBox
后,行为非常不稳定。我有类似的 DatePicker
节点单元工厂,它们工作得很好。我是否误解了 ComboBox#setCellFactory
的用途?
最佳答案
对于startHour
,当任何单元格的当前项目、选定的开始日期、选定的开始分钟或选定的子午线发生更改时,您需要(可能)更新单元格的禁用状态。您可以通过绑定(bind)来完成此操作。
(请注意,您使用了错误的单元格类别。ComboBoxListCell
用于在使用组合框作为编辑器的列表中显示可编辑单元格。您只需要一个普通的 ListCell
即可。)
startHour.setCellFactory((final ListView<String> param) -> {
ListCell<String> cell = new ListCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item); // works in empty case as item will be null, as desired
}
};
cell.disableProperty().bind(Bindings.createBooleanBinding(
() -> {
if (cell.getItem() == null) return false ;
LocalDateTime selected = createDateTime(startDate.getValue(), cell.getItem(),
startMinute.getValue(), startMeridian.getValue());
return selected.isBefore(LocalDateTime.now());
},
cell.itemProperty(),
startMinute.valueProperty(),
startMeridian.valueProperty(),
startDate.valueProperty()
));
return cell ;
});
其他细胞工厂将类似。
对于样式,我强烈建议您使用外部样式表,其规则如下:
.combo-box .list-cell:disabled {
/* style for disabled cell here */
}
这使代码更加简单,并且意味着只有一个位置可以为所有相关的禁用组合框单元格定义样式。
请注意,一旦显示了代表当前时间之后的时间的单元格(因此它已启用),如果它显示的时间足够晚以至于不再晚于当前时间,则它不会自动禁用。 (例如,如果代表 2017 年 4 月 24 日下午 1 点的单元格在当天中午 12:55 首次显示,则该单元格将被启用。如果 10 分钟后再次显示该单元格且没有其他 UI 更改导致 updateItem()
调用,它应该被禁用,但不会。)一个完整的解决方案将使绑定(bind)还观察“当前时间”的变化,为此您可以这样做:
ObjectProperty<LocalDateTime> clock = new SimpleObjectProperty<>(LocalDateTime.now());
Timeline clockwork = new Timeline(new KeyFrame(Duration.seconds(1),
e -> clock.set(LocalDateTime.now())));
clockwork.setCycleCount(Animation.INDEFINITE);
clockwork.play();
然后添加clock
到绑定(bind)中的依赖值列表:
cell.disableProperty().bind(Bindings.createBooleanBinding(
() -> {
if (cell.getItem() == null) return false ;
LocalDateTime selected = createDateTime(startDate.getValue(), cell.getItem(),
startMinute.getValue(), startMeridian.getValue());
return selected.isBefore(clock.get());
},
cell.itemProperty(),
startMinute.valueProperty(),
startMeridian.valueProperty(),
startDate.valueProperty(),
clock
));
关于java - 如何使 ComboBox CellFactory 正确地动态禁用项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590075/
我在 Web 应用程序中尝试了一些字节码操作,到目前为止,效果很好。现在我需要在代码中的某些特定位置注入(inject)一些字节代码。我用 switch 语句和 method.inserAt(); 尝
我正在尝试对数组中的对象值求和,然后计算每个值相对于总数的百分比。 这里是数组: [ { "ratiototale": [ [ { "0":
我在接口(interface)中有以下方法.. Task> SearchAsync(TU searchOptions); 效果很好。 现在我正在尝试进行单元测试以测试何时出现问题 - 代码会抛出异常。
我似乎无法让 reveal 开始工作,我已经严格遵守所有内容,添加 modernizr,添加插件,添加 css(reveal 显然包含在 foundation 中),在开始时初始化 foundatio
我正在尝试移植 raspberrypi's userspace 的位从 C 到 golang 的代码,我遇到了一个涉及 ioctl() 的程序。 我在执行 C 代码时遇到了问题 #define MAJ
我一直在尝试用纯 Java 制作一个简单的游戏,但在绘图时遇到了问题。我正在尝试保持相对较高的帧速率,但是 JFrame.repaint() 不能被“强制”并且只是在下一个可用机会重绘帧的请求这一事实
给定一个字符串 "2*(i+j) = 20 我想返回 ((lambda x: x >= 20), ['i']) 我可以然后直接提供给 constraint。 最佳答案 您正在寻找 eval 的替代方法
我对改进我的 javascript 代码以使其成为正确的 OOP 很感兴趣....目前我倾向于做这样的事情: jQuery(document).ready(function () { Page
我有一个调用两个动画 Action 的事件监听器。不幸的是,它们的开始有少量错开(例如,函数中的第一个首先开始)。 有谁知道正确同步它们的方法吗? 这是我的代码: $("#nav ul li a").
我只需要检查目录是否存在!但是,如果目录是“E:\Test”,其中 E: 是 CD/DVD 驱动器,并且上面没有插入磁盘,我会看到以下 Delphi 和 Windows 问题。 第一种方法: func
同样的问题: https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my
如果您跳转到 this question 中的第一个答案你会看到他使用 Employee.prototype = new Person(); 将 Person.prototype 继承到 Employ
我需要知道如何正确地遍历元素的 ArrayList 并计算元素在列表中出现的次数,而无需事先知道该元素。我尝试了几种方法并且有一种目前有效,但我觉得它很丑陋且不合适。 为了更深入地解释,我有一个 ja
我有一个用 Python 编写的(非常基本但工作完美的)AWS lambda 函数,但是它具有嵌入式凭证以连接到:1)外部网络服务2) DynamoDB 表。 该函数的作用相当基本:它针对服务发布登录
我很好奇 Tornado 推荐的查询 Redis(或任何数据库)的方法是什么。 我见过一些像 https://gist.github.com/357306 这样的例子但他们似乎都在使用对 redis
这更像是一个“我做得对吗”的问题。 快速背景故事:我已经构建了一个 gradle 插件(在一个独立的 gradle/groovy 项目中)。我在另一个 java 项目中使用它。客户项目通过以下方式引用
在我的代码中,我有: function handleMessage() { const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_
我正在努力在 Linux 中刷新嵌入式设备的先前 ROM 转储。我以前的转储包含 oob 数据。我是用nandwrite -n -N -o/dev/mtd0 backup.bin写的,然后再做一次RO
我正在尝试使用 go 为 react-router 提供服务,我已经做到了,但我遇到了麻烦,我认为我做的方式不正确,或者不完整。我正在使用 Mux。我遇到的麻烦是当我按下 时在我的应用程序中,它正确
我正在尝试 promise JSON.parse 方法,但不幸的是没有任何运气。这是我的尝试: Promise.promisify(JSON.parse, JSON)(data).then((resu
我是一名优秀的程序员,十分优秀!