Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:59:49+00:00 2026-05-13T11:59:49+00:00

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

  • 0

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 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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:59:49+00:00Added an answer on May 13, 2026 at 11:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following is a sample script that I have written line=/path/IntegrationFilter.java:150: * <td>http://abcd.com/index.do</td> echo $line
Following is the Play 2 app, https://github.com/playframework/Play20/tree/master/samples/java/forms Which portrays Dynamic form binding where form
Following on from another question I have asked I have created a new class
Following this python example , I encode a string as Base64 with: >>> import
following problem: I have a simple paragraph with a lot of text where I
Following this JavaFX 2.0 tutorial I created a css file in eclipse that includes
Following the instructions here: http://www.padrinorb.com/guides/padrino-mailer I have the delivery method added on the app.rb
Following code is used to get images from particular folder but how to get
Following is a static struct in C++. How can this be represented in java.
Following suggestions on this site, I have adopted SimpleXML from org.simpleframework.xml. I use this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.