Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Check whether a node is a root node or not in JTree
To check whether a node is a root node or not, use the isRoot() method. This returns a boolean value. TRUE if the node is a root node, else FALSE is returned. For example, TRUE is returned since the following node is a root node −
node.isRoot()
Another example, FALSE is returned since the following node isn’t a root node −
node2.isRoot()
The following is an example to check whether a node is a root node or not −
Example
package my;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class SwingDemo {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Demo");
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Videos");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Tutorials");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("QA");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Tools");
node.add(node1);
node.add(node2);
node.add(node3);
node.add(node4);
DefaultMutableTreeNode one = new DefaultMutableTreeNode("PHP Videos");
DefaultMutableTreeNode two = new DefaultMutableTreeNode("HTML5 Videos");
DefaultMutableTreeNode three = new DefaultMutableTreeNode("BlockchainVideos");
DefaultMutableTreeNode four = new DefaultMutableTreeNode("Java");
DefaultMutableTreeNode five = new DefaultMutableTreeNode("DBMS");
DefaultMutableTreeNode six = new DefaultMutableTreeNode("CSS");
DefaultMutableTreeNode seven = new DefaultMutableTreeNode("MongoDB");
DefaultMutableTreeNode eight = new DefaultMutableTreeNode("Python QA");
DefaultMutableTreeNode nine = new DefaultMutableTreeNode("jQuery QA");
DefaultMutableTreeNode ten = new DefaultMutableTreeNode("Photo Editing Tool");
node1.add(one);
node1.add(two);
node1.add(three);
node2.add(four);
node2.add(five);
node2.add(six);
node2.add(seven);
node3.add(eight);
node3.add(nine);
node4.add(ten);
JTree tree = new JTree(node);
for (int i = 0; i Output

Following is our JTree −

Advertisements
