0

I'm being crazy because I don't understand what I'm doing wrong. I need that each node of my Tree have a different icon as if it is correct, incorrect or needs updating. I saw the Oracle Documentation and a lot of webpages (including Stackoverflow) but my Code just show all the nodes with the same icon. I'm sorry if I did something wrong, it's my first POST :>

Display: I can't show because i no have reputation >.<

Imagine a tree and imagine that it show always the leafIcon with the gifNew icon. It seems as if only the state would listen to the last node.

Here's the code (all the vars are correctly created, icons like gifNew too):

ICONS:

static Icon closedIcon = new ImageIcon("folder.png");
    static Icon openIcon = new ImageIcon("folder.png");
    static Icon leafIcon = new ImageIcon("file.png");
    static Icon gifWarn = new ImageIcon("warn.gif");
    static Icon gifOk = new ImageIcon("ok.gif");
    static Icon gifNew = new ImageIcon("plus.gif");
    static Icon gifError = new ImageIcon("error.gif");

Call:

tree.setCellRenderer(new TreeRenderer());

Renderer:

private static class TreeRenderer extends DefaultTreeCellRenderer {


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

            super.getTreeCellRendererComponent(tree, value, sel, exp, leaf, row, hasFocus);
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            String s = node.getUserObject().toString();
            String error;
            if (actualTree.equals("DOORS")){
                error = checkTypeError(s, valuesClass.listOfValuesDOORS);
                //System.out.println("DOORS - S: "+s);
            }else{
                error = checkTypeError(s, valuesClass.listOfValuesTC);
                //System.out.println("TC - S: "+s);
            }

            switch (error) {
                case "CORRECT":
                    setOpenIcon(openIcon);
                    setClosedIcon(closedIcon);
                    setLeafIcon(leafIcon);
                    break;
                case "CREATE":
                    setOpenIcon(gifNew);
                    setClosedIcon(gifNew);
                    setLeafIcon(gifNew);
                    break;
                case "DELETE":
                    setOpenIcon(gifError);
                    setClosedIcon(gifError);
                    setLeafIcon(gifError);
                    break;
                case "UPDATE":
                    setOpenIcon(gifWarn);
                    setClosedIcon(gifWarn);
                    setLeafIcon(gifWarn);
                    break;
                default:
                    setOpenIcon(openIcon);
                    setClosedIcon(closedIcon);
                    setLeafIcon(leafIcon);
                    //System.err.println("ERROR IN RENDERER. VALUE: "+error);
                    break;
            }

            return this;
        }

        /*****************************************
        * Function that return which error have the actual node to push the icon when the tree is created.
        *****************************************/
        protected static String checkTypeError(String txt, List<valuesClass> list){

            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).text.equals(txt))
                    if (list.get(i).create == true){
                        return "CREATE";
                    }else if (list.get(i).delete == true){
                        return "DELETE";
                    }else if (list.get(i).update == true){
                        return "UPDATE";
                    }else{

                        return "CORRECT";
                    }
            }
            return "DEFAULT";
        }
    }
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
chiri4
  • 43
  • 8

2 Answers2

1

The behaviour you are describing is caused by the fact that you are not setting correctly the node icon in the switch statement. The setOpenIcon(), setClosedIcon() etc set the icons that the renderer will use for the whole tree. So the action taken in the last node is going to decide what icons will be rendered at the end.

The DefaultTreeCellRenderer extends a JLabel. This component will be used for each node to render its contents. It is this components' icon that needs to be set i.e. your code should look like this:

switch (error) {
  case "CORRECT":
       setIcon(leafIcon); // sets the icon of the renderer which is a JLabel
       break;
  case "CREATE":
       setIcon(gifNew);
       break;
  case "DELETE":
       setIcon(gifError);
       break;
  ...
}

Be sure to set the icon in all cases though and make sure that you understand how the renderer is used to render tree nodes (the same instance is reused for all nodes)

c.s.
  • 4,786
  • 18
  • 32
  • O.O! AWESOME! IT Works! Now my tree have the differents icons for each case! Now i can continue with the project! :> I can't give you reputation for my low reputation level, but u are a Master :P – chiri4 Jul 15 '13 at 14:30
  • Perhaps you could accept the answer then since it has worked for you :) – c.s. Jul 15 '13 at 14:40
0

Strings are allowed in switch statements in Java 7, and while I haven't used them yet, your syntax looks correct. I suspect that the values of error String are not what you expect.

Since the case values are all upper case, change the line

switch (error) {

to

switch (error.toUpperCase()) {

and see if that solves it. If it doesn't, then print out the value of error, or use the debugger to check it's value.

EDIT: following our discussion below, I suspect that your tree nodes are DefaultMutableTreeNode objects (is that right?) and that you are storing data in the user object.

In that case, cast the value to DefaultMutableTreeNode, and extract your data:

DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
MyData myData = (MyData) node.getUserObject();

You can then test myData to work out what icon to set.

andy256
  • 2,821
  • 2
  • 13
  • 19
  • I'm doing with the libraries of 1.7. Switch works correctly with Strings. – chiri4 Jul 15 '13 at 10:05
  • **Probably my error is in:** String s = node.getUserObject().toString(); I try to catch the text of the node and this function isn't correct. Anyone can tell me how can I extract the text of the actual node? – chiri4 Jul 15 '13 at 12:06
  • Yes, you need to get the contents of your node, from the value parameter. Cast it to the type of node your store in the tree, and then get it's value. – andy256 Jul 15 '13 at 12:27
  • I'm dazed and I can't think clearly. Can you help me with the code? I don't find the way to extract the value. >. – chiri4 Jul 15 '13 at 12:40
  • What data do you store in the tree, ie how do you create the nodes? – andy256 Jul 15 '13 at 13:12
  • I just realized that you're probably using DefaultTreeNode and setting userdata, if so you would have to cast value to DefaultTreeNode to get your userdata. I'll update my answer ... – andy256 Jul 15 '13 at 13:36