JTree question

Hello !
I need some help creating TreeNodes in a tree.
first i will explain what i need.
I have a string that look like this:
d:\A dir\Another dir\file
That string is the complete path ro a file. that path is stored in a database.
I have no ideea how to create tree nodes that will look similar to:
+d
|- A dir
|--- Another dir
|-------file
Can anyone help me in this one please >
Thank you

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display

Similar Messages

  • JTree questions (1) Re-populating (2) Adding popups

    Hi all,
    I've just started to use a JTree in my application and I've encountered two
    problems. Sorry if these have been covered before - I've been Googling around
    for answers.
    I have a UI panel ...
    http://photos1.blogger.com/blogger/541/411/1600/TreeDemo.gif
    ... where a tree is populated with stock quotes from different
    sources. The user selects the stock and the tree should update to show the
    quotes for that stock. The quotes are read from a DB and I want the user to
    be able to delete from the DB using a pop-up on the leaf nodes.
    Problems
    (1) Re-populating
    The only way I've got this to work at this time is to create a new tree from scratch,
    based on the stock name, then put it into a new scroll panel, remove the old scroll
    panel from the container and add the new one. Not good is it? Nasty, nasty.
    I've read a lot about needing to interact with the tree model - is that right?
    (2) Adding popups
    I want to be able to delete items from the DB by doing a right-click on the leaf
    nodes and selecting "Delete" from the pop-up.
    I find that I can't add a mouse listener to the DefaultMutableTreeNode used to
    hold the leaf item. Should I subclass DefaultMutableTreeNode and implement some
    interface to do this?
    Any insights very much appreaciated.

    I know I'm asnwering my own question here - but I found the answer to teh re-population question while waiting.
    I now use the DynamicTree class that is included in the Sun tutorial here
    http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
    Specifically, here
    http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#DynamicTreeDemo
    I'd still welcome any suggestions on the pop-up menu question!

  • Confused -  jTree Questions

    Hi
    I have looked through the forum and other articles for answers to my questions but I am still confused.
    In my SWING application I have a JTree which has Node Type binding on it and displays nodes corresponding to Master and Detail views.
    What is the best way to capture the value of selected Node that is after a button is clicked?
    Secondly what is the best way to create an empty unbinded JTree (When i try to add a JTree the default nodes such as colors, sports etc show up)?
    What is the best way to populate this JTree? (The new JTree will be populated using the selected values from the first JTree after a button is clicked.)
    Any help will be apreciated. I have no clue where to proceed.
    Thanks in advance

    See:
    http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
    Frank

  • JTree questions

    Hi!
    What is the "best practise" on trees:
    Where should you put the methods for adding / deleting / Changes nodes? (In the node-class itself?)
    Where should you put listeners?
    How is a tree painted in the fastest way when you have many nodes?
    Thanks in advance!

    Hello ekren
    Where should you put the methods for adding / deleting / Changes nodes? (In the node-class itself?)Don't add or remove subnodes in the parentnode directly ! Use the methods from den DefaultTreeModel, or you will have problems with the GUI (JTree).
    Where should you put listeners?This depends, what you want to do. The TreeModel for model-Events, the JTree for user-events. So what do you want to do ?
    How is a tree painted in the fastest way when you have many nodes?When you have a custom TreeCellRenderer, then pay attention to your code, because the "getTreeCellRendererComponent"-method is called very often !
    Cache resources, if needed, and don't perform time-expensive tasks !
    Hope it helps
    Freddy

  • Easy JTree question

    I'm using something like the code below to "select" a node in my JTree. The node does get selected (an action is taken via the listener), however the node does not get selected in the graphical sense (i.e. the mouse selected node remains highlighted). How do I get the JTree to render the programatically selected node as the new selection?
      jTree.setExpandsSelectedPaths(true);
      jTree.setSelectionPath(jTreePath);
    ...Thanks

    you could try something like this if you had all your
    checkboxes named ina stuctured order I.E. checkbox_1, checkbox_2
    ect.
    countVar = 0;
    //if you had 10 checkboxes I.E. i<11
    add_btn.onPress=function(){
    for(i=1; i<11; i++){
    if(_root["checkbox_"+i].selected==true){
    countVar+10
    movieClip_mc.gotoAndPlay(countVar)
    }

  • Quick JTree Question

    Hello:
    Is there a way to set a treeNode that does not currently contain any childern with the closedFolderIcon instead of the leafIcon?
    Example:
    If I created a node that allows childern, but is currently empty, I would like to display this node with the closedFolderIcon. This would be similar to an empty directory within a file system.
    I plan on having a "Add Folder" and "Add Node" button to manipulate the tree.
    When I add a rootNode to the JTree I get a leafIcon instead of the closedFolderIcon. I only get the closedFolderIcon once I have added a childNode to the rootNode.
    Any ideas?
    Thanks,
    claude jones

    Please read my posting more carefully. My reply said it was based on my assumption that he wanted to affect the presentation of the nodes, not the definition of what it means to be a leaf node.
    In trees, a leaf node can not have children, ever. By his own posting, claude_jones said "a node that allows childern, but is currently empty". This is not a leaf node.
    He also says, that of this node, "I would like to display this node with the closedFolderIcon". The key word here is "display". Right there, that tells you wants to affect the presentation of the original data, not the original data.
    By the way, if you are still using DefaultTreeModel for your tougher tree problems, you might want to check out the JavaSoft tutorial on creating your own tree model classes. It makes a world of difference and makes your code a lot cleaner. If you're already familiar with this, I apologize.

  • General JTREE question ...

    I'm creating a data-driven JTree. Changes to the JTree will be reflected immediately in the database. Also, the JTree might contain as many as 10,000+ elements.
    Should I buffer this in some type of data structure (e.g. a hashtable ) or should I just write directly to-and-from the JTree? Will performance take a hit?
    Thanks.

    Depends where the changes are coming from. If they are being keyed by a human operator, then propagating those changes directly to the database should not be a problem. At least I don't find it a problem, and my larger JTrees contain over 10,000 nodes. At any rate, I would not design a complicated buffering system until I found there was a problem for it to solve.

  • Question with updating JTree Display

    Hello all,
    I have a question about how to change what a JTree is displaying. Currently I have classes called ElementTreeFrame, ElementTree, ElementTreeModel, and, of course, Element. The ElementTreeFrame is simply the frame where I store a ElementTree, it has 2 buttons (JButtons at the bottom) Add and Open. I am trying to Dynamically add an Element to my ElementTree.
    The Element class has a Vector in it, called childElements. When the user clicks Add, a new Element is added to the Vector. Let me explain a bit further. The element that is currently selected is returned via
    parentElem = (Element)parentPath.getLastPathComponent();
    Then a new Element is added by saying
    parent.childElements.insertElementAt(child, parent.getChildCount());
    which is code the Vector class provides. parent.getChildCount() simply returns the number of children present so that the child (the new Element being added) can be inserted in the right place.
    Here is my problem. When I first add an Element, it works fine. As a matter of fact, I can add as many Elements as I want and it will work fine. That is, until I manually expand the tree. When an Element is added, the tree doesn't expand. So I can add as many as I want, and it will work fine. The instant I coudlbe click on my Element to expand the tree, I can no longer insert a new Element. Technically, it gets inserted correctly, I know this because getChildCount remains correct, but the display is no longer correct. The line that extends from the folder to the new leaf is broken.
    I need to find a way to refresh my tree display. I am at a complete loss on how to do this. I have read about firingTreeNodesInserted or using reload, or a bunch of other methods but I am confused due to an overload of different methods.
    Can anyone tell me what my train of thought should be to do this? I'd just like a simple way for the nodes to be displayed correctly.
    Thank you so much,
    Nick

    Neverrmind, I figured it out. A call to to eTree.updateUI() handled it just nice.
    Thanks to myself, for being so hot.

  • JTree sizing question...

    Hello:
    I have a JTree for which each cell contains a button. I have written a renderer that renders the button, and I realize that I have to do some special stuff to capture a click on the button. My question is unrelated to all of that.
    The problem is that over time, the labels on my buttons change (and may become longer (wider)), but the tree size does not change. In fact, when I update the button label and the tree is re-rendered the rendering of the button gets "chopped off". I've put the tree in a scroll pane, but this doesn't help - the right side of some of the buttons get cut off to the original tree size. I've tried lots of different variations on setPreferredSize, calling repaint, etc, and am not having any luck. I've put together a demonstration of this behavior in a smallish application that I'm posting here, where I create a 2 node tree with buttons that read "Hi", then I change the button labels to "Goodbye" and re-render. You'll see that the button's are cut off about halfway through the button.
    In case its important - I'm running java version 1.5.0_07 on a 32-bit Linux box.
    Any help would be greatly appreciated. Thanks in advance!
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.awt.*;
    public class JTreeQuestion
      public static void main(String [] args)
        JTreeFrame f = new JTreeFrame();
        f.pack();
        f.setLocation(30, 30);
        //Draws buttons with "Hi" (short string)
        f.setVisible(true);
        ButtonNode.updateString("Goodbye");
        //Draws buttons with longer string, buttons get "cut off"
        f.repaint();
    class JTreeFrame extends JFrame
      JTree tree;
      JScrollPane treeView;
      public JTreeFrame()
        super("My Tree");
        DefaultMutableTreeNode root;
        root = new DefaultMutableTreeNode(new ButtonNode());
        root.add(new DefaultMutableTreeNode(new ButtonNode()));
        tree = new JTree(root);
        tree.setCellRenderer(new ButtonNodeRenderer());
        treeView = new JScrollPane(tree);
        add(treeView);
    class ButtonNode
      public static String str = "Hi";
      public static void updateString(String inStr)
      { str = inStr; }
      String getStr()
      { return str; }
    class ButtonNodeRenderer extends DefaultTreeCellRenderer
      public Component getTreeCellRendererComponent(JTree tree,
                              Object value, boolean sel, boolean expanded,
                              boolean leaf, int row, boolean hasFocus)
        Box theBox = new Box(BoxLayout.X_AXIS);
        super.getTreeCellRendererComponent(tree, value, sel, expanded,
                                           leaf, row, hasFocus);
        DefaultMutableTreeNode jtreeNode = (DefaultMutableTreeNode)value;
        theBox.add(new JButton(((ButtonNode)jtreeNode.getUserObject()).getStr()));
        return (theBox);
    }

    For those who are interested. The DefaultTreeModel has a method named nodeChanged() that tells the tree model that a specific node has changed, and the model re-interprets the cell causing its sizse to change as necessary, so that the full button is rendered now.
    Basically what I did was instead of calling repain, I call a method that I wrote that loops through all the tree nodes, indicates they have changed, then repaint's, and it all works out. My trees are relatively small, so this is fine, but if others face the same problem, you'll probably want to selectively indicate which nodes have changed so the tree model doesn't have to do more work than necessary.

  • JTree image question

    I have a question about using images in a JTree.
    I have like 2 parent nodes who both have a lot of child nodes now i know how to get an image for every node but how do i get 1 image for 1 parent with all his children and another image for the other parent with his children.

    It is a programming problem because i dont know how to give echt DefaultMutableTreeNode his own picture. You should think of it like msn when you log in your contacts are in a Tree and your offline contacts have a red icon and online contacts have green one. I need to to the same for my program(chat program). But i can't figure out how but i know how to give alle the DefaultMutableTreeNode's a picture but i cant give individual one's a picture.
    I hope i cleared things up :)

  • JTree#expandRow() Question

    JTree tree;
    //etc...
    1) /** Expand the tree */
    for (int i=0; i<tree.getRowCount(); i++)
         tree.expandRow(i);
    and tree.getRowCount() = 6
    2) /** Expand the tree */
    for (int i=0; i<6; i++)
         tree.expandRow(i);
    My tree has 6 rows, and each row has its own sub-tree hierarchy.
    I wonder why the first case will expand the whole tree, which includes
    the subtrees of each row.
    But in second case, it only expands the first 6 rows in the tree.
    So basically the question is why (1) and (2) have different tree view outputs??

    Well, after you expand row 1, the row count isn't 6 anymore, is it? It's something larger than that, depending on how many children the first node has.

  • JTree Updating question

    Hi there. I have two questions.
    I have a data structure that is being displayed in a JTree, so I made a Model and it works great. The data structure can be updated from non-gui interaction. The nodes don't change position or anything, its just the data that is displayed is changed. I handle it currently by calling a function in the model I made called nodesChanged(), which basically does the following:
    public void nodesChanged()
        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this, new Object[] {rootItem});
        for (int i = 0; i < len; i++) {
          ( (TreeModelListener) treeModelListeners.elementAt(i)).treeNodesChanged(e);
      }It does actually work, the changes are reflected in the JTree, but it seems a little expensive, and the updates to the actual data model could come at about 150 per second. To deal with that now, I just use a timer class that updates it about every 3/10's of a second, which does a pretty good job, but is there a more elegant way to do this? Something where the node is an observer of the data in the data structure (which are Observable objects)?
    The second question I have is when I do the above, sometimes the display name of the node will be larger than the value it had previously, but the Textbox (if that's what it is) doesn't grow, so I get a ... at the end.
    For example, if I have "Run" and it changes to "Stopped", it will show up as "Sto...".
    Any help would be great. TIA.

    well, you can start by making the node you put in the tree model event the lowest node in the tree that needs updating, instead of root.
    I don't know about the timer thing, cuz as far as I know, the listener will invoke code that will refresh the renderers, as opposed to painting where repaint calls can be merged into one. If you aren't getting that many updates all the time, you could implement something where the listener fires to an intermediate listener which will fire the info to the tree after a slight delay. That way if you get multiple updates, you can effectively ignore lots of them.
    The ... thing, I thought that treeNodesChanged was the appropriate method, although maybe it has to be for the specific node, not root.

  • Swing Gurus! A question abt JTREE??

    i have a jtree with nodes in it . when the user selects a node,
    a dialog box appears asking confirmation whether to he wants to exit the current function.
    If he clicks on "yes" it will do the necessary operation.
    Note:This dialog box doesn't appear when the user selects a node for the first time.
    Problem: When the user selects a node after selecting a node earlier,
    it is highlighted with blue color and then a confirmation dialog pops up.
    if he clicks on "NO" i want to revert back to the old node which was earlier
    selected and highlight the node.
    iam able to get the previous node but iam not able to highlight it.
    how to highlight the previous one?
    tks in advance!

    You can try
    tree.setSelectionRow(int row);
    All you have to do is keep track of the previous selected node index and when the user selects "No", use the method to re-select the previous node.
    I haven't tested this, but I am sure it will work...
    Good luck!

  • JTree and selected node question

    Hello
    Is it possible to convert the selected node to an int?
    I guess I'm looking for something llike this.
    int nodeNumber = node.getSelectedIndex();
    There is nothing like that in the API, and it would help greatly if I could find a way do this.
    Thanks
    Have a good holiday
    Jim

    From the API for JTree
    public int getMinSelectionRow()
    Gets the first selected row.
    Returns:
    an integer designating the first selected row, where 0 is the first row in the display
    But I think this is based on how many rows are displayed at the present time and might change if the tree is opened above it.

  • JTree custom cell renderer question

    When using a custom cell renderer is their anyway to tell the offset of the current node?
    For example if you have a tree with the parent node "Parent" that has a few children nodes it will look something like this:
    Parent
    |----Child 1
    |----Child 2
    |----Child 3
    So the parent would have an offset of zero. I want to know how to get the childrens offset? If this is possible?
    What I am trying to accomplish:
    I have a JTree as a scrollpane rowHeader. While the main body of the scrollpane is a JTable. I need the tree's nodes to fill the entire width so that it looks like part of the table. I figure the row header width - current node offset should be the width that I will need each node to be. Any help is welcome. Thanks.

    the renderer tells you the row, the tree can get you a treepath for a row
    TreePath getPathForRow(int row)
    That can tell you the depth of the node.

Maybe you are looking for

  • Follow up on copying unested sequences

    I've been coping sequences as some of you described by using "Select All" and copying and pasting them into new timelines to keep from getting nested sequences, but it seems when I Command-drag marked sections of these copied sequences into a new seq

  • Personalized txt message alerts based on who is sending it?

    Is there anyway on the Iphone to set different text message alerts based on who is sending the text? I am a sysadmin and currently I have our servers sending txt message alerts to my corp phone. I'd like set it to send to my iphone with the caveat of

  • How to get 100% view centered when opening document?

    I work with my documents at 100%. Whenever I open a document the pages are placed rather randomly on my screen... Is there a way to have the pages centered by default? I have to press cmd-alt-0 (to center the spread) and then cmd-1 (to g to 100%) eve

  • How to reset my ipad

    how to reset our iPad it is jammed

  • Design issues

    Hello there, I have 4 classes. 1. User 2. Client 3. UserManager 4. ClientManager User is an abstract class and Client inherits from User. Therefore, a Client is a User. UserManager is solely responsible for User objects management. It handles adding,