Swing:如何使用带有JTextPanes的JTree作为节点?

时间:2021-06-24 12:36:58

JTree uses DefaultTreeCellRenderer as cell renderer.
This class is a subclass of JLabel.

JTree使用DefaultTreeCellRenderer作为单元格渲染器。这个类是JLabel的一个子类。

I want to use JTree with more complex elements than JLabel, such as JTextPane.

我希望使用JTree来处理比JLabel更复杂的元素,比如JTextPane。

Problem is: I can't subclass DefaultTreeCellRenderer, because it would still be a JLabel.

问题是:我不能子类化DefaultTreeCellRenderer,因为它仍然是一个JLabel。

Writing an own TreeCellRenderer is too complex.
Why? Because: DefaultTreeCellRenderer has 17 fields, and does much more than just implementing TreeCellRenderer's getTreeCellRendererComponent(...).

编写自己的TreeCellRenderer太复杂了。为什么?因为:DefaultTreeCellRenderer有17个字段,它不仅仅实现了TreeCellRenderer的getTreeCellRendererComponent(…)。

What simple solution can you devise?

你能想出什么简单的解决办法?

I need the tree elements to be JTextPanes to be able to perform complex formatting.

我需要树元素为JTextPanes才能执行复杂的格式。

2 个解决方案

#1


1  

public class JTextPaneTreeCellRenderer extends JTextPane implements TreeCellRenderer {

Method:

方法:

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

    // do stuff to this instance of JTextPane
    setEditable(selected);
    setText(value.toString()); //Assumes whatever you stuck in the tree has pretty toString

    if (leaf)
        setBackgroundColor(Color.RED);
    return this;
}

You don't need to be as complex as the default implementation. Making the Renedere a subclass of JTextPane will make the implementation of the method much easier.

您不需要像默认实现那样复杂。使Renedere成为JTextPane的子类将使方法的实现更容易。

#2


0  

You could still subclass DefaultTreeCellRenderer and override just the getTreeCellRendererComponent method to return the JTextPane component formatted as you would like it. You would still need to do a lot of the stuff that DefaultTreeCellRenderer does in its implementation, but you would not have to bother with the maintenance aspect of those 17 or so fields.

您仍然可以子类化DefaultTreeCellRenderer,并仅重写getTreeCellRendererComponent方法,以返回按照您希望的格式格式化的JTextPane组件。您仍然需要做许多DefaultTreeCellRenderer在其实现中所做的事情,但是您不需要操心那些17个字段的维护方面。

edit removed the JTextField editor solution after reading comments

编辑在读取注释后删除了JTextField编辑器解决方案

#1


1  

public class JTextPaneTreeCellRenderer extends JTextPane implements TreeCellRenderer {

Method:

方法:

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

    // do stuff to this instance of JTextPane
    setEditable(selected);
    setText(value.toString()); //Assumes whatever you stuck in the tree has pretty toString

    if (leaf)
        setBackgroundColor(Color.RED);
    return this;
}

You don't need to be as complex as the default implementation. Making the Renedere a subclass of JTextPane will make the implementation of the method much easier.

您不需要像默认实现那样复杂。使Renedere成为JTextPane的子类将使方法的实现更容易。

#2


0  

You could still subclass DefaultTreeCellRenderer and override just the getTreeCellRendererComponent method to return the JTextPane component formatted as you would like it. You would still need to do a lot of the stuff that DefaultTreeCellRenderer does in its implementation, but you would not have to bother with the maintenance aspect of those 17 or so fields.

您仍然可以子类化DefaultTreeCellRenderer,并仅重写getTreeCellRendererComponent方法,以返回按照您希望的格式格式化的JTextPane组件。您仍然需要做许多DefaultTreeCellRenderer在其实现中所做的事情,但是您不需要操心那些17个字段的维护方面。

edit removed the JTextField editor solution after reading comments

编辑在读取注释后删除了JTextField编辑器解决方案