开发者

JTree Multiple Node Renderer problem

In the following JTreeBasics.java file, is it possible to have different images for BlackBox and WhiteBox nodes?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.lang.reflect.*;

public class JTreeBasics{
    JFrame f;
    DefaultMutableTreeNode root,n1,n2,ln1,ln2,ln3,ln4,ln3_1,ln3_2,ln3_1_1,ln3_1_2,ln3_1_2_1;
    DefaultTreeModel dtm;
    JTree tree;

    public JTreeBasics() {
        f=new JFrame("JTree Demo");
        f.setSize(400, 500) ;
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        root=new DefaultMutableTreeNode("BlackBox");

            n1=new DefaultMutableTreeNode("WhiteBox");
            root.add(n1);
                ln1=new DefaultMutableTreeNode("Item");
                n1.add(ln1);

            n2=new DefaultMutableTreeNode("BlackBox");
            root.add(n2);
                ln2=new DefaultMutableTreeNode("Item");
                n2.add(ln2);
                ln3=new DefaultMutableTreeNode("WhiteBox");
                n2.add(ln3);
                    ln3_1=new DefaultMutableTreeNode("WhiteBox");

                        ln3_1_1=new DefaultMutableTreeNode("Item");
                        ln3_1.add(ln3_1_1);
                        ln3_1_2=new DefaultMutableTreeNode("BlackBox");

                            ln3_1_2_1=new DefaultMutableTreeNode("Item");
                            ln3_1_2.add(ln3_1_2_1);

                        ln3_1.add(ln3_1_2);

                    ln3.add(ln3_1);
                    ln3_2=new DefaultMutableTreeNode("Item");
                    ln3.add(ln3_2);
                ln4=new DefaultMutableTreeNode("Item");
                n2.add(ln4);

        dtm=new DefaultTreeModel(root);
        tree=new JTree(dtm);

        //most imp. line of this code
        installingCustomRenderer() ;

        f.add(tree);
        f.setVisible(true);
    }

    private void installingCustomRenderer(){
        CustomTreeCellRenderer renderer = new CustomTreeCellRenderer();


        tree.setCellRenderer(renderer);
    }

    public static void main(String[] args){
        new JTreeBasics();
    }
}

class CustomTreeCellRenderer extends DefaultTreeCellRenderer{

    private Font 开发者_JS百科plainFont = null;
    private Font italicFont = null;

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

        super.getTreeCellRendererComponent(tree, value,
        selected, expanded, leaf, row, hasFocus);

        plainFont = getFont();

        italicFont = plainFont.deriveFont(Font.ITALIC) ;

        setFont(italicFont);

        return this;
    }
}


Yes, in getTreeCellRendererComponent you need to consult value and call setIcon() with the appropriate image.

Note: the default implementation looks at the values of expanded and leaf to select between an open, closed, or leaf icon.


In the renderer code cast the return value from the super call to a JLabel (as DefaultTreeCellRenderer is a JLabel) and then you can setIcon() on it according to the the 'value'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜