How can we disable the leaf of JTree in Java?


A JTree is a component that presents a hierarchical view of data. The user has the ability to expand or collapse individual sub-trees. A TreeNode interface defines the methods that must be implemented nodes of a JTree object. The DefaulMutableTreeNode class provides a default implementation of a TreeNode interface. We can disable the leaf of JTree by overriding the getTreeCellRendererComponent() method of DefaultTreeCellRenderer class.

Syntax

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)

Example

import java.awt.*;
import javax.swing.tree.*;
import javax.swing.*;
public class JTreeLeafNodeDisableTest extends JFrame {
   private TreeNode treeNode;
   private JTree tree;
   public JTreeLeafNodeDisableTest() {
      setTitle("JTreeLeafNodeDisable Test");
      treeNode = new DefaultMutableTreeNode("Country");
      tree = new JTree();
      tree.setModel(new DefaultTreeModel(treeNode));
      tree.setCellRenderer(new CustomDefaultTreeCellRenderer());
      add(tree);
      setSize(400, 300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new JTreeLeafNodeDisableTest();
   }
   static class CustomDefaultTreeCellRenderer extends DefaultTreeCellRenderer {
      @Override
      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
         boolean enabled = false;
         sel = enabled;
         hasFocus = enabled;
         Component treeCellRendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
         treeCellRendererComponent.setEnabled(enabled);
         return treeCellRendererComponent;
      }
   }
}

Output

Updated on: 03-Jul-2020

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements