Icon CHANGE in Node Renaming  for JTree

HI all ,
In JTree, V can rename a DefaultMutableTreeNode , by pressing F2 or triple click .
By using the DefaultTreeCellRenderer ,
I applied icons also by -- tree.setCellRenderer(myCellRenderer);
But at the time of renaming , or when v press F2 ,
the icon is changed to default settings ( leafIcon / openIcon / closedIcon ).
How can I apply other Icons to nodes at the time of node renaming ?
Please help ..
Thanks

MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
DefaultTreeCellEditor treeCellEditor = new MyTreeCellEditor(tree, treeCellRenderer);;
MyTreeCellEditorListener cellEditorListener = new MyTreeCellEditorListener();
treeModel = new DefaultTreeModel (rootNode);
tree = new JTree(treeModel);
treeCellEditor.addCellEditorListener(cellEditorListener);
tree.setCellEditor(treeCellEditor);
tree.setCellRenderer(treeCellRenderer);
I am renaming the leaf nodes and MyTreeCellRenderer class override the method
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
and set a different icon to the leaf nodes.
And when i am renaming a leaf node , the default ( leafIcon ) icon will come only to the renaming node at the time of renaming . When i hit the enter key . the default leafIcon will disappear and my customised icon will come .........
My question is that at the time of renaming ........ How v can getback the customised icon ?
thanks

Similar Messages

  • How to set icon for JTree

    hi,
    I have a JTree. For this JTree i have a DefaultJTreeCellRenderer.
    i have done like this
    JTree tree = new JTree();
    DefaultJTreeCellRenderer renderer = new DefaultJTreeCellRenderer();
    renderer.setOpenIcon(....);
    renderer.serCloseIcon(...);
    tree.setCellRenderer(renderer);but this is not working.
    how to change the icons of the node when i click
    thank you

    public class Test extends JFrame implements ActionListener {
         private static final long serialVersionUID = 1L;
         private JButton buttonCreate;
         private JTree tree;
         private DefaultMutableTreeNode defaultMutableTreeNode;
         private DefaultTreeModel defaultTreeModel;
         private int i;
         public Test() {
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              layoutComponents();
              setSize(400, 400);
              setLocationRelativeTo(null);
              setVisible(true);
         private void layoutComponents() {
              defaultMutableTreeNode = new DefaultMutableTreeNode("Root");
              defaultTreeModel = new DefaultTreeModel(defaultMutableTreeNode);
              tree = new JTree(defaultTreeModel);
              tree.setRootVisible(false);
              buttonCreate = new JButton("Create");
              getContentPane().setLayout(new BorderLayout());
              getContentPane().add(tree, BorderLayout.CENTER);
              getContentPane().add(buttonCreate, BorderLayout.SOUTH);
              DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
              ImageIcon icon_close = new ImageIcon("./images/project_closed.gif");
              ImageIcon icon_open = new ImageIcon("./images/project_opened.gif");
              renderer.setOpenIcon(icon_open);
              renderer.setClosedIcon(icon_close);
              tree.setCellRenderer(renderer);
              buttonCreate.addActionListener(this);
         public static void main(String[] args) {
              new Test();
         public void actionPerformed(ActionEvent e) {
              addElementToParent("Child" + i++);
         private DefaultMutableTreeNode addElementToParent(Object theChild) {
              return addElementToParent(defaultMutableTreeNode, theChild, true);
         private DefaultMutableTreeNode addElementToParent(DefaultMutableTreeNode theParent, Object theChild, boolean shouldBeVisible) {
              DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(theChild);
              if (theParent == null) {
                   theParent = defaultMutableTreeNode;
              defaultTreeModel.insertNodeInto(childNode, theParent, theParent.getChildCount());
              TreePath childTreePath = new TreePath(childNode.getPath());
              //          Make sure the user can see the lovely new node.
              if (shouldBeVisible) {
                   tree.scrollPathToVisible(childTreePath);
              tree.requestFocusInWindow();
              return childNode;
    }this is the code which i was trying to execute.
    here i want to explain you is
    when i click the node the icon should change to open_icon
    when i click the other node the previous node icon should change to close_icon
    how is this possible
    null

  • Changing Certain Node Icons

    I can't seem to figure out how to change certain node icons in a JTree due to a 3 letter string. For example 2 nodes one saying "Intro" the other say "Edit - SEC" - the one with SEC has a different icon to "Intro"
    MyRenderer class, here the code which is used to check the string of the node......
    if(leaf && isSecuredFile(value)) {
    setLeafIcon(leafSecuredIcon);
    setToolTipText("This file is Secured Access Only");
    else {
    setToolTipText(null);
    return this;
    protected boolean isSecuredFile(Object value) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
    NodeRecord nodeInfo = (NodeRecord)(node.getUserObject());
    String nodeTitle = nodeInfo.NodeRecord;
    if(nodeTitle.indexOf("SEC") >= 0) {
    return true;
    return false;
    }// MyRenderer class
    I tried the above piece of code, and it compiles with no errors but when I try to run the applet it comes up not initialised with the following....
    java.lang.NoClassDefFoundError: MyRenderer
    at firstPanel.<init>(firstPanel.java:99)
    at JNavigator.jInit(JNavigator.java:55)
    at JNavigator.init(JNavigator.java:40)
    at sun.applet.AppletPanel.run(AppletPanel.java:344)
    at java.lang.Thread.run(Thread.java:484)
    However if I take out this code it works fine but I need to be able to assign different icons depending on the node title string. Therefore I need help.

    here's the whole class, if thats any help...
    public class MyRenderer extends DefaultTreeCellRenderer {
    ImageIcon leafIcon;
    ImageIcon leafSecuredIcon;
    public MyRenderer() {
    leafIcon = new ImageIcon("Leaf.gif");
    leafSecuredIcon = new ImageIcon("LeafSecured.gif");
    public Component getTreeCellRendererComponent(JTree tree, Object val, boolean sel, boolean expand, boolean leaf, int row, boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, val, sel, expand, leaf, row, hasFocus);
    // sets Icons
    setLeafIcon(leafIcon);
    setClosedIcon(new ImageIcon("Folder95C.gif"));
    setOpenIcon(new ImageIcon("Folder95O.gif"));
    // set Expansion icons to + -
    ComponentUI treeUI = tree.getUI();
    if(treeUI instanceof BasicTreeUI) {
    ((BasicTreeUI)treeUI).setExpandedIcon(new ImageIcon("minus.gif"));
    ((BasicTreeUI)treeUI).setCollapsedIcon(new ImageIcon("plus.gif"));
    // decides who icon to use
    if(leaf && isSecuredFile(value)) {
    setLeafIcon(leafSecuredIcon);
    setToolTipText("This file is Secured Access Only");
    else {
    setToolTipText(null);
    return this;
    protected boolean isSecuredFile(Object value) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
    NodeRecord nodeInfo = (NodeRecord)(node.getUserObject());
    String nodeTitle = nodeInfo.NodeRecord;
    if(nodeTitle.indexOf("DOM") >= 0) {
    return true;
    return false;
    }

  • How to Use different icons for JTree in the same level

    Hi guys, i come cross a problem with JTree. The module need a JTree which need to represent items in the same level using different icons. For example:
    [Icon for Root]Root
    |
    |_____________[Icon for Table] TABLES
    |
    |_____________[Icon for Index] INDEXS
    |
    |_____________[Icon for Views] VIEWS
    As i know, the default behavior for JTree is using the same icon for all leaves and
    the same icon for all node that has children. How can i achieve that?
    Thansk a lot!

    subclass DefaultTreeCellRenderer. if you overwrite the method below, then you can set the icon to whatever you want....!
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                                    boolean sel,
                                    boolean expanded,
                                    boolean leaf, int row,
                                    boolean hasFocus) {
         String         stringValue = tree.convertValueToText(value, sel,
                               expanded, leaf, row, hasFocus);
            this.tree = tree;
         this.hasFocus = hasFocus;
         setText(stringValue);
         if(sel)
             setForeground(getTextSelectionColor());
         else
             setForeground(getTextNonSelectionColor());
         // There needs to be a way to specify disabled icons.
         if (!tree.isEnabled()) {
             setEnabled(false);
             if (leaf) {
              setDisabledIcon(getLeafIcon());
             } else if (expanded) {
              setDisabledIcon(getOpenIcon());
             } else {
              setDisabledIcon(getClosedIcon());
         else {
             setEnabled(true);
             if (leaf) {
              setIcon(getLeafIcon());
             } else if (expanded) {
              setIcon(getOpenIcon());
             } else {
              setIcon(getClosedIcon());
            setComponentOrientation(tree.getComponentOrientation());
         selected = sel;
         return this;
        }

  • Icon of a node changes when I edit its content

    I have a JTree that represent an xml document.
    and I have a TreeCellRenderer that sets the Icon of the node according to its type:
    public class XmlTreeCellRenderer extends DefaultTreeCellRenderer {
          * Overrides this method to display the apropriate icon according
          * to the type of the <code>Node</code>.
         @Override
         @SuppressWarnings("hiding")
         public Component getTreeCellRendererComponent(
                   JTree tree, Object value, boolean sel,
                   boolean expanded, boolean leaf, int row, boolean hasFocus) {
              super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
                        row, hasFocus);
              if (!(value instanceof XmlTreeNode)) {
                   return this;
              // retrieves the type of the node
              XmlTreeNode treeNode = (XmlTreeNode) value;
              short nodeType = treeNode.getNodeType();
              // sets the icon of the node and the foreground color
              // according to the type
              switch (nodeType) {
              case Node.ELEMENT_NODE:
                   setForeground(Color.BLACK);
                   setIcon(ELEMENT_ICON);
                   break;
              case Node.TEXT_NODE:
                   setForeground(Color.BLUE);
                   setIcon(TEXT_ICON);               
                   break;
              case Node.COMMENT_NODE:
                   setForeground(Color.GREEN);
                   setIcon(COMMENT_ICON);               
                   break;
              default:
                   setToolTipText("This is an unknown node");
                   break;
              return this;
         // the image resources
         private static final ImageIcon TEXT_ICON =
              new ImageIcon("resource/images/TextIcon.jpg");
         private static final ImageIcon COMMENT_ICON =
              new ImageIcon("resource/images/CommentIcon.jpg");
         private static final ImageIcon ELEMENT_ICON =
              new ImageIcon("resource/images/ElementIcon.jpg");
    }Now the problem is when I edit a node's content the icon changes back
    to the default icon, like this.
    This is before I edit the node's content:
    http://img140.imageshack.us/my.php?image=before6zj.jpg
    This is after:
    http://img89.imageshack.us/my.php?image=after2xd.jpg
    Notice how the "E" changed to the folder icon.
    How do I disable it?
    I want it to stay with the "E" icon even when I edit the node's content.
    I tried in the XmlTreeCellRenderer to use setClosedIcon and setOpenIcon
    but it didn't work.

    I have a JTree that represent an xml document.
    and I have a TreeCellRenderer that sets the Icon of
    the node according to its type:<snip>
    Now the problem is when I edit a node's content the
    icon changes backIt's a bug. See:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4663832

  • How to change the node's icon in a tree when the node collapse or expand?

    how to change the node's icon in a tree when the node collapse or expand?

    Hi,
    You may need to use custom skin for that.
    -Arun

  • Unable to display blinking icon for JTree

    I am unable to see blinking icon for JTree node. If I use any other image which is not blinking, then I am able to see icon.
    If blinking image is used, it shows blank image.
    Please refer to following code.
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.net.URL;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    public class TreeApplet extends JApplet {
    JTree tree;
    DefaultMutableTreeNode root;
    DefaultMutableTreeNode node1;
    DefaultMutableTreeNode node2;
    DefaultMutableTreeNode node3;
    DefaultMutableTreeNode node4;
    private ImageIcon errorIcon = null;
    private URL resource = null;
    private static final long serialVersionUID = 1L;
    public void init() {
         errorIcon = loadIcon("images/errorBlink.gif");
         root = new DefaultMutableTreeNode(new ProcessInfo("root", "error"));
         node1 = new DefaultMutableTreeNode(new ProcessInfo("Node1", "info"));
         node2 = new DefaultMutableTreeNode(new ProcessInfo("Node2", "warn"));
         node3 = new DefaultMutableTreeNode(new ProcessInfo("Node3", "debug"));
         node4 = new DefaultMutableTreeNode(new ProcessInfo("Node4", "error"));
         node1.add(node2);
         node3.add(node4);
         root.add(node1);
         root.add(node3);
         setLayout(new BorderLayout());
         tree = new JTree(root);
         tree.setCellRenderer(new TreeRenderer());
         add(new JScrollPane((JTree) tree), "Center");
    private class TreeRenderer extends DefaultTreeCellRenderer {
         private static final long serialVersionUID = 1L;
         public TreeRenderer() {
              this.setBackgroundSelectionColor(Color.lightGray);
              this.setBorderSelectionColor(Color.BLACK);
         public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf,
              int row, boolean hasFocus) {
              super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
              setIcon(getStatus(value));
              return this;
         private ImageIcon getStatus(Object value) {
              DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
              ProcessInfo nodeInfo = (ProcessInfo) node.getUserObject();
              String status = nodeInfo.getStatus();
              if ( status != null ) {
              if ( status.equalsIgnoreCase("error") ) {
                   return errorIcon;
              return null;
    private ImageIcon loadIcon(String name) {
         ImageIcon icon = null;
         resource = this.getClass().getResource(name);
         if ( resource != null ) {
              icon = new ImageIcon(resource);
         return icon;
    }

    1. Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
    2. A renderer is just a "rubber stamp" used to paint on the table/tree/list/whatever. You can't display animation in an ImageIcon by setting it to a renderer. Since the JLabel subclass used as the renderer isn't a part of any visible component hierarchy, the icon won't be refreshed with new frames.
    db

  • Dynamically changing the color of nodes in a JTree

    I have created a JTree and want to dynamically change the color of some of the TreeNodes as my program runs. The JTree is displayed in a JPanel. There is an algorithm built into which identifies the nodes whose color I need to change. No user actions is performed (everythign is internal to the program).
    It seems that in the TreeCellRender only kicks in when the tree is first displayed. How do I get it to re-render the tree when the TreeModel changes? It seems that the Listeners are only looking for external user interactions.
    Any help would be greatly appreciated.
    Thanks!

    I think I was a bit too vague in my question. Let me try again...
    I have changed an attribute in a node in a JTree. This attribute is changed after the tree was initially rendered, but while the program is still running. I want to tell the TreeCellRenderer to look again at this node since that attribute that was changed will effect how the node should be renderered. I tried using the nodeChanged() method, but it did not work (the colot of the node did not change). Any advise how I can do this?
    Thanks!

  • Tweaking LAF icons for JTree

    What I want to do, at the moment, is to indicate some JTree nodes as disabled, but this could be a more general question. I realise I can substitute a custome renderer to replace the icons representing tree nodes with an image of my own. But I want the replacements to be consistent in appearance with the standard images used in the Windows LAF, ideally I'd like a coresponding stuff for the metal LAF too.
    So:
    1) Where are the standard bitmaps squirelled away so I can grab and modify them.
    2) Is there a proper way of switching images on the basis of the chosen LAF.

    1) I don't know where they are, but you can get them with (e.g.):
    Icon openIcon = (Icon)UIManager.get("Tree.openIcon");2) You can check the currently installed LAF and then decide which icons you use:
    if (UIManager.getLookAndFeel() instanceof javax.swing.plaf.metal.MetalLookAndFeel) {
      UIManager.put("Tree.openIcon", new ImageIcon(getClass().getResource("resources/images/open.png")));
      UIManager.put("Tree.closedIcon", new ImageIcon(getClass().getResource("resources/images/closed.png")));
      UIManager.put("Tree.collapsedIcon", new ImageIcon(getClass().getResource("resources/images/collapsed.png")));
      UIManager.put("Tree.expandedIcon", new ImageIcon(getClass().getResource("resources/images/expanded.png")));
      UIManager.put("Tree.leafIcon", new ImageIcon(getClass().getResource("resources/images/leaf.png")));
    }After setting such properties you should call
    SwingUtilities.updateComponentTreeUI(<component>);to update the whole component tree starting with <component>. So, <component> often is the applet or the main frame of an application where your ui modifications take place..
    Sources:
    i) LAF-Demos in <java-install-dir>/demo/jfc/Metalworks
    ii) Code example to list LAF-properties in a table (search for "ListProperties"): http://forum.javacore.de/viewtopic.php?p=5682&sid=301abe27a4d5fb3099b130c55a6a52e7

  • Getting TreePath for all nodes in a JTree

    Hi everybody
    I want to get TreePath for all the nodes in a JTree
    Can anybody help me?

    Hi,
    i tried to used your code.
    there is a problem, it is not taking the entire paths.
    so if you have some children,it takes the paths only for the fathers.
    so i correct it to the following way(it will take the entire paths):
    public void getPathForAllNodes(TreePath path)
                  Object node = path.getLastPathComponent();
                  pathArrayList.add(path);
                  TreeModel model = tree.getModel();
                  if(model.isLeaf(node))
                  return;
                  int num = model.getChildCount(node);
                  for(int i = 0; i < num; i++)
                       //pathArrayList.add(path);
                       getPathForAllNodes(path.pathByAddingChild(model.getChild(node, i)));
             }thanks for your code

  • HT2240 I have installed quicktime 7 on my max OS X machine, and I have bought QT 7 pro for OS X. I open Qt7 and enter the registration code I got from buying QT 7 pro, the icon changes to pro, but nothing happens and it doesn't appear to upgrade. help

    I have installed quicktime 7 on my max OS X machine, and I have bought QT 7 pro for OS X. I open Qt7 and enter the registration code I got from buying QT 7 pro, the icon changes to pro, but nothing happens and it doesn't appear to upgrade. help

    I have installed quicktime 7 on my max OS X machine, and I have bought QT 7 pro for OS X. I open Qt7 and enter the registration code I got from buying QT 7 pro, the icon changes to pro, but nothing happens and it doesn't appear to upgrade.
    And just what sort of changes in "appearance" did you expect?
    If you can access the editing, conversion, and/or properties options once a file is loaded then the QT 7 app has been upgreaded for "Pro" use.

  • I have iphone 4 . I don't know how that happened that  the normal color  on all icons changed ( for example, normally green color phone icon is now purple color ) and photo pictures turned negative ( ghost-like ). Your help will be appreciated .

    I have iphone 4 . I don't know how that happenned that the normal color on screen and all icons changed ( for example , normally green color phone icon became purple color ,) and photo pictures turned negative appearance ( ghost-like ) . Any idea how to fix this mishap ?  Your help will be appreciated.

    Hi kshieh
    You can try two possible steps:-
    1]Triple click the home button.  You should get an accessibility menu.deselect the negative/black and white option.
    2]Go to settings->General->accessibility->and deselect invert colours.
    That should fix it
    Rithwik

  • On the most upper left of the Title Bar (the part where the x box is on the right side) where the Firefox icon should be (on the upper left corner) I have a printer icon instead. Why did that happen and how do I change it? Thanks for any help.

    On the most upper left of the Title Bar (the part where the x box is on the right side) where the Firefox icon should be (on the upper left corner) I have a printer icon instead. Why did that happen and how do I change it? Thanks for any help.

    I don't actually have an answer for you, but ditto on changing things that aren't broken. Drives me crazy!!

  • How do I get a dotted line to connect nodes in a JTree?

    I am trying to recreate a Windows Explorer application, does anyone know how they get the dotted lines to connect the nodes in the JTree????

    JTree uses a specific line style to represent the edges between nodes. The default is no edges, but we can set JTree�s lineStyle client property so that each parent node appears connected to each of its child nodes by an angled line:
    myJTree.putClientProperty("JTree.lineStyle", "Angled");
    We can also set this property such that each tree cell is separated by a horizontal line:
    myJTree.putClientProperty("JTree.lineStyle", "Horizontal");
    To disable the line style:
    myJTree.putClientProperty("JTree.lineStyle", "None");
    As with any Swing component, we can also change the UI resource defaults used for all instances of the JTree class. For instance, to change the color of the lines used for rendering the edges between nodes as described above, we can modify the entry in the UI defaults table for this resource as follows:
    UIManager.put("Tree.hash", new ColorUIResource(Color.lightGray))
    Hope this serves your purpose.
    Regards,
    Sachin Shanbhag

  • Selectively editing nodes in a JTree

    I need to make certain nodes in a JTree editable, without making every node in the tree editable. How can I accomplish this?

    Is there some kind of method in a JTree or a DefaultMutableTreeNode that I can call to select the node's text and be able to change it?Hmm. And you did read the API for JTree looking for such methods before you posted here, didn't you?
    So tell us which methods you found to be likely candidates.
    db

Maybe you are looking for