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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:15:50+00:00 2026-05-29T18:15:50+00:00

I have a simple TreeCellRenderer which extracts a JButton from the node, and outputs

  • 0

I have a simple TreeCellRenderer which extracts a JButton from the node, and outputs it to the JTree.

public class ButtonCellRenderer extends JButton implements TreeCellRenderer {

  @Override
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean     selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    //setBackgroundNonSelectionColor(tree.getBackground());
    ProgressButton b = null;
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
      if (userObject instanceof ProgressButton) {
        b = (ProgressButton) userObject;
        return b;
      }
    }
    if (b == null) {
      System.out.println("Null!");
      DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
      return defaultRenderer.getTreeCellRendererComponent(tree,
              value, selected, expanded, leaf, row, hasFocus);
    } else {
      System.out.println("Returning label");
      return new JLabel(b.getToolTipText(), b.getIcon(), SwingConstants.CENTER);
    }
  }
}

I’m not sure why, but it seems to be stuck looping, and eats about 30~50% CPU. Is there any way to eliminate this? Or not have to use a renderer to output a JButton into a tree?

I’ve added the revised code above, but now I’m getting a problem where the buttons don’t display properly (they all come out small).

  • 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-29T18:15:52+00:00Added an answer on May 29, 2026 at 6:15 pm

    You are calling super.getTreeCellRendererComponent without storing the value. If later on no renderer is created you call this method again (if c==null...). This is expensive for itself.

    More important:

    A renderer should be a component that is reused and only configured for the specific value to be shown. This is why the default implementation reuses a label and just sets the text, colors, border… That is why renderer calls are quite cheap.

    You create a new JLabel every time you show a value: returnLab = new JLabel(text);. This is expensive. This method will be called for each and every cell shown on each repaint event. This is much more expensive than building a grid of components and defies the whole purpose of the renderer concept.

    Please have a look into the default renderer implementation to see how to do it correctly. And have a look into the tutorial.

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

Sidebar

Related Questions

I have simple SSIS package which reads data from flat file and insert into
I have simple problem: class Weapon{ public: int dmg; float speed; int rate; };
I have simple SSIS package in which On Error event handler I have created
I have simple SSIS package where I import data from flat file into SQL
I have simple Perl/CGI scripts based web server which is mainly used to display
I have simple map-reduce type algorithm, which I want to implement in python and
I have simple scenario in which i hide and show a signle MenuItem in
I have simple HTML code: <input type='hidden' name='cat_id' value='123'/> <a href='#' class='edit'>Edit</a> <a href='#'
I have simple user registration form. In which i am puting city as a
I have simple front controller plugin which contains this code: http://pastebin.com/m155c59b0 When session expire

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.