• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTree Cell Renderer CPU usage

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Sheriff
    Posts: 22784
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic