JTree's handle icon

Hi,
Can we change the icon of the handles of a JTree?
If yes, how can we do that?
Thanks,
Jason

It happens that when I click on the handle icon of the ancestor node, an Exception occurs:
java.lang.NullPointerException
What is goin' on?
Here is code snippet:
void jTree1_valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
Object birac = node.getUserObject();
TreePath niz = (TreePath)jTree1.getLeadSelectionPath();
jTextArea1.setText("");
if (jTree1.isVisible(niz)) {
if (node.isLeaf()) {
My_Class bt = (My_Class)birac;
String tabela1 = bt.getImeTabele();
if (qds1.isOpen()) {
try {
qds1.close();
} catch (Exception ex){} } //if

Similar Messages

  • Help with JTree node handles icon

    Hi
    In my application i use Jtree to display the hirearchical data.But i don't want to the node handles icon to be displayed in the screen.The root node handle icon will be not be displayed if we set jtree.setShowsRootHandles(false) .Is there any way to diable the other node handles icons too?.Please help me in overcoming the problem.
    So, i will be handling the expand/collapse operations of the node with the help of mouse click event.
    Thanks in advance.
    Regards,
    Krish.

    Hi all,
    Any ideas to overcome this problem.?
    Thanks,
    Krish

  • Displaying nodes in a JTree with different icons

    Please can you help me? I have set up a JTree that handles different icons for each item in the tree. The items are all independent and there is no way of grouping them together, so to get around the problem of displaying them in the tree I have stored them in array of images, such that each row in the tree maps to the correct element of the tree (or so I thought).
    However, my overridden version of getTreeCellRendererComponent() displays interesting behaviour. If one of the branches in tree is collapsed, then the number of rows displayed in the tree differs from when it is expanded. This renders my use of an array useless because it doesn't map to what I thought was the structure of the tree and my icons do not align to the correct textual information.
    How can I map my images to the tree so that it displays correctly when the branches are expanded or collapsed?

    You need to be able to figure out what icon to display based on the object that the tree cell is rendering. In other words, the object should have an attribute that you can query that tells your cell renderer what icon to use. If all else fails, change your object to contain a reference to the icon that should be displayed next to it.

  • Jtree leaf custom icons

    I created my own class that extands DefaultTreeCellRenderer and overrided the function getTreeCellRendererComponent.For now I see the customed icon on the leaf,but when I touch with the mouse the panel the vision of the leaf comes back to default.I tried to do repaint it's doesn't help me
    May be you have any suggestions?

    ok I did the following code in a previous project to handle a similer requirement
    It s from a large project so I'll only post a small part and hope that you can understand it
    If you dont just let me know which part
    package lrmk.client.nav;
    import lrmk.client.*;
    import lrmk.client.wnd.*;
    import javax.swing.tree.*;
    import java.awt.*;
    import javax.swing.*;
    public class NavCellRenderer implements javax.swing.tree.TreeCellRenderer
       DefaultTreeCellRenderer form_window  =  new DefaultTreeCellRenderer();
       DefaultTreeCellRenderer dialog_box   =  new DefaultTreeCellRenderer();
       DefaultTreeCellRenderer table_window =  new DefaultTreeCellRenderer();
       DefaultTreeCellRenderer custom =  new DefaultTreeCellRenderer();
       DefaultTreeCellRenderer def  =  new DefaultTreeCellRenderer();
       public NavCellRenderer(){
          form_window.setLeafIcon(ClientGlobals.getImageIcon("lrmk/client/images/form_icon.gif"));
          dialog_box.setLeafIcon(ClientGlobals.getImageIcon("lrmk/client/images/dialog_icon.gif"));
          table_window.setLeafIcon(ClientGlobals.getImageIcon("lrmk/client/images/table_icon.gif"));
       public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
          TreeCellRenderer r = def;
          NavNode nn = null;
          if (value instanceof NavNode)
             nn = (NavNode)value;
          if (nn != null && nn.getIcon() != null)
             r = custom;
             custom.setLeafIcon(nn.getIcon());
          else if (value instanceof WindowNavNode)
             WindowNavNode wnn = (WindowNavNode)value;
             if (CDialogBox.class.isAssignableFrom(wnn.getWndClass()))
                r = dialog_box;
             else
                String name = wnn.getWndClass().getName();
                name = name.substring(name.lastIndexOf("."));
                if (name.startsWith(".Frm"))
                   r = form_window;
                else if (name.startsWith(".Tbw"))
                   r = table_window;
          return r.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
    }

  • JTree change handles of node

    Hi!
    I am using MetalLookAndFeel in my program. I also have a JTree which by default uses Java handles (o--)
    I need to show the handles of windowsLookAndFeel (+ and -) in the tree, but without changing the MetalLookAndFeel.
    Is there a way to do this in JAVA?

    To set the icons,
    UIManager.put("Tree.collapsedIcon", icon1);
    UIManager.put("Tree.expandedIcon", icon2);Note that in order to acquire the icons from the Windows L&F itself you would need to be runnin on a Windows platform, so it may be preferable to include PNGs of them in your classpath. Up to you.

  • JTree Update Leaf Icon

    Hi,
    I have two problem with Jtree.
    1.How can I put different Icons for different leafs of the same node?
    2.How is it possible to refresh or update the Jtree(by adding or removing some leaf) that the leaf Icons remain?
    Thanks for your help.

    1.How can I put different Icons for different leafs of the same node?Yes, ... either write a custom cell renderer or find a "merged icon".. there are examples of both around the forums.
    2.How is it possible to refresh or update the Jtree(by adding or removing
    some leaf) that the leaf Icons remain?No. if you remove a node, the node is gone. If you don't remove the node, the node stays. IF you want to leave the leaf icon, you can have a renderer which just displays the icon and not the text.

  • Jtree change childs icons

    Hi All,
    im wondering if someone try that.
    i have a tree that include a parent and child/s.
    when i selected the father all the child's should be selected too.
    i was trying :
    public void valueChanged(TreeSelectionEvent arg0) {
                        // TODO Auto-generated method stub
                        DefaultMutableTreeNode node = DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                        String s2 = (String)node.getUserObject();
                        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
                       renderer.setLeafIcon(SelIcon);
                       renderer.setOpenIcon(SelIcon);
                       renderer.setClosedIcon(SelIcon);
                      //CheckNodes(node,renderer);
                        renderer.setBackgroundNonSelectionColor(frameColor);
                        tree.setCellRenderer(renderer);
                   }     my problem it is changed the entire tree, and i want to change specific nodes.
    if someone can help ill appreciate it.
    Gabi

    First Thanks for your answer.
    i was trying your suggestion also:
    public class IconRenderer extends DefaultTreeCellRenderer {
         private static final long serialVersionUID = 1L;
         Icon SelectedIcon;
         Icon NotSelectedIcon;
        public IconRenderer(Icon SelIcon,Icon NoTSelIcon) {
             SelectedIcon = SelIcon;
             NotSelectedIcon = NoTSelIcon;
        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 (leaf )
                  //setIcon(SelectedIcon);
                  this.setLeafIcon(SelectedIcon);
                  //this.setClosedIcon(SelectedIcon);
                  //this.setOpenIcon(SelectedIcon);
             return this;
    }in leaf nodes it was working fine but my problem here was with the parents.
    what i mean is :
    //this.setClosedIcon(SelectedIcon);
    //this.setOpenIcon(SelectedIcon);
    it marked the entire tree.
    thanks

  • IDE JTree Edit Bindings Icon property problem

    When I right click on a Jtree then Edit Bindings I cannot set the
    Icon Property
    Open Icon Property
    Closed Icon Property
    because only <default> is available.
    In my JPanel source code I have
    a declaration
    private ImageIcon tree_leaf=new ImageIcon(DEVIRE.class.getResource("/infopath/model/tree_leaf.png"));
    which works fine when I set the propery icon for a Jlabel.
    tree_leaf is displayed in the properties and can be selected and it works.
    How can I have it available for my JTree also, so that I can select it?
    Thank you in advance.
    Edited by: Georgekl on 23 Αυγ 2012 11:09 πμ

    for Delete button , copy the properties from working icon, then u can check and modify

  • How to change JTree's node icon manually?

    Hi, I'm currently making messenger application using awt and swing. I've searched in google and here(got this), but till now I haven't got solution.
    I want to change user's status icon in the buddy list if the user's status is changed(such like offline and online).
    But for now I want to try to change the icon directly by pressing a button first before thinking about listener for the status.
    I have this tree
         CustomCellRenderer rosterTreeRenderer = new CustomCellRenderer();
            DefaultMutableTreeNode rosterRoot = new DefaultMutableTreeNode("root");
            DefaultMutableTreeNode rosterGroup = null;
             rosterGroup = new DefaultMutableTreeNode("group 1");
             rosterRoot.add(rosterGroup);
             rosterGroup.add(new DefaultMutableTreeNode("abc"));
             rosterGroup.add(new DefaultMutableTreeNode("dce"));
             rosterGroup = new DefaultMutableTreeNode("group 2");
             rosterRoot.add(rosterGroup);
             rosterGroup.add(new DefaultMutableTreeNode("zzz"));
             rosterGroup.add(new DefaultMutableTreeNode("xxx"));
             rosterGroup.add(new DefaultMutableTreeNode("yyy"));
                rosterTree.setCellRenderer(rosterTreeRenderer);This is the cell renderer
    public class CustomCellRenderer extends DefaultTreeCellRenderer
         private static final long serialVersionUID = 1L;
         public CustomCellRenderer(){
            public Component getTreeCellRendererComponent( JTree tree, Object value,
                    boolean isSelected, boolean expanded, boolean leaf, int row,
                    boolean hasFocus )
              DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
              String     labelText = (String)node.getUserObject();
              if (isSelected)
                   System.out.println("selected");
                   this.setIcon(UIManager.getIcon("Tree.openIcon"));
                   //((DefaultTreeModel) tree.getModel()).nodeChanged(node);
            else if (leaf) {
                this.setIcon(UIManager.getIcon("Tree.leafIcon"));
              } else if (expanded) {
                   this.setIcon(null);
              } else {
                   this.setIcon(null);
              setText(labelText);
              return this;
        public Color getBackgroundNonSelectionColor() {
            return(null);
        public Color getBackground() {
            return(null);
    }Whenever I click the user, the user's icon change, the first phase of my testing is working.
    But after that I'm confused, how can I change the icon when I click a button at the roster frame. How to invoke and send parameter to change icon of specified user? I already read DefaultTreeCellRenderer javadoc but still haven't found clue to what I wanted, most of the methods noted with "Overridden for performance reasons".
    I've already searched for several days for this problem.. Hope someone can help me..
    Thanks

    Sorry I don't know how to delete my question from other forum. I also don't know about crossposting rule(I don't go into javaranch).
    I'm learning java by google and I'm really confused now, everyone I asked in real life doesn't know the solution. I'm doing a thesis making chat messenger, my lecturer also doesn't know how to solve my problem.
    Sorry I really don't know about the crosspoting rule. I only read the rule from each forum.
    Can someone help me please? :(

  • JTree shows expansion icon when node hasn't got children

    When first displayed, my JTree shows the expansion icon next to non-leaf nodes that do not have children.
    If I click on one of the expansion icon it disappears.
    How can I stop it displaying this in the first place.
    i.e. only display it when the node does have children.
    Thanks.

    I've had a look at the source code to see why this is happening
    javax.swing.plaf.basic.BasicTreeUI.paintExpandControl( ... )
    seems to be the source of this effect.
    It seems that in deciding whether to display expansion icons or not, it ignores whether or not a node has children up until the node is expanded for the first time.
    Surely it should consider whether there are children or not from the beginning.
    Is this a bug or is there reasoning behind this? If so what? Is there any workaround.

  • Root Handles (icons next to the node) not visible in TreeTable

    Hi i am using the example provided at www.java.sun.com for TreeTable. I have a tree table and when i add new nodes the data is visible in the table but the handles are not visible. I don't know how to resolve this problem. Below is my code where i'm adding new data.
    private void addNewNode(MyNode _root) {
    NodeInfo info = new NodeInfo();
    info.order= "Pending";
    info.qty = "200";
    info.price = "20,000";
    MyNode node = new MyNode(info);
    _root.add(node);
    tree.expandPath(new TreePath(_root.getPath()));

    Sorry my mistake...i was not returning TreeTableModel.class in getColumnClass() of my custom treetable model class.

  • Handles in JTree

    I've made an application connecting JTree nodes with JTable.
    When I first click on any cell in JTable and then on an JTree's node handle icon, I get an Exception: "java.lang.NullPointerException".
    But, when I first click on any cell in JTable and then on an JTree's node icon, everything is OK.
    What is goin' on?

    It happens that when I click on the handle icon of the ancestor node, an Exception occurs:
    java.lang.NullPointerException
    What is goin' on?
    Here is code snippet:
    void jTree1_valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
    Object birac = node.getUserObject();
    TreePath niz = (TreePath)jTree1.getLeadSelectionPath();
    jTextArea1.setText("");
    if (jTree1.isVisible(niz)) {
    if (node.isLeaf()) {
    My_Class bt = (My_Class)birac;
    String tabela1 = bt.getImeTabele();
    if (qds1.isOpen()) {
    try {
    qds1.close();
    } catch (Exception ex){} } //if

  • How to hide node handles (legs) from JTree nodes?

    Hello,
    Does anyone has an idea on how to not display JTree node handles (legs)?
    Thank you,
    Mindaugas

    You can do it with Java/Metal look and feel by doing this:
    JTree tree = new JTree(...);
    tree.setClientProperty("JTree.lineStyle", "None");
    For any other L&F, you will have to look at the UI code.
    Mitch Goldstein
    Author, Hardcore JFC

  • Setting background color for entire row of a JTree

    Hi,
    I'm trying to figure out how I can define the component for the entire width of a row of a JTree, not just a section after the arrow and indentation as by implementing a custom TreeCellRenderer.
    Any help would be appreciated.
    Thanks.

    The arrow is part of the UI. You may be able to do this by changing your UI settings The arrow is called a handle - you can set custom handle icons so that may be one approach. Someone else may have a better idea.

  • JTree Important Question

    Hi All,
    in the JTree, is there a way to change the root handles
    icons, not through the UIManager?
    I need the Changes to apply only for the one instance.
    is there a key for the putClientProperty method?
    a work around through the TreeCellRenderer is possible to
    write, but seems as a bit of a waste.
    10x in advance

    OK. If "handle" means the leftmost icons, that is the little circle with a dash pointing right or down under Java L&F, or either a + or a - in a square under Windows & Motif L&F, they are NOT part of the node display, and so they are not dealt with by a renderer. They are completely L&F dependant (while the leaf, open, closed are renderer dependant, and the L&F just provides some "hint" to the default renderer). It is possible that some L&F would display trees in a completely different way, without handles. So this is the tree UI delegate business, not the renderer's.
    You can set the icons for the application globally by setting UIManager properties Tree.expandedIcon and tree.collapsedIcon. (you knew that already). The good news is that the UI delegate seems to get these icons once sometime when it is created, rather than asking the UIManager each time it needs them.
    I tried
        Object previousExpanded = UIManager.put("Tree.expandedIcon", myExpandedIcon);
        Object previousCollapsed = UIManager.put("Tree.collapsedIcon", myCollapsedIcon);
        tree2 =  new JTree(data);
        UIManager.put("Tree.expandedIcon", previousExpanded);
        UIManager.put("Tree.collapsedIcon", previousCollapsed);.
    There is a tree1 created before and a tree3 afterwards, and it works fine, that is only tree2 get my icons.
    I tried to go a little farther, as I am not sure when the ui delegate might get reset. So I wrote a descendant of JTree to do the job in updateUI.
    public class CustomHandleTree extends JTree {
      // republish ancestor non-default constructors
      Icon expanded = null;
      public void setExpandedIcon(Icon anIcon) {
        expanded = anIcon;
        updateUI();
      // do the same for collapsed
      public void updateUI(){
        Object defaultExpanded = null;
        if (expanded != null)
          defaultExpanded = UIManager.put("Tree.expandedIcon", expanded);
         // collapsed stuff too
         super.updateUI();
        if(expanded != null)
          UIManager.put("Tree.expandedIcon", defaultExpanded);
         // collapsed stuff too
    }It works too, and I can even set the handles dynamically, when clicking on some button. It refreshes properly.
    The bad news is that I found that by trial and error, and have no single line of documentation to explain why it should work.
    Hope it helps nevertheless.
    Regards.

Maybe you are looking for

  • Getting my iTunes on my G5 to recognize my iPod

    Help!!!!!!!!! I made the mistake of hooking up my iPod to a wintel machine when I first bought it. Now, when I hook the iPod up to my G5, it doesn't seem to know that it's there. So the question is, how do I get iTunes on my Mac to recognize my iPod

  • Arabic characters is expands and repeated

    Hi, Please help in the following problem which is when we print standard reports including Arabic characters through SAP system the Arabic characters is expands and repeated, we already used device type u201C ARSWINu201D Moreover, we already applied

  • Sharpening treatment gets lost on export or library mode.

    I use Lightroom 5.5 with Windows 7, RAW-photos imported as dng. If I use sharpening and noise treating in the develop mode I loose my modulation if I go back to library mode. The same problem comes up on exporting the photo. So I can't profit of any

  • Fetching order data from Live Cache

    Hi The requirement is to fetch order data from liveCache for a given product based on ATP category . (AY - dependent demands and AU - order reservations) I want to get information about the dates and order quantity. I have identified following two FM

  • ALE-manual configuration of the customer distribution model

    DEAR EXPERTS how do i manually configure the customer distribution model