Refreshing a JTree without collapsing

I have a problem. I've seen a few topics on this issue but none helped.
I try to refresh a tree and preserve the same structure (with the expanded nodes) after refreshing it. In order to do that i save all the expanded paths from the tree before refreshing it and trying to expand them again after refreshing (using expandPath() function). During refreshing all the nodes (but the root) are removed, then added again.
Could anyone tell me what am i doing wrong?
Thank you in advance,
Claudia

The nodes class extends DefaultMutableTreeNode, yes. Yes, the equal method is called.
Here is the refresh method of the tree.
  public void refresh() {
    ArrayList expandedPaths = getExpandedPaths();
    ((MyTreeModel)getModel()).refresh(); // this method removes the nodes of the tree and readding them
    expandPaths(expandedPaths);
  public ArrayList getExpandedPaths() {
    ArrayList expandedPaths = new ArrayList();
    Enumeration enum = getExpandedDescendants(getPathForRow(0));
    while(enum != null && enum.hasMoreElements()) {
      TreePath tp = (TreePath)enum.nextElement();
      expandedPaths.add(tp);
    return expandedPaths;
  public void expandPaths(ArrayList pathlist) {
    for(int i = 0; i < pathlist.size(); i++) {
      expandPath((TreePath)pathlist.get(i));
  }

Similar Messages

  • Add a node to JTree== view change without collapse

    I added a new node in a NodeTree structure.
    And I want to view changes in JTree, without collapse.
    What can I do?

    You need to add the clustered resource into vmm, and not the individual file servers. Its the clustered resource that moves between nodes of the cluster.

  • JTree refresh without collapsing my nodes

    Hello,
    I have a JTree using a custom TreeModel. I've also extended the TreeNode.
    What is the most elegant method to refresh the tree or only a node without
    collapsing the currently expanded nodes?
    Please note that there are multiple types of TreeNodes each representing
    a different object to display in the tree and contains an object that represents its
    value. Also the node has a method createChildren() to determine and create
    the child TreeNodes. Children are loaded from a business delegate. I need the
    tree to re-query the model (maybe for only one node) about the data and redisplay
    the changes without affecting the collapsed/expanded state of the nodes that
    are not changed (not supposed to refresh).
    My model actually asks the node for data like getChildsCount(), isLeaf() etc, etc.
    I'd like to say:
    myCustomNodeInstance.refresh();
    or
    myCustomTreeModelInstance.refresh(myCustomNodeInstance);
    and all nodes that are not children of myCustomNodeInstance to preserve
    their state.
    Thanx

    Did you try to call repaint() method on JTree ?

  • Hiding a intermediate node in a Jtree without hiding leaf nodes

    Hi who can help me out of this problem. What my prob is dat i m interrested in hiding few intermediate nodes in JTree without hiding their children.
    Thanks
    Rakesh

    but how do you then expand to see the children? once the node is hidden, can you by any means expand/collapse it to view/hide the child leaves?

  • Refreshing the jtree in a jpanel

    hi,
    i created a jtree of files in a certain folder... if some files changed in my folder, how can i refresh the jtree in my jpanel...
    thanks!!!

    Hi,
    look here:
    http://forum.java.sun.com/thread.jspa?threadID=478780&messageID=2226565
    L.P.

  • How to create JTree without root node

    Hello;
    I like to create a JTree without root node?
    Any help?
    Thanks!
    --tony                                                                                                                                                                                   

    javadocs JTree,
    setRootVisible
    public void setRootVisible(boolean rootVisible)
    Determines whether or not the root node from the TreeModel is visible.
    Parameters:
    rootVisible - true if the root node of the tree is to be displayedSee Also:
    rootVisible

  • Problem in refreshing a JTree

    Hi,
    Can anyone tell how to refresh a JTree(javax.swing.Jtree) used for displaying file system(both local and remote).
    I'm getting a problem if a file/directoy is added to the file system, after my Applet is loaded. Actually, i have used DefaultTreeModel's reload() method and calling it inside SwingUtilities.invokeLater() using Thread, but its not working.
    Plz help me out.
    Thanks

    Hi everybody,
    CAN ANYONE HELP ME OUT?????????

  • Multiple Selection on JTree without holding down [CTRL] or [SHIFT]

    I need an example about how make multiple selection on JTree without holding down [CTRL] or [SHIFT].
    my JTree contains JCheckBox in any nodes, but I can't select two or more checkBox in time without holding down [CRTL] or [SHIFT].
    thanks for help.
    Jose A.

    I did this a few years ago so my newbiness is going to show through a bit, but I'm too lazy to update it.import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.util.*;
    public class Test3 extends JFrame {
      JTree jt = new JTree();
      MultiTreeSelectionModel mtsm = new MultiTreeSelectionModel(jt);
      JCheckBox multiCheck = new JCheckBox("Multi"),
          branchCheck = new JCheckBox("Branch");
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel content = (JPanel)getContentPane();
        multiCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mtsm.setMultiSelect(multiCheck.isSelected());
        branchCheck.setText("Branch");
        branchCheck.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
                mtsm.setBranchSelect(branchCheck.isSelected());
        JPanel jp = new JPanel();
        jp.add(multiCheck);
        jp.add(branchCheck);
        content.add(jp, BorderLayout.NORTH);
        ActionListener specialKeyListener =
            new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    MultiTreeSelectionModel.keyModifiers = evt.getModifiers();
        KeyStroke keyStroke;
        for (int i = 0; i < keys.length; i++) {
            keyStroke = KeyStroke.getKeyStroke(keys[0], 0, true);
    content.registerKeyboardAction(specialKeyListener, keyStroke,
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    keyStroke = KeyStroke.getKeyStroke(keys[i][0], keys[i][1], false);
    content.registerKeyboardAction(specialKeyListener, keyStroke,
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jt.setSelectionModel(mtsm);
    content.add(new JScrollPane(jt), BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);
    public static void main(String[] args) { new Test3(); }
    private static int[][] keys = {{KeyEvent.VK_CONTROL, ActionEvent.CTRL_MASK},
    {KeyEvent.VK_SHIFT, ActionEvent.SHIFT_MASK},
    {KeyEvent.VK_ALT, ActionEvent.ALT_MASK}};
    class MultiTreeSelectionModel extends DefaultTreeSelectionModel {
    static int keyModifiers;
    private boolean branchSelect, multiSelect;
    private JTree tree;
    private TreePath[] savePaths;
    MultiTreeSelectionModel(JTree Tree) {
    tree = Tree;
    setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    private boolean isSelected(TreePath Path) {
    return tree.isPathSelected(Path) && (keyModifiers & KeyEvent.SHIFT_MASK) == 0;
    private boolean branchSelect() {
    return branchSelect || ((keyModifiers & KeyEvent.ALT_MASK) != 0);
    public void addSelectionPaths(TreePath[] paths) {
    if (branchSelect()) paths = getAllPaths(paths);
    super.addSelectionPaths(paths);
    public void removeSelectionPaths(TreePath[] paths) {
    if (branchSelect()) paths = getAllPaths(paths);
    super.removeSelectionPaths(paths);
    public void setSelectionPaths(TreePath[] paths) {
    if (branchSelect()) {
    paths = getAllPaths(paths);
    if (paths != null && paths.length > 0 && isSelected(paths[0])) {
    super.removeSelectionPaths(paths);
    } else if (multiSelect) super.addSelectionPaths(paths);
    else super.setSelectionPaths(paths);
    protected TreePath[] getAllPaths(TreePath[] paths) {
    if (paths == null || paths.length == 0) {
    return paths;
    Vector vector = new Vector();
    DefaultMutableTreeNode treeNode, thisNode;
    for (int i = 0; i < paths.length; i++) {
    if (paths[i] != null) {
    thisNode = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
    Enumeration enumeration = thisNode.preorderEnumeration();
    while (enumeration.hasMoreElements()) {
    // add all descendants to vector
    treeNode = (DefaultMutableTreeNode) enumeration.nextElement();
    TreePath treePath = new TreePath(treeNode.getPath());
    vector.add(treePath);
    int i = vector.size();
    TreePath[] allpaths = new TreePath[i];
    for (int j = 0; j < i; j++) {
    allpaths[j] = (TreePath) vector.elementAt(j);
    return allpaths;
    protected void setMultiSelect(boolean b) { multiSelect = b; }
    protected boolean isMultiSelect() { return multiSelect; }
    protected void setBranchSelect(boolean b) { branchSelect = b; }
    protected boolean isBranchSelect() { return branchSelect; }
    protected void savePaths(TreePath Path) {
    TreePath[] tmpPaths = getSelectionPaths();
    if (tmpPaths == null) {
    savePaths = null;
    } else {
    int cnt = 0;
    for (int i = 0; i < tmpPaths.length; i++) {
    if (tmpPaths[i].isDescendant(Path)) {
    cnt++;
    savePaths = new TreePath[cnt];
    cnt = 0;
    for (int i = 0; i < tmpPaths.length; i++) {
    if (tmpPaths[i].equals(Path) || tmpPaths[i].isDescendant(Path)) {
    savePaths[cnt++] = tmpPaths[i];
    protected void restorePaths() {
    if (savePaths != null) {
    final DefaultTreeSelectionModel foo = this;
    SwingUtilities.invokeLater(
    new Runnable() {
    public void run() {
    foo.setSelectionPaths(savePaths);

  • Get node jtree without click

    hi all,
    I want get node from jtree without click, just with mouse in node.
    I look in google but dont find nothing...
    I try use listener but MousePressed dont help-me.
    thanks

    jTree1 = new JTree1(treeModel);Sorry, now
    jTree1 = new JTree(treeModel);
    MouseMotionListener mm = new MouseMotionListener(){
          public void mouseMoved(MouseEvent e) {
                System.out.println("moved "+e.getX()+" "+e.getY());
                 int selRow = jTree1.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = jTree1.getPathForLocation(e.getX(), e.getY());
                if(selRow != -1) {
                                    System.out.println("mouse moved..."+selPath);
        public void mouseDragged(MouseEvent e) {
                   System.out.println("dragged "+e.getX()+" "+e.getY());
    jTree1.addMouseMotionListener(mm);
    jScrollPane1.setViewportView(jTree1);

  • Is there any way to display JTree without using applet

    Hi,
    is there any way to display JTree without using applet . Can we display the JTree in a JSP page.
    With Regards,
    Sheema.

    Not a JTree, per se. But there are Javascript solutions out there and there are JSP tag library solutions which use a JTree on the server side to hold and maintain the data and expanded nodes.
    This is one that I've used before, it's pretty good:
    http://www.jenkov.dk/treetag/introduction.tmpl

  • How to reboot or restart or refresh curve 8520 without removing and refixing battery ?

    How to reboot or restart or refresh curve 8520 without removing and refixing battery ?
    Solved!
    Go to Solution.

    Soft reset:
    http://btsc.webapps.blackberry.com/btsc/viewdocument.do;jsessionid=64C8797ED4CAB08C34C13D91377C1704?...
    Please click the Thumbs Up icon if this comment has helped you!
    If your issue is resolved, please click the solution button on the resolution!
    Every BlackBerry should have BlackBerry Protect, get it now! | Follow me on Twitter | Bring Back BBM Music!

  • JTree - sorting without collapsing

    i have a JTree which represents data stored in my data layer. if this data changes, i do not only have to update the view part in the tree but also resort it to certain criteria. as far as well, this works fair enough, my problem is, that with every resorting, i remove/add children in the JTree which leads to structure changes. notifying them to the JTree results in the collapsing of all the branches under the event source. how can i prevent that? i'd like to just resort the nodes on one level and leave the collapse/expand of the nodes as it was.
    tnx for the help!

    Thanks for your advice, i tried that but it doesn't seem to work with my code, the effect stays the same. what do i have to change?
    Note:
    snippetComparator cmp = new MyComparator();
    SortedSet sorted = new TreeSet(cmp);
    sorted.addAll(this.children);
    int i = 0;
    for (Iterator it = sorted.iterator(); it.hasNext(); i++) {
    DefaultMutableTreeNode element = (DefaultMutableTreeNode)it.next();
    this.treeModel.removeNodeFromParent(element);
    this.treeModel.insertNodeInto(element, this, i);
    <<snippet
    this is all i do, no manual creation of any TreeEvents.

  • JTree without expand/collapse icons

    I want to be able to hide or change the expand/collapse icon of a specific node, not the entire Jtree.
    Is there any way to do that?
    Tahnx

    I want to be able to hide or change the expand/collapse icon of a specific node, not the entire Jtree.
    Is there any way to do that?
    Tahnx

  • Updating a JTree without application restart

    I would like to update the JTree in my application without restarting the application. The JTree elements come from a file, therefore, I update the file and want the JTree to be refreshed! I understand that this is done with swing and model and already do so eg: I update combo boxes and these automatically reflect the changes. I use the code below to update the boxes:-
    public void updateBoxes()
    DefaultComboBoxModel newComboModel = new DefaultComboBoxModel(getItems());
    myBox.setModel(newComboModel);
    I would basically like to do exactly the same using another model for a JTree.  Is this possible?  Also, as the JTree has actually been added to a scroll panel, so does this affect things?  Hopefully I can use a simple method like the one provided above for updaing a combo box.
    Thanks for any assistance

    * TestTree.java
    package com.test;
    import javax.swing.tree.DefaultMutableTreeNode;
    public class TestTree extends javax.swing.JFrame {
        private javax.swing.JScrollPane mainScrollPane;
        private javax.swing.JButton btnChooseThree;
        private javax.swing.JTree mainTree;
        private javax.swing.JButton btnChooseOne;
        private javax.swing.JPanel buttonPanel;
        private javax.swing.JButton btnChooseTwo;
        public TestTree() {
            java.awt.GridBagConstraints gridBagConstraints;
            mainScrollPane = new javax.swing.JScrollPane();
            mainTree = new javax.swing.JTree();
            buttonPanel = new javax.swing.JPanel();
            btnChooseOne = new javax.swing.JButton();
            btnChooseTwo = new javax.swing.JButton();
            btnChooseThree = new javax.swing.JButton();
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            mainScrollPane.setViewportView(mainTree);
            getContentPane().add(mainScrollPane, java.awt.BorderLayout.CENTER);
            buttonPanel.setLayout(new java.awt.GridBagLayout());
            btnChooseOne.setText("One");
            btnChooseOne.setMaximumSize(new java.awt.Dimension(70, 27));
            btnChooseOne.setMinimumSize(new java.awt.Dimension(70, 27));
            btnChooseOne.setPreferredSize(new java.awt.Dimension(70, 27));
            btnChooseOne.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnChooseOneActionPerformed(evt);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
            buttonPanel.add(btnChooseOne, gridBagConstraints);
            btnChooseTwo.setText("Two");
            btnChooseTwo.setMaximumSize(new java.awt.Dimension(70, 27));
            btnChooseTwo.setMinimumSize(new java.awt.Dimension(70, 27));
            btnChooseTwo.setPreferredSize(new java.awt.Dimension(70, 27));
            btnChooseTwo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnChooseTwoActionPerformed(evt);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
            buttonPanel.add(btnChooseTwo, gridBagConstraints);
            btnChooseThree.setText("Three");
            btnChooseThree.setMaximumSize(new java.awt.Dimension(70, 27));
            btnChooseThree.setMinimumSize(new java.awt.Dimension(70, 27));
            btnChooseThree.setPreferredSize(new java.awt.Dimension(70, 27));
            btnChooseThree.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnChooseThreeActionPerformed(evt);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
            buttonPanel.add(btnChooseThree, gridBagConstraints);
            getContentPane().add(buttonPanel, java.awt.BorderLayout.NORTH);
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Three");
            rootNode.add(new DefaultMutableTreeNode("One"));
            rootNode.add(new DefaultMutableTreeNode("Two"));
            rootNode.add(new DefaultMutableTreeNode("Three"));
            mainTree.setModel(new javax.swing.tree.DefaultTreeModel(rootNode));
            pack();
        private void btnChooseThreeActionPerformed(java.awt.event.ActionEvent evt) {
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Three");
            rootNode.add(new DefaultMutableTreeNode("One"));
            rootNode.add(new DefaultMutableTreeNode("Two"));
            rootNode.add(new DefaultMutableTreeNode("Three"));
            mainTree.setModel(new javax.swing.tree.DefaultTreeModel(rootNode));
        private void btnChooseTwoActionPerformed(java.awt.event.ActionEvent evt) {
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Two");
            DefaultMutableTreeNode subNode;
            subNode = new DefaultMutableTreeNode("One");
            subNode.add(new DefaultMutableTreeNode("Sub One"));
            subNode.add(new DefaultMutableTreeNode("Sub Two"));
            subNode.add(new DefaultMutableTreeNode("Sub Three"));
            rootNode.add(subNode);
            subNode = new DefaultMutableTreeNode("Two");
            subNode.add(new DefaultMutableTreeNode("Sub One"));
            subNode.add(new DefaultMutableTreeNode("Sub Two"));
            subNode.add(new DefaultMutableTreeNode("Sub Three"));
            rootNode.add(subNode);
            subNode = new DefaultMutableTreeNode("Three");
            subNode.add(new DefaultMutableTreeNode("Sub One"));
            subNode.add(new DefaultMutableTreeNode("Sub Two"));
            subNode.add(new DefaultMutableTreeNode("Sub Three"));
            rootNode.add(subNode);
            mainTree.setModel(new javax.swing.tree.DefaultTreeModel(rootNode));
        private void btnChooseOneActionPerformed(java.awt.event.ActionEvent evt) {
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("One");
            DefaultMutableTreeNode subNode;
            DefaultMutableTreeNode subSubNode;
            subNode = new DefaultMutableTreeNode("One");
            subSubNode = new DefaultMutableTreeNode("Sub One");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub A"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub B"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub C"));
            subNode.add(subSubNode);
            subSubNode = new DefaultMutableTreeNode("Sub Two");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 1"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 2"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 3"));
            subNode.add(subSubNode);
            subSubNode = new DefaultMutableTreeNode("Sub Three");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub X"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub Y"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub Z"));
            subNode.add(subSubNode);
            rootNode.add(subNode);
            subNode = new DefaultMutableTreeNode("Two");
            subSubNode = new DefaultMutableTreeNode("Sub One");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub A"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub B"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub C"));
            subNode.add(subSubNode);
            subSubNode = new DefaultMutableTreeNode("Sub Two");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 1"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 2"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 3"));
            subNode.add(subSubNode);
            subSubNode = new DefaultMutableTreeNode("Sub Three");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub X"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub Y"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub Z"));
            subNode.add(subSubNode);
            rootNode.add(subNode);
            subNode = new DefaultMutableTreeNode("Three");
            subSubNode = new DefaultMutableTreeNode("Sub One");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub A"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub B"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub C"));
            subNode.add(subSubNode);
            subSubNode = new DefaultMutableTreeNode("Sub Two");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 1"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 2"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub 3"));
            subNode.add(subSubNode);
            subSubNode = new DefaultMutableTreeNode("Sub Three");
            subSubNode.add(new DefaultMutableTreeNode("Very Sub X"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub Y"));
            subSubNode.add(new DefaultMutableTreeNode("Very Sub Z"));
            subNode.add(subSubNode);
            rootNode.add(subNode);
            mainTree.setModel(new javax.swing.tree.DefaultTreeModel(rootNode));
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
        public static void main(String args[]) {
            new TestTree().show();
    }

  • Refreshing a jtree(jdeveloper)

    if the tree is expanded then how can it be collapsed on a button's click event
    and if we have a value equivalent to a particular node of jtree then how we can expand the tree upto that level
    i m using jdeveloper and i have to display the in expanded form immediately after selecting a value in a jtable and when i click refresh button then tree must be collapsed

    The problem is that I'm not chaning a node directly. My custom nodes are based on a DOM Document, and I'm changing the contents of one of the document's elements using a text field. So what I need to do is inform the JTree that it needs to re-render.
    Calling treeDidChange() does force the tree to re-render, but the JLabel's at each node do not change their size - so when my renderer does a setText() for a node, the label's bounding box is still the size of the old text and I get a "..." showing that my new text is too big for the box.
    The only way I've found to solve it is to set a new renderer every time I change the model's data. However, this means I am creating tons of these render objects, one on each edit! Yuck!
    I'm sure there's a more elegant or correct way to do this.

Maybe you are looking for