是否可以自定义JTree节点?

时间:2022-10-27 12:37:22

I want to try to implement a tree like this: Each node of the tree is a self defined component,which is composed of a JComboBox and a JTextField. Each time I select an item in the JComboBox, this node will generate three new sub nodes as leaves.

我想尝试实现这样的树:树的每个节点都是一个自定义组件,它由JComboBox和JTextField组成。每次我在JComboBox中选择一个项目时,该节点将生成三个新的子节点作为叶子。

Is it possible to do that? I have tried using the TreeCellRender, but it seems that it just paints the combobox and the textfield, but the combobox is not selectable.

有可能吗?我尝试过使用TreeCellRender,但它似乎只是绘制了组合框和文本框,但组合框不可选。

1 个解决方案

#1


2  

What you want to do is editing the JTree. For this a renderer is not sufficient. Renderers are only used to 'render' the JTree, so are only sufficient if you have a read-only JTree.

你想要做的是编辑JTree。为此,渲染器是不够的。渲染器仅用于“渲染”JTree,因此只有拥有只读JTree才足够。

If you want to edit the model behind the JTree as well, you have to set an editor on it.

如果你想编辑JTree背后的模型,你必须在它上面设置一个编辑器。

The reason that your combobox is not selectable is explained in the Swing table tutorial, in the part about renderers and editors. The same applies for trees (but that tutorial does not contain such a section).

您可以选择组合框的原因在Swing表教程中,有关渲染器和编辑器的部分中进行了解释。这同样适用于树(但该教程不包含这样的部分)。

#1


2  

What you want to do is editing the JTree. For this a renderer is not sufficient. Renderers are only used to 'render' the JTree, so are only sufficient if you have a read-only JTree.

你想要做的是编辑JTree。为此,渲染器是不够的。渲染器仅用于“渲染”JTree,因此只有拥有只读JTree才足够。

If you want to edit the model behind the JTree as well, you have to set an editor on it.

如果你想编辑JTree背后的模型,你必须在它上面设置一个编辑器。

The reason that your combobox is not selectable is explained in the Swing table tutorial, in the part about renderers and editors. The same applies for trees (but that tutorial does not contain such a section).

您可以选择组合框的原因在Swing表教程中,有关渲染器和编辑器的部分中进行了解释。这同样适用于树(但该教程不包含这样的部分)。