Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTree Selection Order Icons

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JTree Selection Order Icons

    Hi,

    When a user selects a leaf, I want to display an icon(number picture) displaying the order in which this leaf is selected. i.e, if node has three leaves, if I click on the first, second and third, I want to display 1,2,3 with the node text. It the user deselects second node, I should automatically renumber the other selected nodes 1 and 2.

    I can use the TreeCellRenderer and set the icone for the selected leaf, but, at the same time how do I refresh the icons on the other leafs ?

    public Component getTreeCellRendererComponent(	JTree tr, 
    Object value, 
    boolean sel, 
    boolean expanded, 
    boolean leaf, 
    int row, 
    boolean focus) 
    {
    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, focus); 
    /*if (sel) {
    this.setFont(getFont().deriveFont(Font.BOLD + Font.ITALIC));	
    }
    else { 
    this.setFont(getFont().deriveFont(Font.PLAIN));
    } 
    */
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) 
    { 
    String sSpecial = ((NBTreeNode) value).sSpecial;
    boolean bSelected = ((NBTreeNode) value).bSelected;
    if(sSpecial != null)
    { 
    if (Integer.parseInt(sSpecial) >= 0 && bSelected == true)
    this.setIcon(Icons[Integer.parseInt(sSpecial)]);
    else
    this.setIcon(null);
    } 
     
    }
     
     
     
    return this; 
    }

    Any help is greatly appreciated.

    Thank you


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JTree Selection Order Icons

    I deleted your duplicate post. Please keep questions to one thread at a time.

    When do you want to refresh the icons? Can't you just call repaint() on the whole JTree? Don't do that from the renderer or you'll have an infinite loop on your hands, but can't you do it from whatever event that should cause the refresh?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. JTree not expanding?
    By captain alge in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 03:32 AM
  3. [SOLVED] Jtree help
    By sman36 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 09:39 AM
  4. Setting Icons and checking for matches
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2010, 08:52 PM