JTree – Java Swing – Example

In this tutorial, we are going to see an example of JTree in Java Swing. JTree class is used to display tree-structured or hierarchical data. JTree is a complex component. It has a “root node” at the top that is the parent of all nodes in the tree. It inherits from the JComponent class.


 
 

JTree constructors class:

JTree constructors

Description

JTree()Create a JTree.
JTree(Object[] value)Creates a JTree with each element of the specified array as a child of a new root node.
JTree(TreeNode root)Creates a JTree with the specified TreeNode as root, which displays the root node.

Example of JTree in Java Swing:

Let’s look at how to build a simple JTree. Let’s say we want to display the list of backend and frontend frameworks in a hierarchical way.

The node is represented by the TreeNode class which is an interface. The MutableTreeNode interface inherits this interface which represents a mutable node. The Swing API provides an implementation of this interface in the form of DefaultMutableTreeNode class.

We will use DefaultMutableTreeNode class to represent our node. This class is provided in the Swing API and we can use it to represent our nodes. This class has a handy add() method that takes an instance of MutableTreeNode.

So first we’ll create the root node. And then we can recursively add nodes to that root.
 
 

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

public class MyTree

{

MyTree()

{

JFrame f = new JFrame();

//root node

DefaultMutableTreeNode framework = new DefaultMutableTreeNode("Framework");

//inner node 1

DefaultMutableTreeNode front = new DefaultMutableTreeNode("Front-End");

//inner node 2

DefaultMutableTreeNode back = new DefaultMutableTreeNode("Back-End");

//leaf

DefaultMutableTreeNode others = new DefaultMutableTreeNode("Others");

//Add inner node 1 to the root node

framework.add(front);

//Add leaves to node 1

DefaultMutableTreeNode angular = new DefaultMutableTreeNode("AngularJS");

DefaultMutableTreeNode react = new DefaultMutableTreeNode("React.js");

DefaultMutableTreeNode meteor = new DefaultMutableTreeNode("Meteor.js");

DefaultMutableTreeNode ember = new DefaultMutableTreeNode("Ember.js ");

front.add(angular); front.add(react); front.add(meteor); front.add(ember);

//Add inner node 2 to the root node

framework.add(back);

//Add leaves to node 2

DefaultMutableTreeNode nodejs = new DefaultMutableTreeNode("NodeJS");

DefaultMutableTreeNode express = new DefaultMutableTreeNode("Express");

back.add(nodejs); back.add(express);

//Add the last leaf to the root node

framework.add(others);

JTree jt = new JTree(framework);

f.add(

new JScrollPane(jt) //Add a scroll bar

);

f.setSize(200,200);

f.setVisible(true);

}

public static void main(String[] args) {

new MyTree();

}

}

Output:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值