0

I have a Swing application that uses a JTree. I want some of the nodes of the tree to be hidden, so I have implemented two DefaultTreeModels, one with every node and a filtered one with only the ones that should be displayed. The latter is set as the actual model.

At some points I must change the filtered nodes, and when I do, the items in the tree update properly, but their behavior is wrong. Nodes do not get highlighted when they are selected (even though they are indeed selected) and the user can no longer double-click to expand a node, they must click the little '+' button.

Below is a generalization of my code, two methods from my custom class that extends JTree.

updateFilter gets called when the filter needs to be updated. populateFilteredNode recursively populates the root node of my filtered model. For simplicity, filteredRoot is a class member variable (of type DefaultMutableTreeNode) and is the root of the filtered model. fullModel and filteredModel are of type DefaultTreeModel

public void updateFilter() {
    // Get current expansion state
    ArrayList<Integer> expansionState = getExpansionState();

    DefaultMutableTreeNode fullModelRoot = fullModel.getRoot();

    // Remove existing nodes in the filtered model
    while(filteredRoot.getChildCount() > 0) {
        filteredModel.removeNodeFromParent(filteredRoot.getFirstChild());
    }

    populateFilteredNode(fullModelRoot, filteredRoot);

    // Repaint tree and restore expansion state
    repaint();
    setExpansionState(expansionState);
}

private void populateFilteredNode(DefaultMutableTreeNode fullNode, DefaultMutableTreeNode filteredNode) {

    int index = 0;

    for(int n = 0; n < fullNode.getChildCount(); n++) {
        DefaultMutableTreeNode fullChildNode = fullNode.getChildAt(n);

        // Show the item and its children if one of many cases is true
        if(shouldShowItem(fullChildNode.getItem())) {
            DefaultMutableTreeNode filteredChildNode = fullChildNode.clone();

            filteredModel.insertNodeInto(filteredChildNode, filteredNode, index++);

            populateFilteredNode(fullChildNode, filteredChildNode);
        }
    }
}

If anyone has a similar experience or knows why the selected node will not appear highlighted, please let me know. Or if there is a better way to accomplish filtering. Or if more code would help provide an answer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Robin
  • 1
  • 1
  • 2
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) *"..my custom class that extends `JTree`."* I'd give odds of 100 to 1 that this is **not** a case for extending `JTree`. – Andrew Thompson Feb 24 '17 at 17:20
  • @Andrew I admit I am new to this, so I don't know exactly when I should or shouldn't create a custom subclass. Why would you bet that this is not a case to extend `JTree`? And would that be related to why the selected nodes don't highlight? – Robin Feb 24 '17 at 17:32
  • I'll know more when I look at the code. I'll look at the code after there is an MCVE/SSCCE. – Andrew Thompson Feb 24 '17 at 17:54

1 Answers1

0

I found something that works for my case, although it's quick and dirty and I don't necessarily understand why it works. This 12-year-old post on Code Ranch somehow got me headed in the right direction. I'm just posting it here in case anyone has a similar problem and it might be of help.

I save the selection path before making any changes to the table model, and then call this new function findNewSelectionPath after the changes were made. Below is a generalized version of the function (I use several custom classes so I did my best to make it look generically usable).

private TreePath findNewSelectionPath(TreePath oldSelectionPath) {
    TreePath newSelectionPath = null;

    if(oldSelectionPath != null) {
        Object[] oldPathComponents = oldSelectionPath.getPath();
        Object[] newPathComponents = new Object[oldPathComponents.length];

        DefaultMutableTreeNode node = (DefaultMutableTreeNode) filteredModel.getRoot();

        // Set the root
        if(oldPathComponents[0].equals(node)) {
            newPathComponents[0] = node;
        }

        // Set the rest of the path components
        for(int n = 1; n < oldPathComponents.length; n++) {
            for(int k = 0; k < node.getChildCount(); k++) {
                if(oldPathComponents[n].equals(node.getChildAt(k))) {
                    newPathComponents[n] = node.getChildAt(k);
                    node = node.getChildAt(k);
                    break;
                }
            }
        }

        // Make sure that the last path component exists
        if(newPathComponents[newPathComponents.length - 1] != null) {
            newSelectionPath = new TreePath(newPathComponents);
        }
    }
    return newSelectionPath;
}
Robin
  • 1
  • 1
  • 2