Adding JCheckbox as icon to JTree Nodes

Hi,
I need to provide a GUI for something like instllation shield. The application shows a list of modules and sub modules in a tree format. Users can select any sub modules for installation, using a checkbox ( located at icon space of a JTree, if possible ).
The checkbox at each module indicates (i) All sub modules are selected, (ii) All sub modules are deselected, (iii) Partial selection.
Is it possible? How to do that?
Please help me out!
Prashant

Oh thank you.
I found the stuff on the link you gave. It is helpful eventhough code is not availabale. I am planning to write the code by implementing necessary interfaces.
Thanks again,
Prashant

Similar Messages

  • Using animation as icon for JTree node

    Hi,
    I am using a custom tree cell renderer. I have a label in the renderer, the label have gif Image Icon, but the problem is it is not getting animated. But when I use a JLabel with gif icon some where else it is working fine, but it is not working for tree node.
    package com.gopi.utilities.gui;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridBagConstraints;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.TreeCellRenderer;
    import com.gopi.remfilebrowser.gui.GUIUtil;
    import com.gopi.remfilebrowser.util.FileBrowserConstants;
    public class CustomTreeCellRenderer implements TreeCellRenderer
         private JPanel panel;
         private JLabel label;
         private TreeCellRenderer defaultRenderer;
         public CustomTreeCellRenderer()
              super();
              panel = GUIUtil.createGridBagPanel();
              label = new JLabel();
              label.setHorizontalAlignment(JLabel.LEFT);
              System.out.println("New");
              GridBagConstraints gc = new GridBagConstraints();
              GUIUtil.fillComponent(panel,label);
              defaultRenderer = new DefaultTreeCellRenderer();
         public Component getTreeCellRendererComponent(JTree tree, Object value,
                     boolean sel,
                     boolean expanded,
                     boolean leaf, int row,
                     boolean hasFocus)
              if(value instanceof NewAbstractTreeNode)
                   NewAbstractTreeNode node = (NewAbstractTreeNode) value;
                   System.out.println("dr");
                   label.setText(value.toString());
                   label.setIcon(ImageLoader.getInstance().getIcon(node.getIconKey()));
                   if(hasFocus && sel)
                        panel.setBackground(FileBrowserConstants.TREE_NODE_SELECTED_COLOR);
                   else if(sel)
                        panel.setBackground(FileBrowserConstants.TREE_NODE_UNSELECTED_COLOR);
                   else
                        panel.setBackground(Color.white);
                   return panel;
              return defaultRenderer.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
    }

    JLabels using ImageIcons are designed to display the icon as is, including animation and all.
    A CellRenderer only paints the Icon once, when the cell is painted. Much ike a rubber stamp of the JComponent. Hence, its not designed to do the animation and all.
    If you really want it, you can probably use MediaTracker and a Timer to do your animation scheduling. Might not be very pretty code though
    ICE

  • Jtree node refuses to collapse upon clicking handle; makeVisible() was used

    Hello,
    While creating a new node and inserting as a leaf in the JTree, I use tree.makeVisible(newTreePath) to expand the new node and make visible. (Using expandPath() will not expand if a leaf was added).
    However now the jtree node refuses to collapse upon clicking the node handle.
    How do I get it to not insist on staying open - be able to collapse manually?
    thanks,
    Anil

    This is the JNI forum. I don't believe your post fits.
    then
    can I correctly assume that any class that
    "implements Cloneable" will handle making either a
    "shallow" or "deep" ... or even "semi-shallow" clone,
    respective to the class context .. right?
    It probably does something. The implementor might not have implemented anything though. And you have no idea what they implemented.
    No idea about your other question.
    You might want to think carefully about why you are cloning though.

  • 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

  • JTree Node Icons

    Ok i know how to set a icon for all nodes in a JTree but my problem is setting incons for certain nodes. I am trying to Create a UML Training Tool for my degree and i have major trouble here. I can change the icons for all but all i want to do is change them for individual nodes, for example a different icon for an actor to a use case, please please can someone help this beginner.

    You need to subclass DefaultTreeCellRenderer and override the getTreeCellRendererComponent method, like:
    class CustomRenderer extends DefaultTreeCellRenderer {
      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 (whatever) setIcon(myIcon);
    }                 Then set the cell renderer for your tree:
    myTree.setCellRenderer(new CustomRenderer());

  • How to change icons of a JTree node dynamically

    Hi all!
    I want to change icon associated with a node dynamically ( i.e. after the tree has being displayed). How can i achieve this?
    Can any one provide me a sample code snippet.
    Thanks in advance
    Murali

    I have created CustomCellRenderer and i'm calling this class as follows
    tree.setCellRenderer((TreeCellRenderer) new CustomCellRenderer(true));
    The boolean value for the constructor (in this case it's true) will be set to
    a local variable in the CustomCellRenderer class. Then upon clicking a node in the tree the boolean value (true) is set and the icon for that node should be changed as specified in the CustomCellRenderer class.
    When i click on a node all the icons of that tree are disappearing.
    Can any one help me in this issue
    public class CustomCellRenderer
              extends          JLabel
              implements     TreeCellRenderer
    private ImageIcon          grayfolderImage;
    private ImageIcon          greenfolderImage;
    private ImageIcon          bluefolderImage;
    private ImageIcon          redfolderImage;
    private ImageIcon          whitefolderImage;
    private boolean               bSelected;
    boolean logfileDeleted;
         public CustomCellRenderer()
              grayfolderImage = new ImageIcon("C:\\images\\grayFolder.gif");     
              greenfolderImage = new ImageIcon("C:\\images\\greenFolder.gif");     
              bluefolderImage = new ImageIcon("C:\\images\\blueFolder.gif");     
              redfolderImage = new ImageIcon("C:\\images\\redFolder.gif");
              whitefolderImage = new ImageIcon("C:\\images\\whiteFolder.gif");     
         public CustomCellRenderer(boolean logfileDeleted){
              this.logfileDeleted = logfileDeleted;
         public Component getTreeCellRendererComponent( JTree tree,
                             Object value, boolean bSelected, boolean bExpanded,
                                       boolean bLeaf, int iRow, boolean bHasFocus )
              // Find out which node we are rendering and get its text
              DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
              String     labelText = (String)node.getUserObject();
              this.bSelected = bSelected;
              // Set the correct foreground color
              /*if( !bSelected )
                   setForeground( Color.black );
              else
                   setForeground( Color.red ); */
              // Determine the correct icon to display
              if( labelText.equals( "ioexception001" ) )
                   setIcon( redfolderImage );
              else if( labelText.equals( "ioexception002" ) )
                   setIcon( greenfolderImage );
              else if( logfileDeleted ==true )
                   setIcon( whitefolderImage );
              else if( labelText.equals( "ioexception004" ) )
                   setIcon( redfolderImage );
              else
                   setIcon(bluefolderImage);
              // Add the text to the cell
              setText( labelText );
              return this;
         // This is a hack to paint the background. Normally a JLabel can
         // paint its own background, but due to an apparent bug or
         // limitation in the TreeCellRenderer, the paint method is
         // required to handle this.
         public void paint( Graphics g )
              Color          bColor;
              Icon          currentI = getIcon();
              // Set the correct background color
              bColor = bSelected ? SystemColor.textHighlight : Color.white;
              g.setColor( bColor );
              // Draw a rectangle in the background of the cell
              g.fillRect( 0, 0, getWidth() - 1, getHeight() - 1 );
              super.paint( g );
    }

  • Set icon for toggle control of JTree node.

    hi.
    Just a question.
    I am looking towards setting the icons of the JTree by this i mean the icon that you click on
    to open say a node .
    I know you can set the icons of the nodes by setIcon inside your TreeCellRenderer but i doubt that
    that will set the icon that is to the left ot your label.
    Has anyone gone down this path recently.
    The class BasicTreeUI has a method
    called paintExpandControl( Graphics g,
    Rectangle clipBounds,
    . Insets insets,
    Rectangle bounds,
    TreePath path,
    int row,boolean isExpanded,
    boolean hasBeenExpanded,
    boolean isLeaf)
    Hopefull this is a method that can paint the toggle node so has anyone subclassed BasicTreeUI and done something like this recently.
    Any advise on the topic is really appreciated.
    Thank you.

    Yes, they are icons and they are called "handles". The easiest way to change then is to use UIManager.
    If you haven't done so before, look through the hash table UIManager.getLookAndFeelDefaults().
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    public class TreeIcons {
         public static void main(String[] args) throws MalformedURLException {
              String prefix = "http://forum.java.sun.com/images/";
              UIManager.put("Tree.openIcon", new ImageIcon(new URL(prefix + "dont_save.gif")));
              UIManager.put("Tree.closedIcon", new ImageIcon(new URL(prefix + "save.gif")));
              UIManager.put("Tree.leafIcon", new ImageIcon(new URL(prefix + "forum_new.gif")));
              UIManager.put("Tree.expandedIcon", new ImageIcon(new URL(prefix + "email_faded.gif")));
              UIManager.put("Tree.collapsedIcon", new ImageIcon(new URL(prefix + "email.gif")));
              JFrame f = new JFrame("TreeIcons");
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.getContentPane().add(new JTree());
              f.setSize(new Dimension(300,400));
              f.setLocationRelativeTo(null);
              f.setVisible(true);
    }

  • How to get the icon type of a JTree node, very urgent

    hi,
    i need to get the type of a JTree node but don't know how. this is quite an emergency. please help. thank you all.
    -joey

    there are several icons defined in the DefaultTreeCellRenderer class, such as closedIcon, leafIcon, openIcon. i was wondering if there is any way to get the icon type of a node. thanks for your reply.
    -joey

  • Possibility to have your own icons in JTree depending on what node

    Can you specify your own icons in a JTree? With this I mean more than your own directory/leaf icons. For example, if you have a family tree, you might want different icons for sons and daugthers etc.

    I did like you said, and it works, almost.
    I use the following code to find out what type my nodes in the tree are. I can just look at my NodeInfo object to get that. But when I click at different nodes in the tree, other nodes than the clicked one changes icons.
    DefaultMutableTreeNode node =
                   (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
              NodeInfo selectedNode = (NodeInfo)node.getUserObject();Edit: Also tried this code but that gave me:
    Exception in thread "main" java.lang.ClassCastException: java.lang.String
         at gui.MyTreeCellRenderer.getTreeCellRendererComponent(MyTreeCellRenderer.java:26)
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
              NodeInfo selectedNode = (NodeInfo)node.getUserObject();Message was edited by:
    sandsater

  • How to display different icon, for different node of jtree

    Hi All,
    How to display different icon, for different node of jtree

    you haven't responded to my last post here: [http://forums.sun.com/thread.jspa?threadID=5323190&messageID=10382676#10382676|http://forums.sun.com/thread.jspa?threadID=5323190&messageID=10382676#10382676]

  • Parse and display xml doc when click on JTree node?

    Ok so here is the code that I have now. You will have to make alot of html files to run the program, but they can be empty for now. What you can do is just put the same exact html file to be displayed in each node element. for example.
    DefaultMutableTreeNode n1_node1_b1 =
                        new DefaultMutableTreeNode(new ModuleInfo("Device", "device.html"));could be changed to
    DefaultMutableTreeNode n1_node1_b1 =
                        new DefaultMutableTreeNode(new ModuleInfo("Device", "status.html"));And just keep adding the status.html page to each one of the nodes.
    anyway.
    For now when I click on the node, the html file is displayed in the JScrollPane rPane = new JScrollPane(htmlPane); and the htmlPane is a JEditorPane
    what I need it to do.
    When I click on the JTree node. I would like to go out to an xml file, parse the information in it, and display it to the pane.
    I will need to later, be able to add, edit, or delete information on the xml through the pane later.
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.JTree;
    import javax.swing.JPanel;
    import javax.accessibility.*;
    import javax.swing.UIManager;
    import javax.swing.JComponent;
    import javax.swing.JEditorPane;
    import javax.swing.tree.TreeSelectionModel;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.ImageIcon;
    import java.net.URL;
    import java.io.IOException;
    import java.awt.Dimension;
    * @author orozcom
    public class Example extends JFrame implements TreeSelectionListener
        private JEditorPane htmlPane;
        private URL helpURL;
        private static boolean DEBUG = false;
        private JTree tree;
        public Example()
            super("Example");
            setSize(1200,900);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            JMenuBar jmb = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenuItem openItem = new JMenuItem("Open");
            JMenuItem saveItem = new JMenuItem("Save");
            JMenuItem exitItem = new JMenuItem("Exit");
            exitItem.addActionListener(new ActionListener()
                public void actionPerformed(ActionEvent ae)
                    System.exit(0);
            fileMenu.add(openItem);
            fileMenu.add(saveItem);
            fileMenu.add(new JSeparator());
            fileMenu.add(exitItem);
            jmb.add(fileMenu);
            setJMenuBar(jmb);
            //  **************** start JTree ******************//       
            JPanel lPane =new JPanel();       
            lPane.setLayout(new BorderLayout());
            DefaultMutableTreeNode top = new DefaultMutableTreeNode(new ModuleInfo("Devices","splashScreen.html"));
            DefaultMutableTreeNode branch1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Module 1", "module1.html"));
            DefaultMutableTreeNode branch2 =
                    new DefaultMutableTreeNode(new ModuleInfo("Module 2", "module2.html"));
            //DefaultMutableTreeNode branch3 =
                    //new DefaultMutableTreeNode(new ModuleInfo("Module 3", "module3.html"));
            //adding to the topmost node
            top.add(branch1);
            top.add(branch2);
            //top.add(branch3);
             *          BRANCH 1           *
            //adding to the First branch
            DefaultMutableTreeNode node1_b1
                    =new DefaultMutableTreeNode(new ModuleInfo("Status", "status.html"));
            //branch1.add(node1_b1);
                DefaultMutableTreeNode n1_node1_b1 =
                        new DefaultMutableTreeNode(new ModuleInfo("Device", "device.html"));
                DefaultMutableTreeNode n2_node1_b1 =
                        new DefaultMutableTreeNode(new ModuleInfo("Network", "network.html"));
                DefaultMutableTreeNode n3_node1_b1 =
                        new DefaultMutableTreeNode(new ModuleInfo("Chassis", "chassis.html"));
                DefaultMutableTreeNode n4_node1_b1 =
                        new DefaultMutableTreeNode(new ModuleInfo("Resources", "resources.html"));
                node1_b1.add(n1_node1_b1);
                node1_b1.add(n2_node1_b1);
                node1_b1.add(n3_node1_b1);
                node1_b1.add(n4_node1_b1);    
            DefaultMutableTreeNode node2_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Project Editor", "projedit.html"));
            DefaultMutableTreeNode node3_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Project Manager", "projmngt.html"));
            DefaultMutableTreeNode node4_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Administration", "administration.html"));
            DefaultMutableTreeNode n1_node4_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Device", "device.html"));
            DefaultMutableTreeNode n2_node4_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Network", "network.html"));
            DefaultMutableTreeNode n3_node4_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Users", "users.html"));
            node4_b1.add(n1_node4_b1);
            node4_b1.add(n2_node4_b1);
            node4_b1.add(n3_node4_b1);
            DefaultMutableTreeNode node5_b1 =
                    new DefaultMutableTreeNode(new ModuleInfo("Logging", "logging.html"));
            branch1.add(node1_b1);
            branch1.add(node2_b1);
            branch1.add(node3_b1);
            branch1.add(node4_b1);
            branch1.add(node5_b1);
             *          BRANCH 2           *
            //adding to the Second branch
            DefaultMutableTreeNode node1_b2 =
                    new DefaultMutableTreeNode(new ModuleInfo("Status", "status.html"));
                DefaultMutableTreeNode n1_node1_b2 =
                        new DefaultMutableTreeNode(new ModuleInfo("Device", "device.html"));
                DefaultMutableTreeNode n2_node1_b2 =
                        new DefaultMutableTreeNode(new ModuleInfo("Network", "network.html"));
                DefaultMutableTreeNode n3_node1_b2 =
                        new DefaultMutableTreeNode(new ModuleInfo("Chassis", "chassis.html"));
                DefaultMutableTreeNode n4_node1_b2 =
                        new DefaultMutableTreeNode(new ModuleInfo("Resources", "resources.html"));
                node1_b2.add(n1_node1_b2);
                node1_b2.add(n2_node1_b2);
                node1_b2.add(n3_node1_b2);
                node1_b2.add(n4_node1_b2);
            DefaultMutableTreeNode node2_b2=
                    new DefaultMutableTreeNode(new ModuleInfo("Project Editor", "projedit.html"));
            DefaultMutableTreeNode node3_b2=
                    new DefaultMutableTreeNode(new ModuleInfo("Project Manager", "projmngt.html"));
            DefaultMutableTreeNode node4_b2=
                    new DefaultMutableTreeNode(new ModuleInfo("Administration", "administration.html"));
                DefaultMutableTreeNode n1_node4_b2=
                        new DefaultMutableTreeNode(new ModuleInfo("Device", "device.html"));
                DefaultMutableTreeNode n2_node4_b2=
                        new DefaultMutableTreeNode(new ModuleInfo("Network", "network.html"));
                DefaultMutableTreeNode n3_node4_b2=
                        new DefaultMutableTreeNode(new ModuleInfo("Users", "users.html"));
                node4_b2.add(n1_node4_b2);
                node4_b2.add(n2_node4_b2);
                node4_b2.add(n3_node4_b2);
            DefaultMutableTreeNode node5_b2=
                    new DefaultMutableTreeNode(new ModuleInfo("Logging", "logging.html"));
            branch2.add(node1_b2);
            branch2.add(node2_b2);
            branch2.add(node3_b2);
            branch2.add(node4_b2);
            branch2.add(node5_b2);
             *          BRANCH 3           *
            //adding to the Third branch
            DefaultMutableTreeNode node1_b3=new DefaultMutableTreeNode("Status");
            DefaultMutableTreeNode n1_node1_b3=new DefaultMutableTreeNode("Device");
            DefaultMutableTreeNode n2_node1_b3=new DefaultMutableTreeNode("Network");
            DefaultMutableTreeNode n3_node1_b3=new DefaultMutableTreeNode("Chassis");
            DefaultMutableTreeNode n4_node1_b3=new DefaultMutableTreeNode("Resources");
            node1_b3.add(n1_node1_b3);
            node1_b3.add(n2_node1_b3);
            node1_b3.add(n3_node1_b3);
            node1_b3.add(n4_node1_b3);
            DefaultMutableTreeNode node2_b3=new DefaultMutableTreeNode("Project Editor");
            DefaultMutableTreeNode node3_b3=new DefaultMutableTreeNode("Project Manager");
            DefaultMutableTreeNode node4_b3=new DefaultMutableTreeNode("Administration");
            DefaultMutableTreeNode n1_node4_b3=new DefaultMutableTreeNode("Device");
            DefaultMutableTreeNode n2_node4_b3=new DefaultMutableTreeNode("Network");
            DefaultMutableTreeNode n3_node4_b3=new DefaultMutableTreeNode("Users");
            node4_b3.add(n1_node4_b3);
            node4_b3.add(n2_node4_b3);
            node4_b3.add(n3_node4_b3);
            DefaultMutableTreeNode node5_b3=new DefaultMutableTreeNode("Logging");
            branch3.add(node1_b3);
            branch3.add(node2_b3);
            branch3.add(node3_b3);
            branch3.add(node4_b3);
            branch3.add(node5_b3);
            tree=new JTree(top,true);
            //tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
            //Set the icon for leaf nodes.       
            ImageIcon deviceIcon = createImageIcon("/device.gif");       
            if (deviceIcon != null)
                DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
                renderer.setLeafIcon(deviceIcon);
                tree.setCellRenderer(renderer);
            else
                System.err.println("Leaf icon missing; using default.");
            //Listen for when the selection changes.
            tree.addTreeSelectionListener(this);
            //Create the scroll pane and add the tree to it.
            //JScrollPane treeView = new JScrollPane(tree);
            tree.setToolTipText(" and ");
            lPane.add(tree);
            getContentPane().add(lPane);
            //  ****************  end  JTree  ******************//
            htmlPane = new JEditorPane();
            htmlPane.setEditable(false);       
            initHelp();
            JScrollPane rPane = new JScrollPane(htmlPane);
            JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lPane, rPane);
            jsp.setDividerLocation(180);
            getContentPane().add(jsp, BorderLayout.CENTER);
            JPanel bPane = new JPanel();
            JButton okButton = new JButton("Ok");
            JButton applyButton = new JButton("Apply");
            JButton clearButton = new JButton("Clear");
            bPane.add(okButton);
            bPane.add(applyButton);
            bPane.add(clearButton);
            getContentPane().add(bPane, BorderLayout.SOUTH);
            setVisible(true);
        /** Required by TreeSelectionListener interface. */
        public void valueChanged(TreeSelectionEvent e)
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
            tree.getLastSelectedPathComponent();
            if (node == null) return;
            Object nodeInfo = node.getUserObject();
            if (node.isLeaf())
                ModuleInfo module = (ModuleInfo)nodeInfo;
                displayURL(module.moduleURL);
                if (DEBUG)
                    System.out.print(module.moduleURL + ":  \n    ");
            else
                displayURL(helpURL);
            if (DEBUG)
                System.out.println(nodeInfo.toString());
        private class ModuleInfo
            public String deviceName;
            public URL moduleURL;
            public ModuleInfo(String device, String filename)
                deviceName = device;
                moduleURL = (URL)Example.class.getResource("/" + filename);
                if (moduleURL == null)
                    System.err.println("Couldn't find file: "
                            + filename);
            public String toString()
                return deviceName;
        private void initHelp()
            String s = "YFDemoHelp.html";
            helpURL = Example.class.getResource("/" + s);
            if (helpURL == null)
                System.err.println("Couldn't open help file: " + s);
            else if (DEBUG)
                System.out.println("Help URL is " + helpURL);
            displayURL(helpURL);
        private void displayURL(URL url)
            try
                if (url != null)
                    htmlPane.setPage(url);
                else
                { //null url
                    htmlPane.setText("File Not Found");
                    if (DEBUG)
                        System.out.println("Attempted to display a null URL.");
            catch (IOException e)
                System.err.println("Attempted to read a bad URL: " + url);
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path)
            java.net.URL imgURL = Example.class.getResource(path);
            if (imgURL != null)
                return new ImageIcon(imgURL);
            else
                System.err.println("Couldn't find file: " + path);
                return null;
        public static void main(String args[])
            new Example();
            //new AssistiveExample();
    }Thanks orozcom

    yes i can at the time of MIRO u can see both
    go in SU01 for and user
    in Parameters enter IVFIDISPLAY in Parameter ID and X in Paramater Value
    thus for that user u will be able to c Invoice number and accounting number after saving MIRo
    u will get message like
    Invoice document 5105608893 was posted ( Accountng Documnt: 5100000000 )
    hope this helps

  • 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 sort Jtree node?

    Now, I want to sort tree node by increasing.
    for example there are three child node:
    [20 -40][
    10 - 20]
    [30- 60].
    they are added to JTree node by different order.but are shown in increasingorder.
    like this:
    [10 - 20]
    [20 -40][
    [30- 60].
    how to control tree node.

    Implement the Comparable interface in t he userobject class and possibly the toString(0 as well.
    Ex.
    public class UserObject implements java.lang.Comparable{
    public int compareTo(Object obj) throws ClassCastException
    public String toString()

  • JTree node incorrectly becomes a leaf after collapse?

    Hi all,
    I'm trying to fix a bug in a JTree where a node spuriously turns into a leaf after the user collapses it, and I'm beginning to wonder whether it's my code that's at fault or something within Java (The project uses JDK 1.3.1.06 - I cannot change this part!)
    The problem is as follows. The user expands the tree and selects a leaf. He or she then clicks the collapse icon of a node somewhere in the path to that leaf. (that node can be the parent, grandparent, great-grandparent or any other node, right up to the root.) The node collapses, but the expand icon is not displayed beside the collapsed node, and double-clicking does not cause the node to expand. In other words, the node now looks like a leaf.
    The problem does not occur if the user collapses the node by double-clicking on the node, or if an non-leaf node was selected.
    My swing knowledge is not that extensive, so I was wondering whether this was a known bug, or whether anyone can think of some obvious mistake I might be making...
    /Michael

    HI,
    Is the node which is becoming leaf do have some childrens?
    If not than checkout while creating object of DefaultMutableTreeNode did u specified have its allowsChildren property to false.
    In case your case is different from above. Than you might have encountered some bug in JTree, which you can report at following URI.
    http://java.sun.com/webapps/bugreport/
    Cheers
    -Hemant

Maybe you are looking for