When a path is in the selection in JTree, it is automatically
expanded.  This is OK when paths are added to the selection whenever
the user clicks directly on visible nodes, but my application adds
paths to the selection programmatically, and (for reasons too long to
go into here) I must disable this automatic expansion.

The code responsible unwanted expansion is buried in the internal
class TreeSelectionHandler of BasicTreeUI:

  public class TreeSelectionHandler implements TreeSelectionListener {
    /**
     * Messaged when the selection changes in the tree we're displaying
     * for.  Stops editing, messages super and displays the changed paths.
     */
    public void valueChanged(TreeSelectionEvent event) {
      // Stop editing
      completeEditing();
      // Make sure all the paths are visible, if necessary.
      if(tree.getExpandsSelectedPaths() && treeSelectionModel != null) {
      TreePath[]           paths = treeSelectionModel
        .getSelectionPaths();

        // HERE'S THE PROBLEM
        if(paths != null) {
          for(int counter = paths.length - 1; counter >= 0;
                counter--) {
                    tree.makeVisible(paths[counter]);
          }
        }
      }

      // REST OF valueChanged

    } // end of valueChanged
  } // end of TreeSelectionHandler

I've tried several things to disable the unwanted expansion, but they
all fail.  The variable that holds the TreeSelectionListener in
BasicTreeUI is private, so one cannot assign to a it a different
implementation of the TreeSelectionListener interface.  Moreover,
TreeSelectionHandler's valueChanged() uses a whole slew of private
variables, so it is impossible (at least to me) to override it without
calling super.valueChanged() (which inevitably results in the unwanted
expansion).

Is there a way to override this unwanted behavior?

Thanks!

KJ
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to