0

I need to be able to set icons for the JTree individual nodes. For example, I have a JTree and I need the nodes to have custom icons that help represent what they are.

  • (wrench icon) Settings
  • (bug icon) Debug
  • (smiley face icon) Fun Stuff

...

And so on. I have tried several sources and got one somewhat working, but it messed up the tree events so, no cigar. Thanks in advance.

As someone requested:

class Country {
    private String name;
    private String flagIcon;

    Country(String name, String flagIcon) {
        this.name = name;
        this.flagIcon = flagIcon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFlagIcon() {
        return flagIcon;
    }

    public void setFlagIcon(String flagIcon) {
        this.flagIcon = flagIcon;
    }
}

class CountryTreeCellRenderer implements TreeCellRenderer {
    private JLabel label;

    CountryTreeCellRenderer() {
        label = new JLabel();
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Object o = ((DefaultMutableTreeNode) value).getUserObject();
        if (o instanceof Country) {
            Country country = (Country) o;
            label.setIcon(new ImageIcon(country.getFlagIcon()));
            label.setText(country.getName());
        } else {
            label.setIcon(null);
            label.setText("" + value);
        }
        return label;
    }
}

Then where it's initialized:

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Countries");
    DefaultMutableTreeNode asia = new DefaultMutableTreeNode("General");
    Country[] countries = new Country[]{
            new Country("Properties", "src/biz/jabaar/lotus/sf/icons/page_white_edit.png"),
            new Country("Network", "src/biz/jabaar/lotus/sf/icons/drive_network.png"),
    };

    for (Country country : countries) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(country);
        asia.add(node);
    }

And this works, it's just I don't want the sub-roots to show, just the nodes. Also, this code makes it so that the item won't highlight when you click it.

Michael Evans
  • 11
  • 1
  • 2
  • _and got one somewhat working_...great, please post and describe the issue you are having – Reimeus Mar 12 '13 at 23:38
  • Not really necessary as it's not what I need anyways but it's posted. – Michael Evans Mar 12 '13 at 23:48
  • What do you mean by sub-roots? – flup Mar 13 '13 at 00:04
  • Uhm, I don't know the proper name for them, they are the things that open and close so you can see the nodes in them. Sorry. – Michael Evans Mar 13 '13 at 00:34
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/) (of your closest attempt). – Andrew Thompson Mar 13 '13 at 00:45
  • I did, it's up there in the OP. I need some help, not random comments. Thanks anyways. – Michael Evans Mar 13 '13 at 00:50
  • 1
    And we could do with more effort, try look at [this](http://stackoverflow.com/questions/14096725/jtree-set-custom-open-closed-icons-for-individual-groups/14098574#14098574) and [this](http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#display) – MadProgrammer Mar 13 '13 at 01:26
  • -1 for insisting to not provide the necessary information to make reasonable answers possible – kleopatra Mar 13 '13 at 09:42
  • What? Did you even see the OP? What else do I need to provide? I identified the issue, posted code, explained what I need. How in the world is that not sufficient? And if you needed more just say so, because from what everyone has said, that is enough. – Michael Evans Mar 13 '13 at 22:39

1 Answers1

1

I don't want the sub-roots to show, just the nodes.

Your implementation of getTreeCellRendererComponent() should see a properly conditioned boolean leaf parameter that you can use as shown here.

if (o instanceof Country && leaf) { ... }
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045