JTree始终以“编辑模式"显示所有节点. [英] JTree, always display all nodes in "edit mode"
问题描述
我正在显示自定义对象的树,并且已经设置了自定义自定义CellTreeEditor
和CellTreeRenderer
.
I'm displaying a tree of custom objects, and I've got custom custom CellTreeEditor
and CellTreeRenderer
set.
现在,我真正想要的是始终像在编辑模式"中一样显示所有对象.现在,我几乎完全实现了CellTreeRenderer.getTreeCellRendererComponent()
和CellTreeEditor.getTreeCellEditorComponent()
.这种工作,但是在执行任何编辑之前,我仍然必须单击节点以使其聚焦.
Now what I really want is to always display all objects as in "edit mode". Right now I have the CellTreeRenderer.getTreeCellRendererComponent()
and CellTreeEditor.getTreeCellEditorComponent()
implemented almost identically. This kind of works, but I still have to click a node to focus it before I can do any editing.
是否还有其他更明智的方法,例如说永远不使用渲染器,默认为我的CellTreeEditor
?
Is there any more sensible way of doing this, perhaps like saying no renderer should never be used, defaulting to my CellTreeEditor
?
******更新****
******UPDATE****
要弄清楚:我所拥有的是一棵看起来像这样的树(是的,它也看起来像胡扯,但这不重要):
To clearify: What I have is a tree looking like this (and yes, it also looks like crap, but that's beside the point):
现在,我通过具有渲染器和编辑器来完成此任务,该渲染器和编辑器从getTreeCell[Renderer|Editor]Component()
返回相同的组件.
Right now, I accomplish this by having a renderer and an editor that returns identical components from getTreeCell[Renderer|Editor]Component()
.
如果我单击渲染器提供的ComboBox的向下箭头,则在打开下拉菜单时它将略微闪烁,但随后会中断并由我的编辑器组件替换.这意味着我必须再次单击它才能打开下拉列表.这是我要避免的行为.
If I click on the down-arrow on the ComboBox supplied by the renderer, it will flicker slighty as it opens the dropdown, but then be interrupted and replaced by my editor component. This means I have to click it again to open the dropdown. This is the behaviour I want to avoid.
推荐答案
扩大我的评论:不,您不希望跨单元共享编辑器(讨厌的事情开始发生),而是添加一个TreeCellListener来侦听更改潜在顾客(也就是关注的)选择路径,然后明确开始在该路径上进行编辑
Expanding my comment: no, you don't want to have your editor shared across cells (nasty thingies start to happen) Instead, add a TreeCellListener which listens for changes in the lead (aka: focused) selection path and then explicitly start editing on that path
final JXTree tree = new JXTree();
tree.setEditable(true);
tree.expandAll();
TreeSelectionListener l = new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
final TreePath path = e.getNewLeadSelectionPath();
if (path != null) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
tree.startEditingAtPath(path);
}
});
}
}
};
tree.addTreeSelectionListener(l);
真正使它起作用的技巧是通常的:将自定义反应包装到invokeLater中,以确保树的内部更新完成
The trick to really make it work is the usual: wrap the custom reaction into an invokeLater to be sure the tree's internal update is complete
这篇关于JTree始终以“编辑模式"显示所有节点.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!