Tabbing through Jtree leaf nodes

Hi ,
I have a Jtree in which the each leaf node is a panel containing one label, one textfield I accomplished it by rendering concept. In this scenario, I want to tab through all the textfields in the tree. I am not able to do that, can anyone pls tell me whether this is possibe, if so pls help me.

If you don't want leaf nodes in the tree just don't add them. If you need it to be dynamic you will need to override the tree model and override the getChlid and getChildCount methods (maybe others) to get the tree model to not show a leaf in the parent node.

Similar Messages

  • Tabbing through a JTree

    Hi,
    I have searched all over for an example of how to tabb through a Jtree, but I have come up empty. I was wondering if anyone else has tried this?
    I actually only want to tab through the Jtree node's which are not collapsed. Seems simple, but I haven't been able to figure it out..
    thanks!

    Hi bsampieri,
    I dunno how i didn't see that...anyways thanks for your help...i got it working, well...almost...the wierd thing is...for some reason, it isn't listening to the "TAB" key...
    here is my code:
    jTree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK), "tabActionKey");
              jTree.getActionMap().put("tabActionKey", new AbstractAction() {
                   public void actionPerformed(ActionEvent e) {
                        int row = jTree.getRowForPath(jTree.getSelectionPath());
                        row++;
                        boolean found = false;
                        for(int x = row; x < jTree.getRowCount(); x++) {
                              if(jTree.isVisible(jTree.getPathForRow(x))) {
                               jTree.setSelectionPath(jTree.getPathForRow(x));
                                found = true;
                                break;
                        if(!found) {
                             for(int x = 0; x < row; x++) {
                              if(jTree.isVisible(jTree.getPathForRow(x))) {
                               jTree.setSelectionPath(jTree.getPathForRow(x));
                                break;
              });now the line
    jTree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK), "tabActionKey");should be:
    jTree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "tabActionKey");but when i put in 0 for a modifier, nothing happens when i hit the tab key. The api says to use a 0 if i dont want any modifiers, so i cant figure out why my action method is never called...
    any ideas?

  • Jtree Select node and change leafs icon problem

    Hi All,
    i create a tree and implement a TreeSelectionListener:
    my mission is whenever i select a node i need to change the icon of this node (for now.later i will have to find if it have childrens).
    import java.awt.Color;
    import java.awt.Component;
    import java.util.Enumeration;
    import java.util.NoSuchElementException;
    import java.util.Vector;
    import javax.swing.ImageIcon;
    import javax.swing.JTree;
    import javax.swing.event.TreeExpansionEvent;
    import javax.swing.event.TreeExpansionListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreeNode;
    import javax.swing.tree.TreePath;
    public class TreeView{
         DefaultMutableTreeNode top;
         JTree tree ;
         Color frameColor;
         public static ImageIcon NoTSelIcon;
         public static ImageIcon SelIcon;
        public static String[] name= new String[8];
         public TreeView(Color BackColor) {
              // TODO Auto-generated constructor stub
            top =  new DefaultMutableTreeNode("Diagnostics");
            this.frameColor=BackColor;
             SelIcon = createImageIcon("../Resource/Images/Select.gif");
             if (SelIcon == null)
                 System.err.println("Tutorial icon missing; using default.");
             NoTSelIcon = createImageIcon("../Resource/Images/NotSelc.gif");
               if (NoTSelIcon == null)
                 System.err.println("Tutorial icon missing; using default.");
         public Component createTreeComponents(){
                //Create the nodes.
                 createNodes(top);
            //Create a tree that allows one selection at a time.
            tree = new JTree(top);
            //TREE LISTENERS
            //Treeselction listener
            Handler hObject = new Handler();
            tree.addTreeSelectionListener(hObject);
           //Tree expand/collapse listener
            HandlerExpansionListener hObjectExpan = new HandlerExpansionListener();
            tree.addTreeExpansionListener(hObjectExpan);
    //       tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
            //set tree background
            tree.setBackground(frameColor);
             tree.setCellRenderer(new OverrideTreeCellRenderer(frameColor,SelIcon,NoTSelIcon));
            return tree;
          private void createNodes(DefaultMutableTreeNode top) {
                 DefaultMutableTreeNode category = null;
                 DefaultMutableTreeNode SubCategory = null;
                 DefaultMutableTreeNode SubCategoryBasee = null;
                 DefaultMutableTreeNode SubSubCategoryBasee = null;
                 category = new DefaultMutableTreeNode("Dfe");
                 top.add(category);
                 //Sub test visible
                 SubCategory = new DefaultMutableTreeNode("Test Visible");
                 category.add(SubCategory);
                 SubCategory.add(new DefaultMutableTreeNode("Son 1"));
                 SubCategory.add(new DefaultMutableTreeNode("Son 2"));
                 SubSubCategoryBasee = new DefaultMutableTreeNode("Test Base");
                 SubSubCategoryBasee.add(new DefaultMutableTreeNode("Grandson 1"));
                 SubSubCategoryBasee.add(new DefaultMutableTreeNode("Grandson 2"));
                 SubCategory.add(SubSubCategoryBasee);
          class Handler implements TreeSelectionListener {
                   public void valueChanged(TreeSelectionEvent arg0) {
                        // TODO Auto-generated method stub
                        System.out.println("treeSelect event ");
                        TreePath trph;
                        trph=arg0.getNewLeadSelectionPath();
                        int count=trph.getPathCount();
                        DefaultMutableTreeNode Selnode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                        String Name = (String)Selnode.getUserObject();
                        setSelected(Selnode,true);
                        int number_ofnodes=getNodeCountBelow((TreeModel)tree.getModel() , Selnode, false);
                        System.out.println("The Number of nodes under "+Name+"="+number_ofnodes);
                        tree.setCellRenderer(new IconRenderer(SelIcon,NoTSelIcon,frameColor));
          class HandlerExpansionListener implements TreeExpansionListener {
                   public void valueChanged(TreeSelectionEvent arg0) {
                        // TODO Auto-generated method stub
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode)  tree.getLastSelectedPathComponent();
                        if (node == null) return;
                      }     // The inner class
                   public void treeCollapsed(TreeExpansionEvent arg0) {
                        // TODO Auto-generated method stub
                        System.out.println("treeCollapsed event ");
                   public void treeExpanded(TreeExpansionEvent arg0) {
                        // TODO Auto-generated method stub
                        System.out.println("treeExpanded event ");
          /** Returns an ImageIcon, or null if the path was invalid. */
             protected static ImageIcon createImageIcon(String path) {
                  //ImageIcon imcon= new ImageIcon(path);
                  //return imcon;
                 java.net.URL imgURL = TreeView.class.getResource(path);
                 if (imgURL != null) {
                     return new ImageIcon(imgURL);
                 } else {
                     System.err.println("Couldn't find file: " + path);
                     return null;
             DefaultMutableTreeNode newnode;
             public void setSelected(DefaultMutableTreeNode Selnode ,boolean isSelected)
                    Enumeration Enchilds=Selnode.children();//ENUMRATE ALL CHILDS FOR THIS NODE
                 if (Enchilds != null)
                      while (Enchilds.hasMoreElements())
                           newnode=(DefaultMutableTreeNode)Enchilds.nextElement();
                           String NameSel = (String)newnode.getUserObject();
                           setSelected(newnode,isSelected);
             //GETTING THE TREE DEPTH
             public int getNodeCountBelow(TreeModel model, Object node, boolean includeInitialNode)
                 int n = includeInitialNode ? 1 : 0;
                 for (int i = 0; i < model.getChildCount(node); i ++)
                     n += getNodeCountBelow(model, model.getChild(node, i), true);
                 return n;
    import java.awt.Color;
    import java.awt.Component;
    import java.util.Enumeration;
    import java.util.NoSuchElementException;
    import javax.swing.Icon;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    public class IconRenderer extends DefaultTreeCellRenderer {
         private static final long serialVersionUID = 1L;
         Icon SelectedIcon;
         Icon NotSelectedIcon;
         Color BackgroundColor;
         boolean Selected=false;
         boolean Leaf=false;
         boolean IsItaChild=false;
         DefaultMutableTreeNode SelctedNode=null;
        public IconRenderer(Icon SelIcon,Icon NoTSelIcon,Color Bacground) {
             SelectedIcon = SelIcon;
             NotSelectedIcon = NoTSelIcon;
             BackgroundColor=Bacground;
             setBackgroundNonSelectionColor(BackgroundColor);
        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);
             Selected=sel;
             Leaf=leaf;
             DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
             String s2 = (String)node.getUserObject();
       return this;
    }my problem is :
    when i select a node the the method "getTreeCellRendererComponent"
    start to run on the entire tree from buttom to top and than from top to buttom.
    for me it waste of time because if has say 100 nodes it wont botthers me.
    but i have 20000 nodes and more its take a time.
    and for all this nodes i have to make compares.
    is there a way to force the DefaultTreeCellRenderer to not run the entire tree???
    Thanks

    You need to make sure that your TreeModel interprets your group nodes to be non-leaf nodes (one of the methods in the TreeModel interface is called isLeaf). If you are using a DefaultTreeModel with DefaultMutableTreeNode objects, you can use the askAllowsChildren property of DefaultTreeModel and the allowsChildren property of DefaultMutableTreeNode to control this. See the API for more details:
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultTreeModel.html
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultMutableTreeNode.html

  • Identifying leaf node in a JTree

    I have a single selection JTree which was constructed using DefaultTreeModel.
    asksAllowsChildren is set to 'true' in the tree model.
    The nodes in the model are constructed using DefaultMutableTreeNode and each
    node is marked if it allows children or not.
    How can I find if a selection is a leaf node or not.
    Thanks.

    Answering my own post, solution is:
    tree.getModel().isLeaf(tree.getSelectionPath().getLastPathComponent())
    where tree is the JTree object.

  • JTree: What do I do, when a leaf node isn't a leaf node?

    Hi All,
    I have a question about the JTree and leaf nodes.
    My requirements are:
    1) I have to have a tree which doesn't "fill" its contents at a certain level until the user expands a node
    2) As a result of #1, leaf nodes must have a "+" sign in front of them until the user tries to expand them at least once (if they are then empty, the "+" is removed in favor of the normal leaf icon.. if they have data, the "+" is replaced with a "-" and the data is filled in)..
    Currently, we have an implementation of this which adds a "dummy" node as a child of the leaf to force it to be a "non-leaf", until the user opens it, then the dummy object is removed and the real data (if any) is added..
    It works, but I'm wondering if there is a simpler way.
    Can anyone offer their opinion? Thanks a bunch.
    Dave

    What I do is to have some code that implements TreeWillExpandListener. What happens then is that when the user clicks on the doohickey to expand a node, my treeWillExpand(TreeExpansionEvent e) method is called. It examines the node that is about to expand and goes out to the database to load up its children. But to make this work, you have to have the doohickey available. This means that your nodes have to know whether their children have been loaded or not; this is the code I use to override the isLeaf() method for them:public boolean isLeaf() {
      return (childrenLoaded ? super.isLeaf() : false);
    }In this code "childrenLoaded" is a boolean instance variable that is initialized to false, then set to true via a property-setter method by the code that loads the node's children.
    That's a rough outline of what I did. Good luck!

  • Hide leaf nodes from jtree.

    Hi,
    How can I hide all nodes that are leafs on a tree? Basically, I'm creating a splitpane where on the left side I have a jtree where when a user clicks on a node on that jtree the leaf nodes will appear on the right split pane as icons.
    V

    Thanks for the reply...
    I wanted to include the leaf nodes in the tree model so that I won't need to work with two data structures--one with the leaf nodes, and another without the leaf nodes.
    So far, I think I resolved the problem. I created my own treemodel class where I pass a defaulttreemodel into it and created a modified getChildCount which will go into the defaulttreemodel and filter out the leaf nodes.
    It all seems to work pretty well.
    My problem now... How do I remove the stupid node icons? :)
    I just want the dashed lines to appear and nothing more....
    Thanks again...
    V

  • Hiding leaf node from jtree ?

    Hi,
    How can I hide all nodes that are leafs on a tree?
    Basically, I'm creating a splitpane where on the left side I have a jtree where when a user clicks on a node ( as a folder ) on that jtree the leaf nodes ( all the children nodes of the selected node ) will appear on the right split pane as icons.
    thanks in advance!

    Define your own renderer class eg:
    class NavTreeCellRenderer extends DefaultTreeCellRenderer {
    public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean sel,
    boolean expanded,
    boolean leaf,
    int row,
    boolean hasFocus)
    if(leaf == true){
    this.setPreferredSize(new Dimension(0, 0));
    super.getTreeCellRendererComponent(tree, dto.getNodeLabel(), sel, expanded, true, row, hasFocus);
    return this;
    Then set this rederer to ur tree
    NavTreeCellRenderer navTCR = new NavTreeCellRenderer();
    jTree.setCellRenderer(navTCR);
    And then ENJOY

  • Can I  Have JTable As Leaf Node Of A JTree?

    Hello there,
    I want to show jtable on a click of a node of a tree. I have seen Treetable utlility on SUN's site. But it not quite what i want.
    Does any one have any idea how to show JTable as a leaf node of a JTree.
    E.g There is a tree called Components. viz.
    Component
    |Node1
    |Node2
    On click of Node1 i should see Table1 and on click of Node2 i should see Table2.
    If anyone has any idea, can you explain how to do it Or some site name where help on this problem is available?
    Please mail back.
    Regards,
    Amit

    I used insertNodeInto() to inser a new node into the tree, and it can be displayed. But when I used addTreeSelectionListener() to click on the newly added node, it cann not reaspond the click. Following is my original addTreeSelectionListener(). You see, if you click a node of FARInfo, then it will open a form, and after user filled in and submit it, a new node will be added into the Jtree. If you click a node of FilledInfo, then the user should view the content of this new node. But now, it seems that the sencond click cannot work. Thanks for your help.
    tree.addTreeSelectionListener( new TreeSelectionListener()
    public void valueChanged(TreeSelectionEvent e4)
    DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
    (tree.getLastSelectedPathComponent ());
    Object nodeInfo = node.getUserObject();
    if (node.isLeaf())
    if ( nodeInfo instanceof FARInfo )
    FARInfo category = (FARInfo) nodeInfo;
    displayURL ( category.categoryURL );
    displayForm ( category.farFormName );
    if ( DEBUG )
    System.out.print ( category.categoryURL + ":\n" );
    else if ( nodeInfo instanceof FilledInfo )
    FilledInfo category2 = ( FilledInfo ) nodeInfo;
    displayFilledForm ( category2.num );
    }else
    return;

  • How to loop through a Data Node

    Hello,
    I have an Interactive Adobe form in WebDynpro Java project. 
    My data View tab includes the following structure:
    Bapi_test
    ValueHelpData
    CodeNumber
    CodeDescription
    Can someone please tell how I can loop through the “ValueHelpData” node, and get values for “CodeNumber” and “CodeDescription”
    The ValueHelpData node has the cardinality of  0..n. and has approximately 100 elements.
    Thanks. Your help is much appreciated.
    Rob.

    Hi Robby,
    You could try the following :
    int n = wdContext.nodeValueHelpData().size();
    for(int i = 0; i < n; i++)
       wdContext.nodeValueHelpData().moveTo(i);
       IPrivateMyDataView.IValueHelpDataElement ele = wdContext.currentValueHelpDataElement();
       String codenum = ele.getCodeNumber();
       String codedesc = ele.getCodeDescription();
       // Any other processing logic..//
    Hope this helps.

  • Right mouse button click in a JTree leaf

    Hi,
    how can I add a mouse listener in a JTree leaf? I have DOM nodes wrapped and a JTree model adapter to set up the JTree. I want the popup menu to show everytime the user clicks on a leaf, but not on the tree itself.
    Thanks!

    Really you just have to add your listener to the tree itself, then use the Point to find the corresponding TreePath (getPathForLocation), from which you'll easily find the node.

  • Leaf nodes in Flex cluster

    Hi,
      can somebody explain what leaf nodes in flex cluster are?from documentation I see that they are nodes which dont have access to storage and they communicate with hub nodes.
      Can they have oracle db instances?If so how is data transfered between hub and leaf nodes?Through interconnect?Doesn't it overload the interconnect?
    Thanks
    Sekar

    Sekar_BLUE4EVER wrote:
    Thanks Aman...Still confused about this...Consider the following scenario
    |       H1       |<------->  |        H2         |   <------> |       H3        |
    |                   |              |                      |               |                    |
    | L1  L2  L3  |              | L1   L2   L3   |               | L1  L 2 L3   |
    | _________|              |___________|               |__________|
    H depicts the hub nodes and L depict the leaf nodes.Assume each Hub node has 3 leaf nodes attached to them.
    Suppose L1 connected to H1 needs a block and modifies it and after sometime L1 connected to H2 needs the same block then it must follow the same 2 way/3 way grant as in normal cache fusion right?
    Does this actually increase the number of hops since the leaf nodes are not directly connected?
    Do we have any control over the leaf node to hub node mapping or is it all automatically managed?
    Thanks
    The blocks are going to be accessed, modified at the Hub nodes only AFAIK as the Hub nodes are considered as DB Nodes. The Leaf Nodes are going to be considered as the Application Nodes. That's the reason, it's better to set up the instances running on the Hub Nodes only rather than the Leaf Nodes. Even if the instance runs on a Leaf Node, the communication is between the Hub and Leaf node only and it won't do any harm as both the nodes-Hub and Leaf(and the other nodes in the Leaf group) would be talking to each other directly. There is no VIP required on the Leaf Nodes so the connections by the database users would be only on the Hub Nodes, I guess and that means, the block movement would remain essentially the same.
    The number of network hops are reduced as you won't be having a requirement to have too many Hub Nodes since each Hub node can connect to  64(?) Leaf Nodes. So essentially, in your case, you would need only 4 Interconnects (2 on one Hub Node and 1 each on the remaining two) for the private interconnect and just 3 network links for the storage for each Hub node.
    I am not sure that I understood the last question of yours.
    HTH
    Aman....

  • JTREE LEAF...PLS help////

    Hm.. i got a problem here.I have created i JTRee and the sub item which is the leaves.How to extend all the leaf node once the Jtree is open

    More dukes please :-)
    Look at the last line that read tree.expandPath(...)
    Note: sometimes DefaultMutableTreeNode.add() and remove() doesn't work well with the JTree so I've changed it to ((DefaultTableModel)jtree.getModel()).insertNodeInto(...) instead
        private void createNodes(JTree tree, DefaultMutableTreeNode top) {
            DefaultMutableTreeNode category = null;
            DefaultMutableTreeNode book = null;
            category = new DefaultMutableTreeNode("Chapter 1 ");
            ((DefaultTreeModel) tree.getModel()).insertNodeInto(category, top, top.getChildCount());
            book = new DefaultMutableTreeNode(new BookInfo("Lesson1-Overview",
            "AD1-1.html"));
            ((DefaultTreeModel) tree.getModel()).insertNodeInto(book, category, category.getChildCount());
            book = new DefaultMutableTreeNode(new BookInfo("Lesson2-Overview2",
            "AD1-2.html"));
            ((DefaultTreeModel) tree.getModel()).insertNodeInto(book, category, category.getChildCount());
            tree.expandPath(new TreePath(category.getPath()));
        }Don't forget the dukes :-)

  • Hierarchical Dimension with a leaf node w/o any parent

    Hi
    We have a Product Dimension with five levels L0, L1, L2, L3 and L4 (L0-lowestlevel, L4-HighestLevel).
    With one Hierarchy STANDARD as
    L4, NULL (Parent)
    L3, L4 (L4 is the parent of L3)
    L2, L3
    L1, L2
    L0, L1 (L1 is the parent of L0)
    The product dimension table has a record with values only for L0 and NULL values for other Levels..
    (ie) We have a requirement to create a value in L0 which will have no parents.. The rest of the values in L0 will have parents.
    Would like to know if this is possible in AW. This was possible in Express Server
    regards
    uma

    HI
    yes we have run into a big problem. We have created leaf nodes w/o parent in aw and created relational views of those. The measure views thus created has data for these dimension values but when accessed thru BI Beans (ie in the cross-tab if we have this measure) the data is not getting displayed for these dimension values (Which do not have parent)
    Any immediate response will be of greate help to us.

  • JTree hiding nodes

    Hi, I have an extrange problem. It seems that I'm unable to hide nodes with the treeRenderer and I don't know why.
    What I'm looking is to hiding leaf nodes, but leaving the nodes to expand. I don't want them remove because, they could become parents.
    My code is:
    import java.awt.Component;
    import javax.swing.JLabel;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultTreeCellRenderer;
    * @author Victor Manuel
    public class TableTreeRenderer extends DefaultTreeCellRenderer {
        public TablaTreeRenderer(){
            super();
        @Override
        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){
                setVisible(false);
            }else{
                setVisible(true);
            return this;
    NOTE the DefaultTreeRenderer extends a JLabel
    For some extrange reason it doesn't do what it has to do.
    Do you have any clues? please.
    regards,
    Victor Pereira
    Edited by: Darryl Burke -- split a long line of code that was breaking the page formatting in some browsers

    Darryl Burke wrote:
    (untested suggestion) Have you tried returning <tt>null</tt> from the <tt>get...RendererComponent(...)</tt> ?"suggestion" - some virus going round and having infested even the best? That's violating the renderers contract - NOOOO WAAAAYYY!
    and yeah, I'm aware that Swing api doc is severely lacking when it comes to null parameters and return values. And funnily, in a jtable, most ui's cope - mainly because cellRendererPane guards against both null container and null component AND the ui doesn't do much with the component besides passing it over to the rendererPane. Both list and tree fail throw on a null component because they do stuff (like sizing f.i.)
    So children... quickly forget what uncle Darryl told you - he was joking <g>
    CU
    Jeanette

  • How to know which leaf node i click and how to add a listener to each node?

    hi! hello to all members, i have a problem i know how to create a listener, but i dont know how to add a listener to each leaf node. here is the scenario i want to do, i have a JTree where theres a topic that you can select on each leaf node, so when the user click the specific topic it will open another JFrame,which i dont know how to do it also, that its! the next problem will be how do i know which leaf node i select, so that i can open a right topic before the JFrame will open?please, i am very need this to solve quickly as possible. thanks again to all members.

    What you have to do is to add a mouse listener on your JTree. Try something like this:
    tree.addMouseListener(new java.awt.event.MouseAdapter() {
             public void mouseReleased(MouseEvent e) {
                tree_mouseReleased(e);
    private void tree_mouseReleased(MouseEvent e) {
          TreePath selPath = tree.getSelectionPath();
          // Check If the click is the Right Click
          if (e.isPopupTrigger() == true) {
          // This is your right Click
           else {
                     // This is your Left Click
    }Your other problem: Set the userObject on nodes and on left click compare it with your object, if it matches, display the appropriate file. Alternatively, if your nodes are unique, you can match the names to open the file.
    Hope this Helps
    Regard
    Raheel

Maybe you are looking for