使用多种不同字体渲染 JTree 中的节点

发布于 2024-10-16 17:27:54 字数 100 浏览 3 评论 0原文

想象一下,我有一个显示多个字符串的 JTree - 例如颜色列表。如何用不同的颜色/字体组合渲染这样一棵树?如何正确实现TreeCellRenderer?

谢谢你的帮助。

Imagine I have a JTree that is showing multiple strings - for example a list of colors. How it would be possible to render such a tree with different color/font combination? How to implement TreeCellRenderer correctly?

Thank you for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2024-10-23 17:27:54

您可以扩展 DefaultTreeCellRenderer。在 getTreeCellRendererComponent 方法中,您调用 super() 并检查条件,例如通过分析值。之后相应地调用 setFont()、setBackground() 和 setForeground() 方法。

You can extend DefaultTreeCellRenderer. In the getTreeCellRendererComponent method you call super() and check you conditions e.g. by analysing value. After that call setFont(), setBackground() and setForeground() methods accordingly.

挖个坑埋了你 2024-10-23 17:27:54

另一种可能的方法是创建一个实现 TreeCellRenderer 的内部类。然后您所要做的就是按照您想要的方式自定义 JLabel。

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

    Component returnValue = null;
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
      if (userObject instanceof Employee) {
        Employee e = (Employee) userObject;
        firstNameLabel.setText(e.firstName);
        lastNameLabel.setText(e.lastName);
        salaryLabel.setText("" + e.salary);
        if (selected) {
          renderer.setBackground(backgroundSelectionColor);
        } else {
          renderer.setBackground(backgroundNonSelectionColor);
        }
        renderer.setEnabled(tree.isEnabled());
        returnValue = renderer;
      }
    }
    if (returnValue == null) {
      returnValue = defaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded,
          leaf, row, hasFocus);
    }
    return returnValue;
  }

取自此网站

Another possible way is to make an inner class that implements TreeCellRenderer. All you have to do then is customize the JLabel the way you want.

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

    Component returnValue = null;
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
      if (userObject instanceof Employee) {
        Employee e = (Employee) userObject;
        firstNameLabel.setText(e.firstName);
        lastNameLabel.setText(e.lastName);
        salaryLabel.setText("" + e.salary);
        if (selected) {
          renderer.setBackground(backgroundSelectionColor);
        } else {
          renderer.setBackground(backgroundNonSelectionColor);
        }
        renderer.setEnabled(tree.isEnabled());
        returnValue = renderer;
      }
    }
    if (returnValue == null) {
      returnValue = defaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded,
          leaf, row, hasFocus);
    }
    return returnValue;
  }

taken from this site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文