焦点丢失后推迟事件队列 [英] Postpone Event Queue after Focus Lost
问题描述
我有一个定制单元格编辑器的 JTable
。编辑器实现了 FocusListener
,所以我可以检查单元格的内容是否有效,如果用户点击远离单元格。
我想在 focusLost
中使用 JOptionPane
(在 EventDispatchThread
)允许用户选择是恢复到旧值还是接受调整后的值。
这是问题;如果用户正在编辑一个单元格,然后点击一个离开表格的按钮, JOptionPane
这就是我想要发生的事情:
- 用户编辑单元格
- 用户单击按钮
- 单元格检测到焦点丢失
JOptionPane显示和用户选择操作 - JOptionPane关闭和单元格的值设置
- 按钮的actionListeners调用
用户编辑单元
$ b
$ b
$ b 可以推迟按钮的动作事件直到 JOptionPane
关闭从其他线程看, JDialog
有一些神奇的功能,以确保事件分派继续进行,所以Dialog本身可以处理事件。
TableCellEditor#stopCellEditing()
,显示对话框,如果值无效,则返回false。 要在焦点丢失时自动停止表格编辑,请使用表格。 putClientProperty(terminateEditOnFocusLost,true);
,但我不认为这将停止按钮动作侦听器运行。相反,我通常会停止actionPerformed中的表格编辑,并在返回false时执行任何操作(或者在适当的时候取消编辑,例如,如果操作是删除该表格行)。
I have a JTable
with a custom cell editor. The editor implements FocusListener
so I can check if the cell's contents is valid if the user clicks away from the cell.
I'd like to use a JOptionPane
within focusLost
(in the EventDispatchThread
) to allow the user to select whether to revert to an old value or accept an adjusted value.
Here's the problem; if the user is editing a cell, then clicks a button away from the table, the button's actionlisteners
are alerted before JOptionPane
has returned.
This is what I'd like to happen:
- User edits cell
- User clicks button
- Cell detects focus lost
- JOptionPane displayed and user selects action
- JOptionPane closes and cell's value set
- Button's actionListeners called
Instead, this is happening:
- User edits cell
- User clicks button
- Cell detects focus lost
- JOptionPane displayed and user selects action
- Button's actionListeners called
- JOptionPane closes and cell's value set
Is it possible to postpone the button's action events until after the JOptionPane
has closed?
From other threads, I've read that JDialog
does some magic to ensure event dispatching continues so the Dialog itself can handle events.
Put your validation logic inside TableCellEditor#stopCellEditing()
, showing your dialog and returning false if the value is not valid.
To automatically stop table editing on focus lost, use table.putClientProperty("terminateEditOnFocusLost", true);
, but I don't think that will stop the buttons action listener from running. Instead I usually stop the table edit in the actionPerformed and do nothing when false is returned (or cancel editing when appropriate, for example if the action is to delete that table row).
这篇关于焦点丢失后推迟事件队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!