Results 1 to 7 of 7
  1. #1
    vishnu_knl is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Smile Dynamically changing icons of some nodes in a JTree

    Hi,

    I am looking to change the icons in a Jtree nodes depending on some condition. For Example I am populating n number of Jobs in the Jtree under a single parent by reading a folder for xml files.
    and some of the jobs might be currently running and i wish to show the icons for these running jobs with a green dot to differentiate them from other jobs. As follows

    Jobs
    #job1
    #job2
    #job3

    [OR]

    Jobs
    #job1
    #job2
    #job3

    I just need to differentiate it from other jobs be it the icon changing or the text color changing.
    Any suggestions?

    Regards
    --vishnu--

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Dynamically changing icons of some nodes in a JTree

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    vishnu_knl is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Dynamically changing icons of some nodes in a JTree

    Thank you Kevin! But i have already been through this link and cant find any help. i tried the following scenario just to change the images and it did not work.
    I tried whit a sleep of 1000 and i can only see the blue icon always. The green doesnt seem to show up atall even after i adjust the sleep timing. If i remove the call for the changeNodeIcons11(), then i can see the green icons.

    private void jButton_changeActionPerformed(java.awt.event.Actio nEvent evt) {
    // TODO add your handling code here:
    changeNodeIcons();

    try {
    Thread.sleep(1000);
    } catch (InterruptedException ex) {
    Logger.getLogger(EasyLoaderPro.class.getName()).lo g(Level.SEVERE, null, ex);
    }

    changeNodeIcons1();

    }


    public void changeNodeIcons() {

    ImageIcon greenIcon = new ImageIcon(".\\src\\treetest\\images\\greendot.gif" );
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) job_tree.getCellRenderer();

    if (greenIcon != null) {
    System.out.println("Green icon Found");
    renderer.setLeafIcon((Icon) greenIcon);
    job_tree.setCellRenderer(renderer);
    job_tree.invalidate();
    job_tree.repaint();

    } else {
    System.err.println("Leaf icon missing; using default.");
    }

    }
    public void changeNodeIcons1() {

    ImageIcon blueIcon = new ImageIcon(".\\src\\treetest\\images\\bluedot.gif") ;
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) job_tree.getCellRenderer();


    if (blueIcon != null) {

    renderer = (DefaultTreeCellRenderer) job_tree.getCellRenderer();
    renderer.setLeafIcon((Icon) blueIcon);
    job_tree.setCellRenderer(renderer);
    job_tree.invalidate();
    job_tree.repaint();
    } else {
    System.err.println("Leaf icon missing; using default.");
    }

    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Dynamically changing icons of some nodes in a JTree

    Don't call Thread.sleep() on the EDT. You won't be able to see anything happening while the EDT is sleeping, since you're blocking the painting thread.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Dynamically changing icons of some nodes in a JTree

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    vishnu_knl is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Dynamically changing icons of some nodes in a JTree

    Kevin,
    I appreciate your help very much.

    Its' just that I did not know that these two forums are the same. I thought they were different. If not i would never have cross posted this. I was just looking for a quick solution and hence thought i might get different people on different forums .

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Dynamically changing icons of some nodes in a JTree

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. help with jtree icons
    By ankit_k in forum AWT / Swing
    Replies: 4
    Last Post: 06-16-2011, 08:18 PM
  2. Adding Icons to JTree
    By Nidi in forum New To Java
    Replies: 3
    Last Post: 02-19-2011, 12:23 PM
  3. Netbeans: Add nodes to JTree?
    By mishathegoat in forum AWT / Swing
    Replies: 8
    Last Post: 11-15-2010, 01:55 AM
  4. How to dynamically add Nodes to JTree?
    By javanewbie in forum AWT / Swing
    Replies: 3
    Last Post: 07-13-2009, 07:20 AM
  5. Nodes displayed in JTree
    By Orange in forum AWT / Swing
    Replies: 6
    Last Post: 08-08-2008, 05:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •