如何基于动态更改的Jtree for Java桌面应用程序更改Jtree的颜色 [英] How do i change color of Jtree based on my dynamic changing Jtree for java desktop application

查看:160
本文介绍了如何基于动态更改的Jtree for Java桌面应用程序更改Jtree的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有人,我正在使用JPanelJFrame制作桌面应用程序.这是我的树结构:

Everyone, I'm making desk app with JPanel and JFrame. Here is my tree structure:

默认树

@Root
 |-L1B  (node-1)
 |-L2A (node-2)
 |-L1A (node-3)

此后,我正在读取文件(假设两个值:value1value2)并添加叶子数据.

After this i'm reading a file (let's suppose two values: value1 and value2) and add leaf data.

所以,我想这样改变颜色:

So, I'de like to change the color like this:

@Root
 |**-L1B**  (node-1)(with green color)
    | value1(with green color)
    | value2(with green color)
 |-L2A (node-2)
 |-L1A (node-3)

value1值可能是60s,这意味着在60s内它将变为绿色,然后变为红色.

value1 value might be 60s, meaning that for 60s it will in be green and then become red.

@Root
 |**-L1B**  (node-1)(with green color)
    | value1(with green red)
    | value2(with green color)
 |-L2A (node-2)
 |-L1A (node-3)

在60s之后,value2的值可能比value1大60s,因此在60s内它将变为绿色,然后变为红色.

And after 60s, value2 value might be 60s more than value1, so that for 60s it will be green and then turn red.

@Root
 |**-L1B**  (node-1)(with green color)
    | value1(with green red)
    | value2(with green color)
 |-L2A (node-2)
 |-L1A (node-3)

所以,基本上,我想要运行的进程的层次结构.运行时,颜色应为绿色,然后将更改为另一种颜色.

So, basically I want the hierarchy of running processes. When it is running, the color should be green and then it will change to another color.

推荐答案

您正在寻找的是自定义呈现.

What you are looking for is a custom rendered.

为此,请使用JTree并在渲染器中传递setCellRenderer()方法.

To do that, take your JTree and call the setCellRenderer() method passin your renderer.

基本渲染器是DefaultTreeCellRenderer的继承.返回渲染的方法是getTreeCellRendererComponent().

A basic renderer is an inheritance of DefaultTreeCellRenderer. The method that returns the rendering is getTreeCellRendererComponent().

不幸的是,您的问题非常模糊,因此我无法给出更具体的示例,因此通用示例将是:

Unfortunately, your question is very vague, so I can't give a more specific example, so a generic example would be:

JTree paintedTree = new JTree();
paintedTree.setCellRenderer(new DefaultTreeCellRenderer() {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component renderedItem = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        if (((YourClass)value).getTime() > 60) {
            renderedItem.setBackground(Color.GREEN);
        }

        return renderedItem;
    }
});

请注意,此答案严格来说是从渲染颜色的角度出发. (((YourClass)value).getTime() > 60)简化了用于确定进程是否正在运行的代码,以使答案始终清晰可见.

Note that this answer is strictly from the rendering of colors' point of view. The code for determining if the process is running was simplified by (((YourClass)value).getTime() > 60) to keep the answer in focus.

也请检查页面.这可能对您的追求有所帮助.

Also, check this page. It might help you with your pursuit.

这篇关于如何基于动态更改的Jtree for Java桌面应用程序更改Jtree的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆