Icon was changed to default icon when editting a node on JTree

I have a tree with icon on nodes. However, when I edit the node, the icon is changed to default icon.
I don't known how to write the treeCellEditor to fix that one.
The following is my code:
package description.ui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.WindowConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class Tree extends javax.swing.JPanel {
     private JTree tree;
     private JScrollPane jScrollPane1;
     public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.getContentPane().add(new Tree());
          frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
          frame.pack();
          frame.show();
     public Tree() {
          super();
          initGUI();
     private void initGUI() {
          try {
               BorderLayout thisLayout = new BorderLayout();
               this.setLayout(thisLayout);
               setPreferredSize(new Dimension(400, 300));
                jScrollPane1 = new JScrollPane();
                this.add(jScrollPane1, BorderLayout.CENTER);
                    DefaultMutableTreeNode rootNode = createNode();
                    tree = new JTree(rootNode);
                    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
                    jScrollPane1.setViewportView(tree);
                    ToolTipManager.sharedInstance().registerComponent(tree);
                    MyCellRenderer cellRenderer = new MyCellRenderer();
                    tree.setCellRenderer(cellRenderer);
                    tree.setEditable(true);
                    tree.setCellEditor(new DefaultTreeCellEditor(tree, cellRenderer));
                    //tree.setCellEditor(new MyCellEditor(tree, cellRenderer));
          } catch (Exception e) {
               e.printStackTrace();
     private void btRemoveActionPerformed(ActionEvent evt) {
         TreePath path = tree.getSelectionPath();
         DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
         ((DefaultTreeModel)tree.getModel()).removeNodeFromParent(selectedNode);
     private DefaultMutableTreeNode createNode() {
         DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Doc");
         DefaultMutableTreeNode ch1 = createChuongNode(rootNode, "Ch1");
         DefaultMutableTreeNode ch2 = createChuongNode(rootNode, "Ch2");
         createTextLeafNode(ch1, "title");
         return rootNode;
     private DefaultMutableTreeNode createChuongNode(DefaultMutableTreeNode parent, String name) {
         DefaultMutableTreeNode node = new DefaultMutableTreeNode(new ChapterNodeData(name));
         parent.add(node);
         return node;
     private DefaultMutableTreeNode createTextLeafNode(DefaultMutableTreeNode parent, String name) {
         DefaultMutableTreeNode node = new DefaultMutableTreeNode(new TitleNodeData(name));
         parent.add(node);
         return node;
      private class MyCellRenderer extends DefaultTreeCellRenderer {
             ImageIcon titleIcon;
             ImageIcon chapterIcon;
             public MyCellRenderer() {
                 titleIcon = new ImageIcon(getClass().getClassLoader()
                        .getResource("description/ui/icons/Text16.gif"));
                 chapterIcon = new ImageIcon(getClass().getClassLoader()
                        .getResource("description/ui/icons/Element16.gif"));
             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 (isChapterNode(value)) {
                     setIcon(chapterIcon);
                     setToolTipText("chapter");
                 } else if (isTextLeafNode(value)) {
                     setIcon(titleIcon);
                     setToolTipText("title");
                 return this;
             protected boolean isChapterNode(Object node) {
                 return ((DefaultMutableTreeNode)node).getUserObject() instanceof ChapterNodeData;
             protected boolean isTextLeafNode(Object node) {
                 return ((DefaultMutableTreeNode)node).getUserObject() instanceof TitleNodeData;
      private class MyCellEditor extends DefaultTreeCellEditor {
             ImageIcon titleIcon;
             ImageIcon chapterIcon;
          public MyCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
              super(tree, renderer);
              titleIcon = new ImageIcon(getClass().getClassLoader()
                     .getResource("description/ui/icons/Text16.gif"));
              titleIcon = new ImageIcon(getClass().getClassLoader()
                     .getResource("description/ui/icons/Element16.gif"));
          public Component getTreeCellEditorComponent(
                       JTree tree,
                       Object value,
                       boolean isSelected,
                       boolean expanded,
                       boolean leaf,
                       int row) {
              super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
              return this.editingComponent;
      abstract class NodeData{
          String name;
          public NodeData(String name) {
              this.name = name;
          public String getName() {
              return name;
          public void setName(String name) {
              this.name = name;
          public String toString() {
              return name;
      class ChapterNodeData extends NodeData {
          public ChapterNodeData(String s) {
              super(s);
      class TitleNodeData extends NodeData {
          public TitleNodeData(String attr) {
              super(attr);
}

Arungeeth wrote:
I know the name of the node... but i cant able to find that nodeHere is some sample code for searching and selecting a node:
    TreeModel model = jtemp.getModel();
    if (model != null) {
        Object root = model.getRoot();
        search(model, root, "Peter");//search for the name 'Peter'
        System.out.println(jtemp.getSelectionPath().getLastPathComponent());
    } else {
        System.out.println("Tree is empty.");
private void search(TreeModel model, Object o, String argSearch) {
    int cc;
    cc = model.getChildCount(o);
    for (int i = 0; i < cc; i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(o, i);
        if (model.isLeaf(child)) {
            TreeNode[] ar = child.getPath();
            String currentValue = Arrays.toString(ar);
            if (currentValue.contains(argSearch)) {
                jtemp.setSelectionPath(new TreePath(ar));
        } else {
            search(model, child, argSearch);
}

Similar Messages

  • How to change the default JRE when there are several version of JRE in Sys

    How to change the default JRE when there are several version of JRE in System?
    i have installed j2sdk1.5.0 then installed j2ee1.4,then installed Weblogic6.1 which use jdk1.3
    Now the JRE is jdk1.3\bin
    When run class that was compiled with jdk1.5,throw:
    java.lang.UnsupportedClassVersionError
    How can i change the jre to 1.5?

    There is a workaround to move from 1.5 version to the older 1.4 version. But this could be specific to the browser setting the JRE version.
    Excerpts from sun docs:
    However, a user can still run older versions. To do so, launch the Java Plug-in Control Panel for the older version, then (re)select the browser in the Browser tab.
    Example:
    Assume you are running on Microsoft Windows with Microsoft Internet Explorer, have first installed version 1.4.2, then version 5.0, and you want to run 1.4.2.
    Go to the j2re1.4.2\bin directory where JRE 1.4.2 was installed. On a Windows default installation, this would be here: C:\Program Files\Java\j2re1.4.2\bin
    Double-click the jpicpl32.exe file located there. It will launch the control panel for 1.4.2.
    Select the Browser tab. Microsoft Internet Explorer might still appear to be set (checked). However, when 5.0 was installed, the registration of the 1.4.2 JRE with Internet Explorer was overwritten by the 5.0 JRE.
    If Microsoft Internet Explorer is shown as checked, uncheck it and click Apply. You will see a confirmation dialog stating that browser settings have changed.
    Check Microsoft Internet Explorer and click Apply. You should see a confirmation dialog.
    Restart the browser. It should now use the 1.4.2 JRE for conventional APPLET tags.
    Details are here
    http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/jcp.html
    My system (Windows XP) has the version 1.5_09 set as the default. However i just installed JRE 1.5_06 and would like to revert back to _06 as the default JRE..
    Will update if i find more information

  • How do I change the default quality when saving a series of edited photos in Photoshop Elements 9

    Say I have saved a series of edited photos using Quality 5. I then change to saving a new series using Quality 10. Each time I save a photo the quality appears as 5 and I have to change it manually to 10. Eventually the default quality will automatically change to 10, but it takes a fair numbers of saves to do this. How do I get the default quality to change as soon as I want it changed rather than having to wait for the software to finally do it itself?

    Interesting point. My situation is where I change the default quality then carry on using the new quality without closing down the program. What you are suggesting is that changing the default quality,  closing down the program, then restarting it might get the quality change to stick. I'll give it a whirl and see what happens.

  • How do I change my default browser when Safari won't open? (crashes)

    I have not been able to get Safari to run, it crashes when trying to open it. I was going to change to a different default browser until I figured out a fix or had time to reinstall the OS (the solution offered by apple tech support) but the only way I know of to change the default browser is a preference inside safari - one that I can't get to. Does anyone know what file I can manually edit on my machine to change that preference setting without opening Safari? Many thanks in advance!

    Hello Dorsey
    Camino
    Omniweb
    Opera
    Shira & Firefox
    I am using Shira & it is a very fast browser, however i would run permissions repair & a disk repair, first if Safari can not be opened long enough to set the default ( You set the default In Safari Preferences->> General tab-> Default Web Browser:)
    Are you using IE or other browser to download the browser of your choice?
    Regards,
    Eme

  • How to change the default structure when exporting data in CSV format?

    Hello,
    can some one tell us how to change the default structure in CRM when exporting lists in CSV format (with Option "Always use unformatted list format (CSV) for download" ? Because we want to add a new structure for our own -is it possible ?
    If it is possible where can we find these structure ? In the blueprint customizing ?
    Thank you very much,
    Christian

    There is a workaround to move from 1.5 version to the older 1.4 version. But this could be specific to the browser setting the JRE version.
    Excerpts from sun docs:
    However, a user can still run older versions. To do so, launch the Java Plug-in Control Panel for the older version, then (re)select the browser in the Browser tab.
    Example:
    Assume you are running on Microsoft Windows with Microsoft Internet Explorer, have first installed version 1.4.2, then version 5.0, and you want to run 1.4.2.
    Go to the j2re1.4.2\bin directory where JRE 1.4.2 was installed. On a Windows default installation, this would be here: C:\Program Files\Java\j2re1.4.2\bin
    Double-click the jpicpl32.exe file located there. It will launch the control panel for 1.4.2.
    Select the Browser tab. Microsoft Internet Explorer might still appear to be set (checked). However, when 5.0 was installed, the registration of the 1.4.2 JRE with Internet Explorer was overwritten by the 5.0 JRE.
    If Microsoft Internet Explorer is shown as checked, uncheck it and click Apply. You will see a confirmation dialog stating that browser settings have changed.
    Check Microsoft Internet Explorer and click Apply. You should see a confirmation dialog.
    Restart the browser. It should now use the 1.4.2 JRE for conventional APPLET tags.
    Details are here
    http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/jcp.html
    My system (Windows XP) has the version 1.5_09 set as the default. However i just installed JRE 1.5_06 and would like to revert back to _06 as the default JRE..
    Will update if i find more information

  • Is it possible to change the default URL when creating new hyperlinks?

    I'm currently working to take a 128 page catalog that was created in InDesign for print and turn it into a interactive PDF to post on our companies website. The catalog contains thousands of part numbers and I'd like to link each one to its corresponding stock status page on our website that gives current availability and pricing for any particular customer.
    So say I'm dealing with part number "XXXX". When I go to add the hyperlink the default web address it spits out is " http://XXXX ". Instead of that, would it be possible to change the default to " http://forums.adobe.com/discussion/XXXX/1 ". Obviously I just used this website as an example, but it would save so much time if I could avoid having to type the exact same URL every time.
    Or maybe is there a quicker way to do this other than what I am currently trying?
    Thanks

    I've found some external software applications that will do it, so that leads me to believe its not possible within ID CC.

  • How do I change the default page when I open a new tab or window?

    <blockquote>Locking duplicate thread.<br>
    Please continue here: [[/questions/872056]]</blockquote>
    I recently downloaded a piece of software to translate languages. I removed the software, but remnants still appear. It changed my homepages (which I was able to change back). But, when I open a new window or tab, the search page associated with this software comes up.

    See:
    *http://www.babylon.com/support/faq/usage.html#22
    *[[/questions/746530]]

  • How do I change the default windows when starting indesign

    The default when I start up Indesign is “only the applications bar is showing”. I would like to be able to change the default to having the Tools and Control showing as well as a few windows like pages, styles, swatches, hyperlinks – not ALL of the windows. I’ve tried setting this up when I first open up Indesign and no documents open. But it still changes back to the default whrn I reopen Indesign. Is there anyway to change this?

    launch indesign--open and position the panels you want--then go to the workspace menu and save your workspace--then, when you launch Indesign it will appear like your saved workspace
    see these articles:
    http://www.tech4pub.com/2012/10/24/indesign-cs5-5-tip-work-efficiently-with-custom-workspa ces/
    http://help.adobe.com/en_US/indesign/cs/using/WSa285fff53dea4f8617383751001ea8cb3f-6d6ea.h tml#WSa285fff53dea4f8617383751001ea8cb3f-6d6ca

  • Changing the default password when configuring.

    Hi,
    can anyone tell me how to change the default password and username when you are first configuring your router?  You know when you go to http://192.168.1.1/?
    I know the default password is admin and there is no user name but I would like to change that so that not just anyone who knows the default password can see the wep incryption I have in place.  Please can someone help?

    What router are you using?
    On most Linksys routers, you cannot change the User Name.  You must leave it blank.
    To change the Password, login to the router, and go to the "Administration" tab.  You should find the Password somewhere nearby.

  • Re: end of song icon when editing in GB-track markings

    Strange thing--I followed the Bullets & Bones'Converting LPs to iTunes article through to making the split on the track, created a New Basic Track, selected its Solo button, and dragged the track to the new track. But the end of song marker (EoS) didn't appear! So, when I sent the track to iTunes, the entire recording was sent instead of just the track I wanted. I had made sure that I clicked the right thing and moved the playhead out of the way, but that little purple triangle didn't appear.
    Is there something the article (a very good one, BTW) forgot to tell me?
    Jacinda

    The End of Song marker probably still sits at the rightmost position it had (it doesn't move left if you shorten the song). So drag it back in, or even better, use the yellow cycle ribbon for finer adjustment.

  • Fonts change size and content when edited in photoshop cs6

    When I try editing the type in the document created in Photoshop CS6, some of the fonts I click on change their size, formatting as well as content of what it says.
    It also started happening when I try changing canvas size. All of the fonts that got corrupted somehow, changing their formatting and content as I'm changing the canvas size.
    The document has been created from scratch..
    thanks for your help ahead of time!
    Lana

    I've reset my brush, so "replace current brushes with the default brushes" was taken.

  • Apply changes overwrites initial record when editing a datagrid

    If I go in to edit record 1 of a datagrid using the pencil icon; use the next/previous buttons to go to another record; then press "Apply Changes", it over writes record 1. Does this happen to anyone else, or is this a bug?
    I'm running Apex 4.1.1 on Oracle 11 XE on Windows XP via EPG. This happens with both Internet Explorer 8 and Google Chrome version "17.0.963.56 m".
    Thanks,
    Tak

    I frequently do this for my grids maybe the datagrid has this fucntionality built in to detect changes made by users to rows?
    Should I make a custom component that extends?
    my current solution is to do this:
    <GridColumn  width="140" headerText="Margin (disabled)" headerText.editMode="Margin (editing)" dataField="margin"  editable="false"  editable.editMode="true"  >
                        <itemEditor>
                            <fx:Component>
                                <DefaultGridItemEditor>
                                    <fx:Script>
                                        <![CDATA[
                                            override public function set value(newValue:Object):void
                                                textArea.text = newValue != null ? newValue.toString() : "";
                                                this.data.isModifiedClientSide = 1;
                                        ]]>
                                    </fx:Script>
                                </DefaultGridItemEditor>
                            </fx:Component>
                        </itemEditor>
                    </GridColumn>

  • Changing JCheckBox default color when disabled

    I am using JCheckBoxes to serve as a checklist providing feedback to the user on how far through a sequence of events the program has progressed. I do not want the user to be abel to check the boxes himself.
    I currently am using JCheckBox.setEnabled(false) to keep the user for ticking the boxes. Unfortunately this changes the foreground color from black (readable) to gray (unreadable). setForeground has no effect.
    So I have two questions:
    1. Is there a better way to secure JCheckBoxes from the user?
    2. If not is there a way to alter the default disabled color for specific CheckBoxes?

    hopefully there's a better way than this
    (the checkBoxIcon code is just to do with the tick)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.metal.*;
    class Testing extends JFrame
      public Testing()
        UIManager.put("CheckBox.disabledText",Color.BLACK);
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JCheckBox cbx = new JCheckBox("Disabled",new CheckBoxIcon(),true);
        getContentPane().add(cbx,BorderLayout.NORTH);
        getContentPane().add(new JCheckBox("Enabled",true),BorderLayout.SOUTH);
        pack();
        cbx.setEnabled(false);
      public static void main(String[] args){new Testing().setVisible(true);}
    class CheckBoxIcon implements Icon
      protected int getControlSize() { return 13; }
      private void paintOceanIcon(Component c, Graphics g, int x, int y)
        ButtonModel model = ((JCheckBox)c).getModel();
        g.translate(x, y);
        int w = getIconWidth();
        int h = getIconHeight();
        if (model.isPressed() && model.isArmed())
          g.setColor(MetalLookAndFeel.getControlShadow());
          g.fillRect(0, 0, w, h);
          g.setColor(MetalLookAndFeel.getControlDarkShadow());
          g.fillRect(0, 0, w, 2);
          g.fillRect(0, 2, 2, h - 2);
          g.fillRect(w - 1, 1, 1, h - 1);
          g.fillRect(1, h - 1, w - 2, 1);
        g.setColor( MetalLookAndFeel.getControlInfo() );
        g.translate(-x, -y);
        if (model.isSelected())
          drawCheck(c,g,x,y);
      public void paintIcon(Component c, Graphics g, int x, int y)
        ButtonModel model = ((JCheckBox)c).getModel();
        int controlSize = getControlSize();
        if (model.isPressed() && model.isArmed())
          g.setColor( MetalLookAndFeel.getControlShadow() );
          g.fillRect( x, y, controlSize-1, controlSize-1);
          drawPressed3DBorder(g, x, y, controlSize, controlSize);
        else
          drawFlush3DBorder(g, x, y, controlSize, controlSize);
        g.setColor( MetalLookAndFeel.getControlInfo() );
        if (model.isSelected())
          drawCheck(c,g,x,y);
      private void drawFlush3DBorder(Graphics g, int x, int y, int w, int h)
        g.translate(x, y);
        g.setColor(MetalLookAndFeel.getControlDarkShadow());
        g.drawRect(0, 0, w-2, h-2);
        g.setColor(MetalLookAndFeel.getControlHighlight());
        g.drawRect(1, 1, w-2, h-2);
        g.setColor(MetalLookAndFeel.getControl());
        g.drawLine(0, h-1, 1, h-2);
        g.drawLine(w-1, 0, w-2, 1);
        g.translate(-x, -y);
      private void drawPressed3DBorder(Graphics g, int x, int y, int w, int h)
        g.translate(x, y);
        drawFlush3DBorder(g, 0, 0, w, h);
        g.setColor(MetalLookAndFeel.getControlShadow());
        g.drawLine(1, 1, 1, h-2);
        g.drawLine(1, 1, w-2, 1);
        g.translate(-x, -y);
      protected void drawCheck(Component c, Graphics g, int x, int y)
        int controlSize = getControlSize();
        g.fillRect( x+3, y+5, 2, controlSize-8 );
        g.drawLine( x+(controlSize-4), y+3, x+5, y+(controlSize-6) );
        g.drawLine( x+(controlSize-4), y+4, x+5, y+(controlSize-5) );
      public int getIconWidth() {return getControlSize();}
      public int getIconHeight() {return getControlSize();}
    }

  • How do I change the default setting when printing a book

    I live in Mexico and i just bought an iMac, the iPhoto software defaults to the Spain website and I would like to change the settings so that I can print books and have them sent to an US address. Any ideas?

    According to this article, you cannot have iPhoto books shipped to an U.S. address if your billing address is outside the U.S. or Canada.
    (19841)

  • How do I change the default location when browsing white pages or stores

    when viewing walmart it show my store as Kahului, Maui. How do I change it to show store location as waipahu, hawaii?

    If Walmart isn't using geolocation, see this. <br />
    http://whatismyipaddress.com/
    Where does your IP Address say you are located?

Maybe you are looking for

  • How to deploy web app from PC to Linux with Netbeans ?

    I am developing a web app in Netbeans 6.0 on my PC, using the Tomcat comes with the IDE, now I need to deploy it to a Linux server running Tomcat, can I do it from inside the IDE ? Can it copy all the servlets and other java classes to the Linux for

  • Networking a firewire hard drive from a Mac and access the drive from a PC

    I'm trying to network a firewire hard drive from my Mac and access that hard drive from a Windows XP PC. I can see the main internal hard drive but I cannot see the firewire hard drive. Does anybody have a solution or work around? Many thanks, Roy

  • Scroll a dataGrid using a button

    Please help me know the way to scroll through dataGrid items using buttons instead of the dataGrids scrollbar. Many thanks, Trace

  • FSL-06002 Error 1060

    During installation of WAS6.4SP9 i see the follwowing error in the log: ERROR 2005-02-21 13:19:18 FSL-06002  Error 1060 (The specified service does not exist as an installed service. ) in execution of a 'OpenService' function, line (255), with parame

  • My applet doesn't start in IE with it's default java runtime machine!!

    i have written an applet which works with UTF-8. in my computer i have java 1.4 SDK and it is easily working but when i run this applet in another computer with windows 2000 O.S. with IE it doesn't work.but other applet works properly on it.what can