- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 TableView
中使用 CheckBoxTableCell
我不想在用户单击复选框时执行某些代码。此代码需要在检查 CheckBox 之前以及更改关联的属性之前执行。
由于 CheckBoxTableCell
不会触发 startEdit()
、commitEdit()
等,因此我需要另一个可以放置代码的位置。
updateItem()
会来不及了,因为那里的值已经改变了。
有人知道我可以插入代码的其他地方吗?还是我最好只编写自己的 CheckBoxTableCell?
对于那些想进一步思考的人:当用户选中复选框时,我想运行一个代码来检查他的决定是否会在以后给他带来问题。当属性更改时,我有一个将新值上传到数据库的 changeListener。如果可能有任何问题(我的代码返回 true),那么应该有强制性的错误消息:“你真的想这样做......blabla”并且只有当他确认应该实际检查复选框时,值才会改变并上传到数据库。
这就是我设置 CheckBoxTableCell
的方式:
//init cell factories
Callback<TableColumn<PublicMovie, Boolean>, TableCell<PublicMovie, Boolean>> checkBoxCellFactory
= (TableColumn<PublicMovie, Boolean> p) -> new CheckBoxTableCell<>();
//Assign Column
allMoviesCheckBoxColumn = (TableColumn<PublicMovie, Boolean>) allMoviesTableView.getColumns().get(0);
//Set CellValueFactory
allMoviesCheckBoxColumn.setCellValueFactory(cellData -> cellData.getValue().getSomeBooleanProperty();
someBooleanProperty
有一个监听器。当更改为 true 时,statusProperty
更改为 1
。
此代码片段位于 TableView
基础类的构造函数中。每当用户单击 CheckBox 时,它就会被执行。
this.status.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
DataHandler.fireOnChangedEvent(PublicMovieChangeListener.STATUS_CHANGED, getMovie(), newValue, oldValue);
});
DataHandler
包含一个 Listeners
列表(到目前为止只有一个)。当 DataHandler
调用 onChange
事件时执行以下代码:
@Override
public void onChange(int changeID, PublicMovie changedPublicMovie, Object newValue, Object oldValue) {
switch (changeID) {
case STATUS_CHANGED:
boolean mayUpdate = check();
//check is a placeholder for the code that checks for problems.
//It will return false if there might be a problem.
if(!mayUpdate){
mayUpdate = ErrorDialogs.getConfirmationDialog("Überfüllung!", header, content);
}
if (mayUpdate) {
updateMovie(changedPublicMovie);
} else {
changedPublicMovie.getStatusProperty().set((int) oldValue);
refreshAllMoviesTable();
}
break;
case THREE_DIM_CHANGED:
updateMovie(changedPublicMovie);
break;
case CHILDREN_CHANGED:
updateMovie(changedPublicMovie);
break;
}
您在这里看到的是我试图撤销已更改的属性...但该复选框仍处于选中状态。因此,我正在寻找一种在更改属性之前执行检查的方法。
最佳答案
基于 CheckBoxTableCell
的文档:
If you want to be notified of changes, it is recommended to directly observe the boolean properties that are manipulated by the
CheckBox
.
我并不是说使用 CheckBoxTableCell
是不可能的,但很可能不是那么简单。
在大多数情况下,可以使用 EventFilter
来管理这种“预选”事件。
因此,您可以尝试将普通的 TableColumn
与使用这些 EventFilter
的适当 cellFactory
一起使用。
我的例子
使用的cellFactory
:
tableCol.setCellFactory(p -> {
CheckBox checkBox = new CheckBox();
TableCell<Person, Boolean> tableCell = new TableCell<Person, Boolean>() {
@Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null)
setGraphic(null);
else {
setGraphic(checkBox);
checkBox.setSelected(item);
}
}
};
checkBox.addEventFilter(MouseEvent.MOUSE_PRESSED, event ->
validate(checkBox, (Person) cell.getTableRow().getItem(), event));
checkBox.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if(event.getCode() == KeyCode.SPACE)
validate(checkBox, (Person) cell.getTableRow().getItem(), event);
});
tableCell.setAlignment(Pos.CENTER);
tableCell.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
return tableCell;
});
和使用的validate
方法:
private void validate(CheckBox checkBox, Person item, Event event){
// Validate here
event.consume();
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");
// Set the checkbox if the user want to continue
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK)
checkBox.setSelected(!checkBox.isSelected());
}
是做什么的:
它呈现一个简单的 CheckBox
并向此 CheckBox
添加两个 EventFilter
。第一个在鼠标按下时执行,第二个在按键时执行(此处使用 KeyCode.SPACE
)。它们都调用了 validate
方法。 validate
方法使用事件(确保 CheckBox
的选择状态未被修改),“验证”输入(我这边没有验证)然后显示确认对话。如果用户同意,则设置 CheckBox
的 selectedProperty
。
关于JavaFX CheckBoxTableCell 捕获 ClickEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270033/
我在面板上有 10 个标签 - 和 10 个相同的 ClickEvents(更改背景颜色)。 我怎样才能减少代码,即编写一个通用程序? 像这样的东西: foreach (Control c in pa
我有一个按钮,当您单击它时会显示一个对话框。 但是,如果您单击该按钮,它会显示对话框,同时它将关闭然后再次打开。 似乎对话框的 closeEvent 在按钮上的 clickEvent 之前抛出。因此,
我正在使用循环函数将一些元素填充到 div 中。该 div 的每个元素都有 onClick 函数。问题是 onclick 函数没有触发。 这是我的代码: var Source = "#boxcard"
这是代码,谁能告诉我如何让onClick事件运行? Activity 的 xml 文件,其中包含一个抽屉导航 fragment 、一个 ImageView 、一个 TextView 和一个 Image
我创建了 NavigationView 并设置了 3 个项目。 如下图所示: 现在我想要的是,当我点击特定项目时,它应该启动一个 Activity 。 如何实现? 谢谢! 最佳答案 我从您的问题中可以
当我点击元素栏中的元素时,它什么都不做,我可以将它拖到任何我想要的地方。然后我重新打开库存,元素又回来了。我希望单击该项目(在本例中为通知项目)以切换通知 boolean 值并关闭库存。请帮忙。这可能
当我单击一个按钮时,必须显示一个下拉菜单。当我点击页面中的任何地方时,它需要隐藏。我遇到的问题是:当我运行该页面时,我可以看到下拉菜单而不是点击事件。我尝试了不同的 div 标签,但没有效果。 演示:
在 TableView 中使用 CheckBoxTableCell 我不想在用户单击复选框时执行某些代码。此代码需要在检查 CheckBox 之前以及更改关联的属性之前执行。 由于 CheckBoxT
我正在使用 MVVM 在 WPF 中创建一个应用程序,但我处于困境,因为我无法将命令绑定(bind)到 Canvases ClickEvent。 我想在 Canvas 上放置一些矩形并将其位置保存到数
我正在尝试创建一个简单的按钮,当单击它时,我想触发 FormPanel 内的隐藏 FileUpload 小部件。到目前为止我做了两件事: 我创建了一个原生 JavaScript单击 FileUploa
我有一个带有一些文本字段的基本 BeanFieldGroup 编辑器。我公开了 save-Button 以便 Presenter 类可以注册到它。 问题:当 View 中的 TextField 元素出
HTML: content Javascript: this.content = ko.observable(false); //Named function triggers at
下面是我今天想到并创建的一个小项目的链接。我无法使用 IIFE 让按钮多次工作。我已经在没有 IIFE 的情况下解决了这个问题,但我的理解是最好将代码设为私有(private)...任何建议将不胜感激
我有一个 ListView,可将数据从数据库填充到复杂的 ListView 中。数据库的“gotoURL”列中的字符串是使用 putExtra Intent 将链接加载到 WebView 的 URL。
共有三个图标。我把图标放在整数数组上。我怎样才能给他们点击监听器事件。 icon0、icon1、icon2 事件不同。我想给他们点击事件。但我做不到。如何分别给图标点击事件 我使用 wheel.gam
我通过以下方式启用了 google map mylocation 标记: googleMap.isMyLocationEnabled = true 然后它将显示以下内容: 但现在我想要在用户触摸此标记
是否可以向 ImageButton 添加点击事件? 最佳答案 是的!你可以像这样实现 onclick 方法 ImageButton ib = (ImageButton)findViewById(R.i
我希望在我的网站上有一个模拟 CTRL + +(放大)的按钮。所以我需要创建 2 个按键或其他东西。我有以下触发 1 个按键的命令: Stackoverflow f
尝试合并时遇到以下问题 CSS用 JS 转换事件处理程序。我知道如何使用 CSS 过渡属性,例如: div { width: 50px; height: 50px; backg
“当用户单击描述 TextView 和 Like ImageButton 时,我尝试创建 Toast 消息。但 list_item 未响应触摸事件” “我询问了很多其他人关于改变焦点的问题,但他们都没
我是一名优秀的程序员,十分优秀!