• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree Cell Renderer CPU usage

 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a Swing application where I display information using JTree. I am using TreeCellRenderer as I have icons etc. on the JTree.

I noticed that when I launch my JTree, the CPU cycles goes to 25-26 and stays there. Even though I (user) do not perform any activity on the UI, the CPU cycles still remains the same till I close the application.

I added few println statement and noticed that TreeCellRenderer's getTreeCellRenderer() method is called infinitely till the Jtree is displayed. Probably it could be the reason for high CPU cycle.

Do you any suggestions, how can I resolve the issue. How can I use TreeCellRenderer and have control the CPU cycles ?

Thanks a million.

Regards.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by looking at the code in your TreeCellRenderer. Remember that its method... the one with the long name and parameter list which I don't have in my memory right now... will be called frequently. So if you're doing anything non-trivial in that method, that could be a problem.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate on Paul's response -- in the getTreeCellRendererComponent method:
  • Don't perform disk I/O such as reading images for icons
  • Don't modify the state of the JTree or any GUI component apart from the renderer component itself
  • Avoid creating new objects


  • To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20659
    64
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A Thakur wrote:I noticed that when I launch my JTree, the CPU cycles goes to 25-26 and stays there. Even though I (user) do not perform any activity on the UI, the CPU cycles still remains the same till I close the application.

    You have a 4-core CPU, and the JVM is using one of these cores completely. On a single core CPU you would have seen a CPU usage of 99% and you would have had trouble closing the application.

    Something in your renderer causes an infinite loop. Not in the traditional for/while loop sense, but it has some code that causes the tree to be repainted which causes the renderer to be called again, which causes the tree to be repainted, etc.
     
    A Thakur
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi all,

    I found the problem. I had following code in getTreeCellRendererComponent() method

    if(tree!=null){
    tree.revalidate();
    tree.repaint();
    }


    After commenting the above block, getTreeCellRendererComponent() is not called in a infinite loop. As a results CPU cycle always comes to 0 when no activity is performed.

    Thanks again for all the help.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic