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 6822343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:39:08+00:00 2026-05-26T21:39:08+00:00

I have a tree with DefaultMutableTreeNodes only. I want to write a TreeCellRenderer that

  • 0

I have a tree with DefaultMutableTreeNodes only. I want to write a TreeCellRenderer that depends on the type of the contained user object. I tried to write a simple example (which doesn’t work. That’s why I ended up here). On the first level beyond the root node, the types of the user objects are either AANodeUserObjector LocalAANodeUserObject. Both should be rendered with directory icons even though there might be no child. The other feature of this renderer is, that it should render nodes with user object type AANodeUserObject in red color and bold when they are not “current” (in my example, this is always true). Here is my code:

    public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus)
    {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        // Precondition für Default TreeNode
        Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
        if (userObject instanceof TestTree.AANodeUserObject || userObject instanceof TestTree.LocalAANodeUserObject)
        {
            if (expanded)
            {
                setIcon(openIcon);
            }
            else
            {
                setIcon(closedIcon);
            }
        }

        if (userObject instanceof TestTree.AANodeUserObject)
        {
            TestTree.AANodeUserObject aAnode = (TestTree.AANodeUserObject) userObject;
            if (!mVersionInfo.get(aAnode))
            {
                renderOutdatedAaNode();
            } else {
                renderDefault();
            }
        }  else {
            renderDefault();
        }
        return this;
    }

    private void renderDefault()
    {
        setTextNonSelectionColor(cColorBlack);
        setTextSelectionColor(cColorBlack);
        setFont(getFont().deriveFont(Font.PLAIN));
    }

    private void renderOutdatedAaNode()
    {
        setTextNonSelectionColor(cColorRed);
        setTextSelectionColor(cColorRed);
        setFont(getFont().deriveFont(Font.BOLD));
    }

You can see the result on the attached pictureThe resulting tree.

What irritates me is that the directory-icon-thing works fine, the text style doesn’t: only the second node (Hashmap value is false) AANodeUserObject should be red and nothing else.
What am I doing wrong? Can somebody help?
Thanks!
Mathias

  • 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-26T21:39:08+00:00Added an answer on May 26, 2026 at 9:39 pm

    I solved the problems by calling super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus) twice:

    • The first time as first line of getTreeCellRendererComponent(). Without the call, getFont(setFont()) to set the font style to bold will return a NullPointerException.

    • The second time at the last line in the method to render the nodes properly.

    There might be another solution but after many different tries, this has worked for me.

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

Sidebar

Related Questions

I have a tree that consists of several objects, where each object has a
I want write custom TreeCellRenderer to have Root, nodes and leafs in different color.
I have tree control object created using CTreeCtrl MFC class. The tree control needs
I have a tree structure in memory that I would like to render in
I have a tree data structure that is L levels deep each node has
I have an own Tree object implemented in PHP. Say we have a tree
I have a simple TreeCellRenderer which extracts a JButton from the node, and outputs
I have a tree, which childs dispaching event, if i run the object outside
I have a tree using an ArrayCollection/Object data provider. The following code worked with
I want to have a JTree in my swing application that does not have

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.