Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

JTree - custom icons

Is it possible to use custom icons for those small icons next to each node within a JTree?

I don't mean, a custom open and close folder image... I mean, each node within the entire JTree could potentially be different...

Here's my tree cell renderer (thanks CEHJ):

    private class NameOnlyRenderer extends DefaultTreeCellRenderer
    {
        public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected,
                                                                         boolean expanded, boolean leaf, int row, boolean hasFocus )
        {
            File f = (File ) value ;
            String text = f.getName() ;
            return super.getTreeCellRendererComponent( tree, text, selected, expanded, leaf, row, hasFocus ) ;
        }
    }


And I have a method which will return an Icon based on the file type:

    public Icon getIcon( File f )
    {
        return (new JFileChooser()).getIcon( f ) ;
    }

......

Can this be achieved?

Thanks loads
0
InteractiveMind
Asked:
InteractiveMind
4 Solutions
 
borislavmarkovCommented:
The default renderer is in fact a label, so you can use it's icon like this:
 
private class NameOnlyRenderer extends DefaultTreeCellRenderer
    {
        public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected,
                                                                        boolean expanded, boolean leaf, int row, boolean hasFocus )
        {
            File f = (File ) value ;
            String text = f.getName() ;
            JLabel lbl = (JLabel)super.getTreeCellRendererComponent( tree, text, selected, expanded, leaf, row, hasFocus ) ;
            //****************
            lbl.setIcon(getIcon(f));
            //***************
            return lbl;
        }
    }
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can try:

ImageIcon leafIcon = new ImageIcon ( getClass ().getResource ( "/image1.gif" ) ) ; // assuming that is the icon
ImageIcon closedIcon = new ImageIcon ( getClass ().getResource ( "/image2.gif" ) ) ;
ImageIcon openIcon = new ImageIcon ( getClass ().getResource ( "/image3.gif" ) ) ;
UIManager.put ( "Tree.leafIcon", leafIcon ) ;
UIManager.put ( "Tree.openIcon", openIcon ) ;
UIManager.put ( "Tree.closedIcon", closedIcon ) ;


>> http://www.objects.com.au/java/examples/swing/IconTreeCell.do

Says bad gateway?
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now