The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.tree  [15 examples] > Node Expansion  [2 examples]

e1030. Preventing the Expansion or Collapse of a Node in a JTree Component

There are two ways to prevent the expansion or collapse of a node. The first way is to listen to expansion events and veto those events that expand or collapse particular nodes. This method allows any listener to prevent expansion of collapse of a node (see e1032 Listening for Expansion and Collapse Events in a JTree Component).

The second way is to override JTree.setExpandedState(). This method is suitable if the tree wants to impose particular expansion or collapse rules. This method is demonstrated in this example.

    JTree tree = new JTree() {
        protected void setExpandedState(TreePath path, boolean state) {
            // Ignore all collapse requests; collapse events will not be fired
            if (state) {
                super.setExpandedState(path, state);
            }
        }
    };

 Related Examples
e1029. Expanding or Collapsing All Nodes in a JTree Component

See also: Events    Layout    Nodes    Selections   


© 2002 Addison-Wesley.