如何使用自定义模型更新树节点大小?

时间:2022-06-30 12:37:49

I am using custom model in my JTree. When the label of some node changes, I am passing TreeModelEvent to the listeners in the following way:

我在我的JTree中使用自定义模型。当某个节点的标签发生变化时,我会以下列方式将TreeModelEvent传递给侦听器:

       protected void fireNodeChanged(TreePath path) {
            TreeModelEvent evt = new TreeModelEvent(this, path);
            for(TreeModelListener listener : listeners) {
                listener.treeNodesChanged(evt);
            }
        }

where path is the path to changed node, including itself.

其中path是更改节点的路径,包括自身。

This causes the title change, but the size of it does not:

这会导致标题更改,但它的大小不会:

如何使用自定义模型更新树节点大小?

Swing truncates the title and adds ellipsis (pointed with red arrow).

Swing截断标题并添加省略号(用红色箭头指向)。

How to fix this? Note that I have custom model and can't call nodeChanged, I should emulate it.

如何解决这个问题?请注意,我有自定义模型,不能调用nodeChanged,我应该模仿它。

2 个解决方案

#1


0  

Without a snippet there are several guesses to make, but this situation seems familiar to me. Try adding a renderer to your JTree that performs an updateUI() on the rendering component before returning it:

没有一个片段可以做几个猜测,但这种情况对我来说似乎很熟悉。尝试将渲染器添加到JTree,在渲染组件上执行updateUI()之后再返回它:

tree.setCellRenderer(new DefaultTreeRenderer() {
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        Component c = super.getTreeCellRendererComponent(tree, value,
                selected, expanded, leaf, row, hasFocus);
        // You can do more changes here
        ((JComponent) c).updateUI();
        return c;
    }});

About the updating, if "emulating" means implementing it and does what's supposed to do, that's correct. As long as it implements TreeModel and the methods do what's supposed, JTree should do the rest.

关于更新,如果“模拟”意味着实现它并做了应该做的事情,这是正确的。只要它实现了TreeModel并且方法做了所谓的,JTree应该完成其余的工作。

The truncation might be related to the fact that trees and tables do not lay their "components" out but use renderers instead to paint them in faked containers.

截断可能与树和表没有放置“组件”但使用渲染器代替在伪造容器中绘制它们这一事实有关。

And, by the way, you can have a look at EventListenerList for a proper collection of event listeners.

顺便说一句,您可以查看EventListenerList以获取正确的事件侦听器集合。

#2


0  

Have you implemented addTreeModelListener(TreeModelListener l) in your custom TreeModel implementation? You'll want to call treeNodesChanged(TreeModelEvent e) on any registered listeners when your nodes are modified. Take a look at the source code for DefaultTreeModel for an example (the notification propagates up through the node's parents). Or, just have your custom model extend DefaultTreeModel and save yourself some time re-implementing all the listener stuff.

您是否在自定义TreeModel实现中实现了addTreeModelListener(TreeModelListener l)?在修改节点时,您需要在任何已注册的侦听器上调用treeNodesChanged(TreeModelEvent e)。看一下DefaultTreeModel的源代码示例(通知通过节点的父节点向上传播)。或者,让你的自定义模型扩展DefaultTreeModel并节省一些时间重新实现所有监听器的东西。

#1


0  

Without a snippet there are several guesses to make, but this situation seems familiar to me. Try adding a renderer to your JTree that performs an updateUI() on the rendering component before returning it:

没有一个片段可以做几个猜测,但这种情况对我来说似乎很熟悉。尝试将渲染器添加到JTree,在渲染组件上执行updateUI()之后再返回它:

tree.setCellRenderer(new DefaultTreeRenderer() {
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        Component c = super.getTreeCellRendererComponent(tree, value,
                selected, expanded, leaf, row, hasFocus);
        // You can do more changes here
        ((JComponent) c).updateUI();
        return c;
    }});

About the updating, if "emulating" means implementing it and does what's supposed to do, that's correct. As long as it implements TreeModel and the methods do what's supposed, JTree should do the rest.

关于更新,如果“模拟”意味着实现它并做了应该做的事情,这是正确的。只要它实现了TreeModel并且方法做了所谓的,JTree应该完成其余的工作。

The truncation might be related to the fact that trees and tables do not lay their "components" out but use renderers instead to paint them in faked containers.

截断可能与树和表没有放置“组件”但使用渲染器代替在伪造容器中绘制它们这一事实有关。

And, by the way, you can have a look at EventListenerList for a proper collection of event listeners.

顺便说一句,您可以查看EventListenerList以获取正确的事件侦听器集合。

#2


0  

Have you implemented addTreeModelListener(TreeModelListener l) in your custom TreeModel implementation? You'll want to call treeNodesChanged(TreeModelEvent e) on any registered listeners when your nodes are modified. Take a look at the source code for DefaultTreeModel for an example (the notification propagates up through the node's parents). Or, just have your custom model extend DefaultTreeModel and save yourself some time re-implementing all the listener stuff.

您是否在自定义TreeModel实现中实现了addTreeModelListener(TreeModelListener l)?在修改节点时,您需要在任何已注册的侦听器上调用treeNodesChanged(TreeModelEvent e)。看一下DefaultTreeModel的源代码示例(通知通过节点的父节点向上传播)。或者,让你的自定义模型扩展DefaultTreeModel并节省一些时间重新实现所有监听器的东西。