- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
是否有任何简单的方法让 TreeTableView(或 TableView)尝试在失去焦点时提交值?
不幸的是,我没有成功使用 javafx TableCellFactories 的任何默认实现,这就是为什么我尝试了我自己的 TreeTableCell 实现以及一些不同的 tableCell 实现,例如来自 Graham Smith 的实现。 ,这似乎是最直接的,因为它已经为失去焦点实现了一个钩子(Hook),但是该值从未提交并且用户更改被重置为原始值。
我的猜测是,每当失去焦点时,受影响的 Cell 的 editingProperty 始终已经为 false,这导致 Cell 永远不会在 focusLost 上提交值。这里是原始 (oracle-)TreeTableCell 实现 (8u20ea) 的相关部分,这导致我的方法失败:
@Override public void commitEdit(T newValue) {
if (! isEditing()) return; // <-- here my approaches are blocked, because on focus lost its not editing anymore.
final TreeTableView<S> table = getTreeTableView();
if (table != null) {
@SuppressWarnings("unchecked")
TreeTablePosition<S,T> editingCell = (TreeTablePosition<S,T>) table.getEditingCell();
// Inform the TableView of the edit being ready to be committed.
CellEditEvent<S,T> editEvent = new CellEditEvent<S,T>(
table,
editingCell,
TreeTableColumn.<S,T>editCommitEvent(),
newValue
);
Event.fireEvent(getTableColumn(), editEvent);
}
// inform parent classes of the commit, so that they can switch us
// out of the editing state.
// This MUST come before the updateItem call below, otherwise it will
// call cancelEdit(), resulting in both commit and cancel events being
// fired (as identified in RT-29650)
super.commitEdit(newValue);
// update the item within this cell, so that it represents the new value
updateItem(newValue, false);
if (table != null) {
// reset the editing cell on the TableView
table.edit(-1, null);
// request focus back onto the table, only if the current focus
// owner has the table as a parent (otherwise the user might have
// clicked out of the table entirely and given focus to something else.
// It would be rude of us to request it back again.
ControlUtils.requestFocusOnControlOnlyIfCurrentFocusOwnerIsChild(table);
}
}
我成功地覆盖了这个方法并在调用原始 commitEdit() 方法之前“手动”提交了值,但这会导致像 enter 这样的键上的提交提交值两次(在键上 + 在焦点丢失时)。此外,我根本不喜欢我的方法,所以我想知道是否还有其他人以“更好”的方式解决了这个问题?
最佳答案
经过一些挖掘,结果是罪魁祸首(又名:在 textField 失去焦点之前取消编辑的合作者)是处理 mousePressed 的 TableCellBehaviour/Base:
simpleSelect(..)
edit(-1, null)
不幸的是,一个 hackaround 需要 3 个合作者
simpleSelect(...)
的 TableCellBehaviour,它在调用 super 之前调用额外的 api(而不是 edit(-1..))一些代码片段(full code):
// on XTableView:
public void terminateEdit() {
if (!isEditing()) return;
// terminatingCell is a property that supporting TableCells can listen to
setTerminatingCell(getEditingCell());
if (isEditing()) throw new IllegalStateException(
"expected editing to be terminated but was " + getEditingCell());
setTerminatingCell(null);
}
// on XTableCellBehaviour: override simpleSelect
@Override
protected void simpleSelect(MouseEvent e) {
TableCell<S, T> cell = getControl();
TableView<S> table = cell.getTableColumn().getTableView();
if (table instanceof XTableView) {
((XTableView<S>) table).terminateEdit();
}
super.simpleSelect(e);
}
// on XTextFieldTableCell - this method is called from listener
// to table's terminatingCell property
protected void terminateEdit(TablePosition<S, ?> newPosition) {
if (!isEditing() || !match(newPosition)) return;
commitEdit();
}
protected void commitEdit() {
T edited = getConverter().fromString(myTextField.getText());
commitEdit(edited);
}
/**
* Implemented to create XTableCellSkin which supports terminating edits.
*/
@Override
protected Skin<?> createDefaultSkin() {
return new XTableCellSkin<S, T>(this);
}
注意:TableCellBehaviour 的实现在 jdk8u5 和 jdk8u20 之间发生了巨大变化(黑客的乐趣 - 不适合生产使用 ;-) - 在后者中覆盖的方法是 handleClicks(..)
顺便说一句:大量投票给 JDK-8089514 (在旧的 jira 中是 RT-18492)可能会加速核心修复。不幸的是,至少需要作者角色才能在新跟踪器中投票/评论错误。
关于java - 如何为 TableView/TreeTableView 启用 focusLost 提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694616/
我正在 YouTube 类(class)中学习 Java,显然遇到了这个问题。我尝试在 focusLostevento 中创建一个循环。但是,它无法正常工作(程序会忽略它)。如果有人有解决方案,我会很
我已经将我的 vim 编辑器(我使用 MacVim)设置为在失去焦点时自动保存文件: autocmd FocusLost * silent! wall 我还使用这个自动命令自动从 python 文件中
我已经将我的 vim 编辑器(我使用 MacVim)设置为在失去焦点时自动保存文件: autocmd FocusLost * silent! wall 我还使用这个自动命令自动从 python 文件中
当我尝试从 focusLost 上的文本字段中提取数据时,出现这个奇怪的错误。 所以我有一个文本字段,其想法是当用户在单击其他地方后输入数据时,必须验证输入。 这是构造函数中的行,我将榕树监听器添加到
FocusLost 事件在 WPF 的组件上正常工作对于用户控件,它是不同的: 如果用户控件中的任何控件被单击或获得焦点,用户控件的 FocusLost 将立即被触发!我怎样才能阻止它? 我无法解决这
我使用 SWT Text 字段,我想防止它在没有输入文本或只有空格时失去焦点。如果是这种情况,我也想通知用户。 我当前的解决方案是在 FocusListener 的 focusLost 方法中检查它。
这是我在事件方法中的代码: Code private void txtCampusSearchFocusGained(java.awt.event.FocusEvent evt) {
我希望在从多个表中选择任何行时在文本框中正确显示数据。 当我右键单击表 1 的任意行时,我会得到 table1: 1。如果我单击表 2 的任何行,我也会得到 table1: 1 (但它应该是 tabl
当 JFrame 失去焦点时,我需要将鼠标设置在特定位置,但在最小化时则不需要。当框架失去焦点(FocusListener)时,我使用 Robot 通过启动调用机器人移动鼠标的计时器来将鼠标设置在位置
我有一个带有快捷键的 MenuItem,用于临时保存 JTextFied 的数据并打印它 保存功能在 JTextField 的 FocusLost 事件上调用,打印通过 MenuItems actio
我在 JFrame 中有 5 个 JTextField,并且我在循环中向所有这些 JTextField 添加了一个 FocusListener。 在 focusGained() 事件中,我使用 set
我的代码中有一个 JCombobox。我添加了 FocusLost 事件。但无论如何都没有开火。我已经尝试了很多时间,但没有找到解决方案。 jcbItemType.addFocusListener(n
只要 JTable 中存在丢失焦点,我就需要捕获现有单元格的行和列。 但是,下面的条件始终为 false,因为源始终是 JTextField 或 JComboBox。 public void focu
我们的一位客户报告我们的应用程序出现异常。问题是,我完全无法理解如何重现此错误。 代码如下: btn.addActionListener(new ActionListener() { @Ove
我在网页中嵌入了一个 Java JApplet。JApplet 窗口包含扩展 JPanel 的类的单个实例 - 与 JApplet 大小相同。当用户单击面板中的按钮时,小程序可以生成弹出窗口 (JFr
是否有任何简单的方法让 TreeTableView(或 TableView)尝试在失去焦点时提交值? 不幸的是,我没有成功使用 javafx TableCellFactories 的任何默认实现,这就
我试图在我的组合框中绑定(bind)一个 focuslost 事件,但它没有发生。 这是我的代码: jComboBox1.addFocusListener(new FocusListener(){
我有一个 Java Swing 应用程序,它有许多 JTextField 和一个数据模型。 离开文本字段(焦点丢失)时,文本将写入模型。到目前为止,一切顺利。 有 JMenu-Actions 从模型读
我有一个 JTree,其中树的每个节点代表一些用户数据。数据是可编辑的并存储在文件/数据库中。有一堆 JTextField 可让您编辑/更新节点的用户数据。当 JTextField 失去焦点时,会触发
如果在单击另一个组件时弹出菜单仍处于打开状态,则该组件不会获取事件,因为它可能已被弹出菜单占用。一般所有 JPopupmenus 都会发生这种情况。这仅发生在带有 Windows LAF (Windo
我是一名优秀的程序员,十分优秀!