File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTree Cell Renderer CPU usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree Cell Renderer CPU usage" Watch "JTree Cell Renderer CPU usage" New topic
Author

JTree Cell Renderer CPU usage

A Thakur
Greenhorn

Joined: Apr 06, 2010
Posts: 20
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.


SCJP 5.0
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

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

Joined: May 03, 2008
Posts: 4664
    
    5

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.

    luck, db
    There are no new questions, but there may be new answers.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19792
        
      20

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    A Thakur
    Greenhorn

    Joined: Apr 06, 2010
    Posts: 20
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JTree Cell Renderer CPU usage