JTextField in JTree - make it editable

断了今生、忘了曾经 提交于 2019-12-04 16:40:06
Eric Leibenguth

In principle you should cleanly separate the renderer and the editor. In your case, they are closely tied together (in fact, the editor returns the JTextField instance of the renderer)... A renderer is not supposed to be editable. I doubt in your case that the editor is ever called.

I see two possible solutions:

  • Heavily refactor your code, to separate rendering and editing (e.g. rendering with JLabel and editing with JTextField -- or simply using a DefaultTreeCellEditor -- example here). Let the tree determine when the edit starts, when to call the editor, when to cancel an edit, etc...
  • Or you can by-pass the standard editing mechanism (as you are already doing more or less): Completely remove the TreeCellEditor from your code and let the renderer do all the work. Add appropriate listeners to your JTextField and JCheckBox inside the renderer, and update the model when changes are made. (Note that one drawback is that you'll have a primitive editing mechanism: e.g. the edit is not cancelled if user presses ESC).

As to:

And I do not know how to add a JLabel in the same line of the JTextField

Just let your renderer return a JPanel to which you add the JTextField and the JLabel.

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