This discussion is archived
1 Reply Latest reply: Nov 6, 2012 4:02 AM by 800268 RSS

JTree Disappears From UI When Node is Expanded

972427 Newbie
Currently Being Moderated
I'm experiencing a frustrating issue where the JTree in my UI disappears upon node expansion. It uses a DefaultTreeModel and DefaultMutableTreeNodes. This is similar to {thread:id=2131740}, but I couldn't arrive at a solution based upon that discussion.

Basically I have a tree structure like:

+root (invisible)
---+investment1
------attribute1
------attribute2
---+investment2
------attribute1
------attribute2

My JTree disappears entirely from my GUI when:

(1) I add more than one investment and then click to expand one.
(2) I have one already-expanded investment, then add a second investment.

Put simply: if two investments are present and one is expanded, the JTree disappears. I have tried:

(1) Subclassing DefaultMutableTreeNode with my own tree nodes and ensuring that hashcode is unique and independent of child nodes.
(2) Getting rid of the JScrollPane
(3) Getting rid of the CheckTreeManager
(4) Calling Node.add() instead of insertNodeInto()
(5) Changing the order of adds (eg attributes added prior to adding investment to root)

None of the above have had an effect. I'm quite stumped and would appreciate assistance, thanks!
public class SelectionTree extends JTree{
 
  private static final long serialVersionUID = -7143435038160440156L;
  private DefaultMutableTreeNode root;
  CheckTreeManager checkTreeManager;
  DefaultTreeModel treeModel;
  
  public SelectionTree() {
    // Let the superconstructor create an underlying empty model
    super(new DefaultMutableTreeNode());
    Log.logMessage(LogLevel.DEBUG, "SelectionTree.SelectionTree()");
    
    // Render our tree as a check tree (with selectable check boxes)
    checkTreeManager = new CheckTreeManager(this); 
    
    // Get the underlying tree model
    treeModel = (DefaultTreeModel) this.getModel();
    
    // Get a reference for the root node of the model
    root = (DefaultMutableTreeNode) treeModel.getRoot();

    // Don't show the root node of the tree
    setRootVisible(false);
    
    // Don't let nodes be edited
    this.setEditable(false);
    
    // Put this tree view in a vertical scroll pane
    JScrollPane jsp = new JScrollPane(this);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  }
  
  public boolean containsSymbol(String symbol) {
    for (int i = 0; i < root.getChildCount(); i++) {
      if (root.getChildAt(i).toString().equals(symbol)) return true;
    }
    return false;
  }

  public void addSymbol(String symbol, String[] attributes) throws UIException {
    Log.logMessage(LogLevel.DEBUG, "SelectionTree.addSymbol("+symbol+","+Arrays.toString(attributes)+")");
    
    if(containsSymbol(symbol)) {
      throw new UIException("Symbol '" + symbol + "' already exists in UI tree!");
    }
    
    // Create the new node to add
    DefaultMutableTreeNode toAdd = new DefaultMutableTreeNode(symbol);
    
    // Add that node underneath the root node
    treeModel.insertNodeInto(toAdd, root, root.getChildCount());
    
    // Add each of the new attribute nodes for the new node
    for(String s : attributes) {
      treeModel.insertNodeInto(new DefaultMutableTreeNode(s), toAdd, toAdd.getChildCount());
    }

    // Expands nodes above this node to ensure that it is visible
    this.setSelectionPath(new TreePath(treeModel.getPathToRoot(toAdd)));
  }
}
Edited by: 969424 on Nov 4, 2012 9:03 PM

Edited by: 969424 on Nov 4, 2012 9:11 PM

Edited by: 969424 on Nov 4, 2012 9:13 PM
  • 1. Re: JTree Disappears From UI When Node is Expanded
    800268 Expert
    Currently Being Moderated
    969424 wrote:
    My JTree disappears entirely from my GUI when:

    (1) I add more than one investment and then click to expand one.
    (2) I have one already-expanded investment, then add a second investment.
    You code throws an UIException when adding a node with the same name, so I guess that is what happens. Your code works fine otherwise (note the scroll pane in the constructor does nothing).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points