Java Swing:如何在 TreeListeners:valueChanged 之前调用 stopCellEditing()?
这是对这些早期问题的后续:
当我使用 terminateEditOnFocusLost
属性(如下所示)时,我的 CellEditor 在表格失去焦点时正确停止编辑:
jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
它也适用于我的 JButton。 在处理按钮按下操作之前,为我的 TableCellEditor 调用 stopCellEditing()
方法。 但是,当我将它与 JTree 一起使用并且树选择发生变化时,TreeSelectionListener.valueChanged
方法会在 stopCellEditing()
之前调用。
有谁知道是否有一种方法可以强制首先调用 stopCellEditing() ,或者我应该为这个问题找到一些解决方法?
This is a follow-up to these earlier questions:
- How to stop editing with DefaultCellEditor when a separate JBtton is pressed
- Sun Bug 4724980: JTable: Add API to control what happens to edits when table loses focus.
When I use the terminateEditOnFocusLost
property, like below, my CellEditor correctly stops editing when the table loses focus:
jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
It also works with my JButtons. The stopCellEditing()
method is called for my TableCellEditor before the button press action is processed. But when I use it with a JTree, and the tree selection changes, the TreeSelectionListener.valueChanged
method is called before stopCellEditing()
.
Does any one know if there is a way to force stopCellEditing()
to be invoked first, or should I just make up some work-around for this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JTree
在Swing
中没有类似的客户端属性。但是
SwingX
中JXTree
(从JTree
派生的类)执行以下操作:invokeStopEditing
。如果您无法使用
SwingX
,您可以随时查看JXTree
的源代码,看看这个 StopEditing 机制是如何工作的:JXTree SwingX 1.0 API 文档和 Javadoc
(转至源选项卡)
特别地,从第
974
行开始,创建一个侦听器来监视KeyboardFocusManager
等上的“permanentFocusOwner
”属性更改...JTree
does not have similar client property inSwing
.But
JXTree
, a derived class fromJTree
, inSwingX
does:invokeStopEditing
.If you can't use
SwingX
, you can always look at the source code ofJXTree
and see how this StopEditing mecanism works:JXTree SwingX 1.0 API Documentation and Javadoc
(go to Source tab)
Specially, starting from line
974
, a listener is created to monitor "permanentFocusOwner
" property change on theKeyboardFocusManager
etc...我不太确定我是否理解您有关
TreeSelectionListener
的问题以及它与调用的计时stopCellEditing()
方法之间的关系。 您正在创建自定义TreeCellEditor
吗? 如果是这样,有关此编辑器设置的更多信息将很有用。但是,您还引用了之前的一项,该项目涉及
JTable
上的单元格编辑、其对外部组件的焦点丢失以及这对编辑单元格的影响。 我将此视为您希望为JTree
提供类似的解决方案的暗示...如上所述,
JTree
并未实现属性的处理” TerminateEditOnFocusLost"
开箱即用。 这并不意味着您可以自己做。查看
JTable
的代码,它非常简单。 一个类放在一起,其唯一的工作是在焦点更改时确定 JTree 是否仍然具有焦点,如果没有,则调用 stopEditing() ,如果没有,则调用 stopEditing() 。失败则调用cancelEditing()
。 在这里,它适用于树:您会注意到您的树必须能够以某种方式被此类访问。 需要执行几个设置调用才能完成这项工作:
这应该具有额外的好处,可以使您的
JTree
的行为与您按JTable
时的行为相同。代码>JButton。I am not quite sure I understand your question with regards to the
TreeSelectionListener
and how it relates to the timingstopCellEditing()
method being called. Are you creating a customTreeCellEditor
? If so, some more info on the setup of this editor would be useful.However, you also referenced an earlier item which pertained to cell editing on a
JTable
, its loss of focus to an outside component, and the affect of this on the editing cell. I have taken that as a hint that you would like a similiar solution forJTree
...As noted,
JTree
does not implement the handling of the property for"terminateEditOnFocusLost"
out of the box. This doesn't mean that you can do it yourself.Looking at the code for
JTable
, it is pretty straight forward. A class is put together whose only job is to identify if theJTree
still has focus when there is a focus change, and if not, it callsstopEditing()
and if that fails it callscancelEditing()
. Here it is, adapted for a tree:You will note that your tree has to be accessible somehow to this class. There are a couple setup calls to perform to make this work:
This should have the added benefit of making your
JTree
behave the same way that yourJTable
behaves when you press aJButton
.