Override JTree.setExpandedState() to prevent Expansion or Collapse of a Node in a JTree in Java
Description
The following code shows how to override JTree.setExpandedState() to prevent Expansion or Collapse of a Node in a JTree.
Example
// w w w . ja v a2s. c om
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
public class Main {
public static void main(String[] argv) {
JTree tree = new JTree() {
protected void setExpandedState(TreePath path, boolean state) {
if (state) {
super.setExpandedState(path, state);
}
}
};
JFrame frame = new JFrame("Ignore all collapse requests");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(tree));
frame.setSize(380, 320);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »