- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How can we disable the maximize button of a JFrame in Java?
- Java Program to get the previous leaf from a JTree
- How to get the leaf after this node in a JTree component with Java?
- Can we disable JComboBox arrow button in Java?
- How can we disable the cell editing inside a JTable in Java?
- How can we disable cut, copy and paste functionality of a JTextArea in Java?
- Can we prevent the collapse of nodes and child nodes in a JTree with Java?\n
- How can we ENABLE AND DISABLE a particular MySQL event?
- How to get the leaves of nodes in a JTree with Java?
- How to make a JTree editable in Java?
- How to automatically resize a JTree in Java
- How to set the Row Height of a JTree with Java?
- Can I get the node at a specified index in a JTree with Java?
- How to get the depth of root node in a JTree with Java?
- How can we implement the HTML text of JButton in Java?

Advertisements