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.

Similar Messages

  • JTree and selected tree node

    I am allowing the user to search for a node in a JTree. I can find the node and progamatically expand and select the node. What I would like to do next is find the x,y coordinates of this selected node so I can use Robot to move the mouse to this node. I have hover help which is chock full of information in HTML describing this node, however, the user now is required to move the cursor to this selected node to see the hover help.
    Any help would be appreciated.

    Hi ,
    try this
    jlabel.setIcon( null ) ;

  • JTree get selected node/object values

    I wan to get all the selected nodes/objects values that I added to the JTree. How can I do this?
    Thanks.

    TreePath[] paths = tree.getSelectedPath();
    for(int i = 0; i < paths.length; i++){
    TreeNode node = (TreeNode)paths.getLastPathComponent();
    Object mySelectedObject = node.getUserObject();
    }Some minor Errors
    We have to use array paths[i] instead of paths
    The correct code as used by me is
            javax.swing.tree.TreePath[] paths = jTree1.getSelectionModel().getSelectionPaths();
            for(int i = 0; i < paths.length; i++){
                javax.swing.tree.TreeNode node = (javax.swing.tree.TreeNode)paths.getLastPathComponent();
    System.out.println(((javax.swing.tree.DefaultMutableTreeNode)node).getUserObject());
    CSJakharia

  • JDev 11g ADF Tree question on programmatically expand and select node

    I have two questions:
    1. I'm trying to automatically expand the first level tree nodes upon display of the tree. I tried to initialize a RowKeySetImpl object, and added ArrayList of first level nodes, and used setDisclosedRowKeys method. The tree nodes are not rendered properly. If I take the following code out, then the tree content is rendered properly. Here is my code. I have three nodes that I want to expand.
    ArrayList node1 = new ArrayList();
    node1.add(new Integer(0));
    ArrayList node2 = new ArrayList();
    node2.add(new Integer(1));
    ArrayList node3 = new ArrayList();
    node3.add(new Integer(2));
    RowKeySetImpl expand = new RowKeySetImpl();
    expand.add(node1);
    expand.add(node2);
    expand.add(node3);
    this.mainTree.setDisclosedRowKeys(expand);
    2. I have a Panel Splitter, on the first facet, it contains a tree, on the second facet it will be either 'task list', or 'edit task'. When user selects a task in the 'task list', I would like to display 'edit task', while highlight the selected 'task' node on the tree.
    In respond to the ActionListener of the command link (each command link corresponds to task):
    1. How do I determine the properly node in the tree?
    2. I constructed a RowKeySetImpl object and called setSelectedRowKeys. But I get messagebox saying "To display the webpage again, Internet Explorer needs to resend the information you've previously submitted." If I clicked Retry button, the message box just gets displayed again. If I cancel, the node I hardcoded does get selected properly.
    Please help me!
    Thanks,

    I believe this has already been fixed in our main code branch. I will enter a boxer bug and make sure that the fix gets backported to boxer so that it is available in the next boxer patch. If I remember correctly, the issue had to do with trees with a large display area (with default fetchSize of 25). The issue is that the tree has to make multiple round trips to the server in order fill the visible area (view port) on the page.
    Do either of the following workarounds work:
    1) Set a fetchSize attribute on the af:tree tag larger than 25 (<af:tree fetchSize="75">). You may want to increase the rangeSize in your pageDef file as well so that the model rangeSize matches the view fetchSize.
    2) Shrink the size of the tree on the page so that it does not have to make multiple round trips to the server to fill it's view port.

  • G4 and Xgrid node question

    I am currently running Logic on a G4 and thinking of the most effective way to expand my capabilities in order to work on film scoring. I was thinking of buying a mini mac and running and xgrid to utilize both computers for processing (with the node app in logic). I wanted to know if anyone has tried this or if it is compatible. If not, are there any other suggestions for ways to increase my processing power, or perhaps it isn't necessary? Thanks

    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.

  • DnD, JTree and inter-node placement

    Hi, there.
    Drag'n'Drop with JTrees has been discussed at length in this list. I have a DnD-capable application, and the user sees the nodes affected by the drag position as selected, so he knows where the drop will perform.
    Now I want to drop not only on other nodes, but also inbetween. You may imagine there is a red bar shown as soon as the drag is between tree nodes, and the result will be a new node inserted at exactly this position.
    Any ideas how to implement that?
    Hiran

    Still on the problem. Have some thoughts. But absolutely no idea whether this may work at all:
    Upon dragover, the cell affected is selected. That means I query getClosestPathForLocation() and then call setSelectionPath with the result. This works for nodes.
    Now I need to detect the inter-node placement. So the row must get calculated by myself, and if necessary, an extra node will be inserted into the tree. This extra node is rendered just as slim red line, so the user knows now he drops between nodes. Whatever happens now (drop performed, mouse moved elsewhere), the red line will be removed first.
    What do you think?

  • JTree  and Unique node values

    Hi,
    I am dynamically generating a JTree based on (non-unique) category descriptions found in a database. My trouble is that each node must also be identified by a (hidden) unique node id number.
    I have played around with myNode.setUserObject("uniqueNodeNumber");
    but that appears to change the title of the node itself.
    Any ideas or suggestions would be greatly appreciated.
    Thank you!

    class UserObject {
        private int id;
        private Object object;
        public UserObject (int id, Object object) {
            this.id = id;
            this.object = object;
        public int getID () {
            return id;
        public Object getObject () {
            return object;
        public String toString () {
            return object.toString ();
    }You can add this object to your tree.
    Kind regards,
      Levi

  • Focus Problem with JTree and Menus

    Hi all,
    I have a problem with focus when editing a JTree and selecting a menu. The problem occurs when the user single clicks on a node, invoking the countdown to edit. If the user quickly clicks on a menu item, the focus will go to the menu item, but then when the countdown finishes, the node now has keyboard focus.
    Below is code to reproduce the problem. Click on the node, hover for a little bit, then quickly click on the menu item.
    How would I go about fixing the problem? Thanks!
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    public class MyTree extends JTree {
         public MyTree(DefaultTreeModel treemodel) {
              setModel(treemodel);
              setRootVisible(true);
         public static void main(String[] args) {
              JFrame frame = new JFrame();
              JMenuBar bar = new JMenuBar();
              JMenu menu = new JMenu("Test");
              JMenuItem item = new JMenuItem("Item");
              menu.add(item);
              bar.add(menu);
              frame.setJMenuBar(bar);
              Container contentPane = frame.getContentPane();
              contentPane.setLayout(new GridLayout(1, 2));
              DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("Root");
              root1.add(new DefaultMutableTreeNode("Root2"));
              DefaultTreeModel model = new DefaultTreeModel(root1);
              MyTree tree1 = new MyTree(model);
              tree1.setEditable(true);
              tree1.setCellEditor(
                   new ComponentTreeCellEditor(
                        tree1,
                        new ComponentTreeCellRenderer()));
              tree1.setRowHeight(0);
              contentPane.add(tree1);
              frame.pack();
              frame.show();
    import java.awt.FlowLayout;
    import java.util.EventObject;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultTreeCellEditor;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreeCellEditor;
    import javax.swing.tree.TreeCellRenderer;
    public class ComponentTreeCellEditor
         extends DefaultTreeCellEditor
         implements TreeCellEditor {
         private String m_oldValue;
         private TreeCellRenderer m_renderer = null;
         private DefaultTreeModel m_model = null;
         private JPanel m_display = null;
         private JTextField m_field = null;
         private JTree m_tree = null;
         public ComponentTreeCellEditor(
              JTree tree,
              DefaultTreeCellRenderer renderer) {
              super(tree, renderer);
              m_renderer = renderer;
              m_model = (DefaultTreeModel) tree.getModel();
              m_tree = tree;
              m_display = new JPanel(new FlowLayout(FlowLayout.LEFT));
              m_field = new JTextField();
              m_display.add(new JLabel("My Label "));
              m_display.add(m_field);
         public java.awt.Component getTreeCellEditorComponent(
              JTree tree,
              Object value,
              boolean isSelected,
              boolean expanded,
              boolean leaf,
              int row) {
              m_field.setText(value.toString());
              return m_display;
         public Object getCellEditorValue() {
              return m_field.getText();
          * The edited cell should always be selected.
          * @param anEvent the event that fired this function.
          * @return true, always.
         public boolean shouldSelectCell(EventObject anEvent) {
              return true;
          * Only edit immediately if the event is null.
          * @param event the event being studied
          * @return true if event is null, false otherwise.
         protected boolean canEditImmediately(EventObject event) {
              return (event == null);
    }

    You can provide a cell renderer to the JTree to paint the checkBox.
    The following code checks or unchecks the box with each click also:
    _tree.setCellRenderer(new DefaultTreeCellRenderer()
      private JCheckBox checkBox = null;
      public Component getTreeCellRendererComponent(JTree tree,
                                                    Object value,
                                                    boolean selected,
                                                    boolean expanded,
                                                    boolean leaf,
                                                    int row,
                                                    boolean hasFocus)
        // Each node will be drawn as a check box
        if (checkBox == null)
          checkBox  = new JCheckBox();
          checkBox .setBackground(tree.getBackground());
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        checkBox.setText(node.getUserObject().toString());
        checkBox.setSelected(selected);
        return checkBox;
    });

  • How to select node in JTree without firing event?

    I have got standard situation. JTree in the left panel, and several edit boxes in right panel. Certainly, I have TreeSelectionListener, which on every tree node selection shows corresponding model values in edit boxes.
    I'd like to implement next logic:
    1. Something was changed in any edit box in right panel. Model has been changed.
    2. User clicks on different node in tree.
    3. Dialog "Message was not saved. Save?" with Yes/No/Cancel buttons are shown.
    Yes/No buttons are easy to handle.
    Question is about Cancel. I'd like on Cancel button left all edit boxes in their present state (do not restore values from saved model) and select back node, which wasn't saved.
    Problem is next. If I select node by setSelectionPath or smth like that, but... JTree fires event and my listener receives onTreeItemSelected back, which checks that node wasn't saved and ......
    Who does have any idea, or have done similar tasks? How can I select node and do not allow tree fire event this time?
    Thanks in advance.

    First, as soon as the model changes (when editing any
    combo box) some flag will be set. Now the logic which
    updates the combo boxes will do it only on a change of
    the current node and (this is new) if the flag wasn't
    set. You should have some flag anyway because somehow
    you must determine when to show the dialog, shouldn't
    you?Yes, I have got this logic implemented. But it's only the half :)
    I know exactly when my model has been changed, but if it was changed, i'd like to ask user what to do next - svae/loose changes/cancel
    And on cancel i'd like to select last edited tree node and do not get event from tree at that moment.
    >
    Second way, prevent selecting a new node if that flag
    has been set. You could do this by subclassing
    DefaultTreeSelectionModel and overriding some methods
    (setSelectionPath() et al).Ok. I'll investigate this.
    >
    MichaelThanks.

  • Change icon of a non selected node in JTree

    Hello
    I have a swing application contening a JTree.
    I'd like to know how to change the icon of a non selected node in my tree. I have the information about the node and its path. I try to change userObject information and the icon of the node with the setIcon of (DefaultMutableTreeNode), and reload the node (DefaultTreeModel) but it doesn't work.
    Do I have to set the change in my CellRenderer ?
    Any advice will be welcome
    Anne

    If you carefully look at the java tutorial tree, the change is made for all the leaf icon. What I want is to change the icon of a not selected node of my tree while I do other stuff with the other nodes like select, display their contents, drag them ...
    the operation of a not selected node is independant from the selection in the tree.
    So I still ask how to force a node to reload and take care of the change of his icon.
    Anne

  • JTree: How to get the currently selected node

    How do I get the currently selected node in JTree?
    getLastSelectedPathComponent() this method always return the last selected node and not the current one.
    Thanks in advance
    Sachin

    Use
    TreePath selectedPath = tree.getSelectionPath()If your tree allows multiple selections, use
    TreePath [] selectedPaths = tree.getSelectionPaths() this will return an array of all selected tree paths.
    Once you get the tree path, call the treePath.getLastPathComponent(). this should tell you the currently selected node.
    Hope this helps
    Sai Pullabhotla

  • Coloring selected nodes in JTree

    i am currently using setCellRenderer() for highlighting a selected node in my JTree.but when i select another node, the previously selected node loses its highlight.i would like to know of how i could make the highlight in the selected nodes remain persistent.

    I am currently using DISCONTIGUOUS_TREE_SELECTION only. my requirement is that i should be able to select any number of nodes.so i use this selection.
    but at the same time, my highlight in selected nodes should be like a toggle-state.
    if i select an already selected node ( say A), the highlight has to go away.
    and at the same time, th highlight on previously selected nodes (say B, C) should be persistent.
    am currently using TreeSelectionListener. and for rendering am using SetCellRenderer.
    in this scenario, how could my work be accomplished?

  • JTree cut and paste multiple child and ancestor nodes not function correct

    Hello i'm creating a filebrowser for multimedia files (SDK 1.22) such as .jpg .gif .au .wav. (using Swing, JTree, DefaultMutableTreeNode)
    The problem is I want to cut and paste single and multiple nodes (from current node til the last child "top-down") which partly functions:
    single nodes and multiple nodes with only one folder per hierarchy level;
    Not function:
    - multiple folders in the same level -> the former order gets lost
    - if there is a file (MMed. document) between folders (or after them) in the same level this file is put inside one of those
    I tried much easier functions to cope with this problem but every time I solve one the "steps" the next problem appears...
    The thing I don't want to do, is something like a LinkedList, Hashtable (to store the nodes)... because the MMed. filebrowser would need to much resources while browsing through a media library with (e.g.) thousands of files!
    If someone has any idea to solve this problem I would be very pleased!
    Thank you anyway by reading this ;)
    // part of the code, if you want more detailed info
    // @mail: [email protected]
    import java.util.Enumeration;
    import javax.swing.*;
    import javax.swing.tree.*;
    // var declaration
    static Enumeration en;
    static DefaultMutableTreeNode jTreeModel, lastCopiedNode, dmt, insert, insertParent, insertSameFolder;
    static String varCut;
    static int index= 0;
    static int counter = 0;
    /* cut function */
    if (actionCommand.equals ("cut"))
         // get the selected node (DefaultMutableTreeNode)
         lastCopiedNode = (DefaultMutableTreeNode)  selPath.getLastPathComponent ();
         // get the nodes in an Enumeration array (in top- down order)
         en = dmt.preorderEnumeration();
         // the way to make sure if it is a cut or a copied node
         varCut = "cut";
    /* paste function */
    if (actionCommand.equals ("paste"))
    // node is cut
    if (varCut == "cut")
    // is necessary for first time same folder case
    insertParent = dmt;
    // getting the nodes out of the array
    while(en.hasMoreElements())
         // cast the Object to DefaultMutableTreeNode
         // and get stored (next) node
         insert = (DefaultMutableTreeNode) en.nextElement();
    // check if the node is a catalogue when getRepresentation()
    // is a function of my node creating class (to know if it is
    // a folder or a file)
    if (insert.getRepresentation().equals("catalogue"))
         // check if a "folder" node is inserted
         if (index == 1)
              counter = 0;
              index = 0;
         System.out.println ("***index and counter reset***");
         // the node is in the same folder
         // check if the folder is in the same hierarchy level
         // -> in this case the insertParent has to remain
         if (insert.getLevel() == insertParent.getLevel())
              // this is necessary to get right parent folder
              insertSameFolder = (Knoten) insert.getParent();
              jTreeModel.insertNodeInto (insert, insertSameFolder, index);
    System.out.println (">>>sameFolderCASE- insert"+counter+"> " + String.valueOf(insert) +
              "\ninsertTest -> " + String.valueOf(insertTest)
              // set insertParent folder to the new createded one
              insertParent = insert;
              index ++;
         else // the node is a subfolder
              // insertNode
              jTreeModel.insertNodeInto (insert, insertParent, index);
              // set insertParent folder to the new createded one
              insertParent = insert;
    System.out.println (">>>subFolderCASE- insertParent"+counter+"> " + String.valueOf(insertParent) +
              "\ninsertParent -> " + String.valueOf(insertParent)
              index ++;
    else // the node is a file
         // insertNode
         jTreeModel.insertNodeInto (insert, insertParent, counter);
         System.out.println (">>>fileCASE insert "+counter+"> " + String.valueOf(insert) +
         "\ninsertParent -> " + String.valueOf(insertParent)
    counter ++;
    // reset index and counter for the next loop
    index = 0;
    counter = 0;
    // reset cut var
    varCut = null;
    // remove the node (automatically deletes subfolders and files)
    dmt.removeNodeFromParent (lastCopiedNode);
    // if node is a copied one
    if varCut == null)
         // insert copied node the same way as for cut one's
         // to make it possible to copy more than one node
    }

    You need to use a recursive copy method to do this. Here's a simple example of the copy. To do a cut you need to do a copy and then delete the source.
    import java.io.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.*;
    import java.util.*;
    public class CopyTree extends JFrame
         Container cp;
         JTree cTree;
         JScrollPane spTree;
         DefaultTreeModel cModel;
         CNode cRoot;
         JMenuBar menuBar = new JMenuBar();
         JMenu editMenu = new JMenu("Edit");
         JMenuItem copyItem = new JMenuItem("Copy");
         JMenuItem pasteItem = new JMenuItem("Paste");
         TreePath [] sourcePaths;
         TreePath [] destPaths;
         // =====================================================================
         // constructors and public methods
         CopyTree()
              super("Copy Tree Example");
              addWindowListener(new WindowAdapter()
              {public void windowClosing(WindowEvent evt){System.exit(0);}});
              // edit menu
              copyItem.addActionListener(new ActionListener()
              {public void actionPerformed(ActionEvent e){mnuCopy();}});
              editMenu.add(copyItem);
              pasteItem.addActionListener(new ActionListener()
              {public void actionPerformed(ActionEvent e){mnuPaste();}});
              editMenu.add(pasteItem);
              menuBar.add(editMenu);
              setJMenuBar(menuBar);
              buildModel();
              cp = getContentPane();
              cTree = new JTree(cModel);
              spTree = new JScrollPane(cTree);
              cp.add(spTree, BorderLayout.CENTER);
              pack();
         static public void main(String [] args)
              new CopyTree().show();
         // =====================================================================
         // private methods - User Interface
         private void mnuCopy()
              sourcePaths = cTree.getSelectionPaths();
         private void mnuPaste()
              TreePath sp, dp;
              CNode sn,dn;
              int i;
              destPaths = cTree.getSelectionPaths();
              if(1 == destPaths.length)
                   dp = destPaths[0];
                   for(i=0; i< sourcePaths.length;i++)
                        sp = sourcePaths;
                        if(sp.isDescendant(dp) || dp.isDescendant(sp))
                             JOptionPane.showMessageDialog
                                  (null, "source and destinations overlap","Paste", JOptionPane.ERROR_MESSAGE);
                             return;
                   dn = (CNode)dp.getLastPathComponent(); // the node we will append our source to
                   for(i=0; i< sourcePaths.length;i++)
                        sn = (CNode)sourcePaths[i].getLastPathComponent();
                        copyNode(sn,dn);
              else
                   JOptionPane.showMessageDialog
                        (null, "multiple destinations not allowed","Paste", JOptionPane.ERROR_MESSAGE);
         // recursive copy method
         private void copyNode(CNode sn, CNode dn)
              int i;
              CNode scn = new CNode(sn);      // make a copy of the node
              // insert it into the model
              cModel.insertNodeInto(scn,dn,dn.getChildCount());
              // copy its children
              for(i = 0; i<sn.getChildCount();i++)
                   copyNode((CNode)sn.getChildAt(i),scn);
         // ===================================================================
         // private methods - just a sample tree
         private void buildModel()
              int k = 0;
              cRoot = new CNode("root");
              cModel = new DefaultTreeModel(cRoot);
              CNode n1a = new CNode("n1a");
              cModel.insertNodeInto(n1a,cRoot,k++);
              CNode n1b = new CNode("n1b");
              cModel.insertNodeInto(n1b,cRoot,k++);
              CNode n1c = new CNode("n1c");
              cModel.insertNodeInto(n1c,cRoot,k++);
              CNode n1d = new CNode("n1d");
              cModel.insertNodeInto(n1d,cRoot,k++);
              k = 0;
              CNode n1a1 = new CNode("n1a1");
              cModel.insertNodeInto(n1a1,n1a,k++);
              CNode n1a2 = new CNode("n1a2");
              cModel.insertNodeInto(n1a2,n1a,k++);
              CNode n1a3 = new CNode("n1a3");
              cModel.insertNodeInto(n1a3,n1a,k++);
              CNode n1a4 = new CNode("n1a4");
              cModel.insertNodeInto(n1a4,n1a,k++);
              k = 0;
              CNode n1c1 = new CNode("n1c1");
              cModel.insertNodeInto(n1c1,n1c,k++);
              CNode n1c2 = new CNode("n1c2");
              cModel.insertNodeInto(n1c2,n1c,k++);
         // simple tree node with copy constructor
         class CNode extends DefaultMutableTreeNode
              private String name = "";
              // default constructor
              CNode(){this("");}
              // new constructor
              CNode(String n){super(n);}
              // copy constructor
              CNode(CNode c)
                   super(c.getName());
              public String getName(){return (String)getUserObject();}
              public String toString(){return  getName();}

  • Reloading JTree and expanding last selected rows

    I'm working on an applet that loads the nodes of a JTree from a database. When a node is selected, its data are displayed in a different panel. When a "reload" button is clicked, all the nodes in the JTree are removed except for the root node, and the JTree is recreated from the database. I'm try to get the new JTree to expand and select the rows that were selected before reloading, but I can't get this to work.
    Before removing the nodes, I save the currently selected row numbers using JTree.getSelectionRows(). After recreating the JTree, I re-select the previously selected rows and try to expand them:
    // tree is the name of the JTree.
    tree.setSelectionRows(selected);
    for (int i= 0; i < Array.getLength(selected); i++) {
                System.out.println("selected row: " + selected);
    tree.scrollRowToVisible(selected[i]);
    tree.updateUI();
    The previously selected rows do not automatically become visible in the resulting JTree. I also tried using tree.expandRow instead of scrollRowToVisible, with the same results.
    Any help would be appreciated!

    I think part of your problem is this...
    When you repopulate your tree, and only the root node is visible, getRowCount() will return 1. When you call expandRow(x) where x > 1, the result is basically a no-op (nothing happens). This is the correct behavior for these methods.
    In other words, I think you need to come up with a whole new algorithm.
    Let's assume you have a tree that looks like...
    Node 0
    |__________Node 0.0
    |               |__________Node 0.0.0
    |
    |__________Node 0.1
    |__________Node 0.2
                     |__________Node 0.2.0
                     |__________Node 0.2.1Try this...
    //save the current open/closed state of the tree and selection state
    Vector<Integer> selectedRows = new Vector<Integer>();
    Vector<Boolean> openClosed = new Vector<Boolean>(tree.getRowCount());
    openClosed.setSize(tree.getRowCount());
    for( int i = 0; i < tree.getRowCount(); ++i )
       if( tree.isExpanded(i) )
            openClosed.set(i,true);
       else
            openClosed.set(i,false);
       if( tree.isRowSelected(i) )
           selectedRows.add(i);
    //at this point we have all the needed state information
    // rebuild to tree from the database now
    // then do this
    int rowIndex = 0;
    while( rowIndex < tree.getRowCount() )
        if( openClosed.getElementAt(rowIndex).booleanValue() == true )
             tree.expandRow(rowIndex);
             // note that if a row gets expanded, getRowCount() will increase
        ++rowIndex;
    // at this point, your tree should be expanded exactly as it was before the reload
    int[] rows = new int[selectedRows.size()];
    int index = 0;
    for( Integer i: selectedRows )
       rows[index++] = i;
    tree.setSelectionRows(rows);
    // at this point the tree should have the same selection as before the reload.I hope this helps. Please reward me the Duke Dollars if it does. Not awarding the Duke Dollars kills the system (which is an honor system). A dead system hurts us all.
    P.S. I guess I never referenced the little tree I drew. Oh well. It looks great doesn't it?

  • How could i get the selected node in the JTree

    getLastSelectedPathComponent() �returns the parent node of currenr selected, so how could I get the selected node itself in the JTree
    I will appretiate for any help!

    i think you can get by....
    TreePath treePath = tree.getSelectionPath();
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();

Maybe you are looking for