How to stop editing with DefaultCellEditor when a separate JBtton is pressed

不打扰是莪最后的温柔 提交于 2019-12-03 18:09:53

问题


I got a table with a custom TableCellEditor (extending DefaultCellEditor) with a JFormattedTextField as the editor component.

Now I got problem: when I press a separate button while editing. When the button is pressed, the editor remains "open and active" while I'd want it to stop editing so that the changes made would be available for the operations caused by the button.

So how to cause the editing to be stopped when a distinct button is pressed. I tried setFocusLostBehavior(JFormattedTextField.COMMIT) for the JFormattedTextField but it didn't seem to have effect.

Hope somebody has some good ideas =)


回答1:


You may try

jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);




回答2:


Thanks asalamon74, that works quite nicely.

There's debate over the thing at Sun Bug Database : Bug 4724980: JTable: Add API to control what happens to edits when table loses focus. (also other bug entries are found). The terminateEditOnFocusLost turns on "commit-or-cancel" behaviour that can be seen at the Java sources at the class JTable$CellEditorRemover

And yes, it's somewhat strange that it isn't documented at the APIs etc...




回答3:


Had also more issues with these things, see the question Java Swing : changing JTree selection while editingstopCellEditing() : how to get stopCellEditing() called before TreeListeners:valueChanged?



来源:https://stackoverflow.com/questions/377924/how-to-stop-editing-with-defaultcelleditor-when-a-separate-jbtton-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!