Advertisement



< Prev
Next >



JTree class




JTree class is used to create a hierarchical view of data, with one top main root node, its sub-root nodes and their children nodes. The user can expand or collapse the top main root node and its sub-root nodes. When a node is selected from the JTree, a TreeSelectionEvent is generated, which is handled by implementing TreeSelectionListener interface. JTree is another lightweight component which extends JComponent class.





Constructors of JTree


Constructor Description
public JTable() Creates a JTree with a sample model.
public JTree(TreeModel newModel) Creates a JTree using TreeModel.
public JTree(TreeNode root) Creates a JTree with the specified TreeNode as its root.





Methods of JTree class


Methods Description
public TreeModel getModel() Gets the TreeModel whose data is displayed by JTree.
public int getRowCount() Gets the total number of the rows in the JTree.
public void addTreeSelectionListener(TreeSelectionListener tsl) Adds a listener for TreeSelectionEvents with JTree.





An example of JTree


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;


public class Tree2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{
JFrame jf;
JTree tree1; 
DefaultTreeModel dTree;
JLabel label1, label2;

A()
{
 
jf= new JFrame("JTree");
label1 = new JLabel("Displaying tree");

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Nutritious Food");

//Creating 3 children of the root node, Nutritious Food.
DefaultMutableTreeNode fruits = new DefaultMutableTreeNode("Fruits");
DefaultMutableTreeNode vegetables = new DefaultMutableTreeNode("Vegetables");
DefaultMutableTreeNode dryFruits = new DefaultMutableTreeNode("Dry Fruits");

//Adding 3 children - to Nutritious Food
rootNode.add(fruits);
rootNode.add(vegetables);
rootNode.add(dryFruits);


//Creating 3 children of fruits
DefaultMutableTreeNode fruit1 = new DefaultMutableTreeNode("Apple");
DefaultMutableTreeNode fruit2= new DefaultMutableTreeNode("Mango");
DefaultMutableTreeNode fruit3= new DefaultMutableTreeNode("Grapes");

//Adding 3 children to fruits
fruits.add(fruit1);
fruits.add(fruit2);
fruits.add(fruit3);


//Creating two children of vegetables
DefaultMutableTreeNode vegetable1 = new DefaultMutableTreeNode("Cabbage");
DefaultMutableTreeNode vegetable2= new DefaultMutableTreeNode("Tomato");
DefaultMutableTreeNode vegetable3 = new DefaultMutableTreeNode("Potato");

//Adding two child of vegetables
vegetables.add(vegetable1);
vegetables.add(vegetable2);
vegetables.add(vegetable3);


//Creating and adding one child dry fruits
DefaultMutableTreeNode dryFruit1 = new DefaultMutableTreeNode("Almonds");
DefaultMutableTreeNode dryFruit2 = new DefaultMutableTreeNode("Walnuts");
dryFruits.add(dryFruit1);
dryFruits.add(dryFruit2);


dTree= new DefaultTreeModel(rootNode);

//Creating a JTree from DefaultTreeModel, implementer of TreeModel Interface.
JTree tree = new JTree(dTree);

//Adding JTree to JScrollPane
JScrollPane scrollP = new JScrollPane(tree);

scrollP.setBorder(BorderFactory.createEmptyBorder()); //How to remove the border of JScrollPane.
scrollP.setPreferredSize(new Dimension(300, 230));

jf.add(label1);
jf.add(scrollP);


jf.setLayout(new FlowLayout());
jf.setSize(400,320);
jf.setVisible(true);
}

}

When you run the code, you are presented a window that shows a tree structure with the root Nutritious Food and its children node and their children nodes.

Figure 1

When you click on the each of these children node, you are shown its children nodes, depicting our constructed JTree.

Figure 2



Advertisement




Handling JTree events by implementing TreeSelectionListener


In the next code, we are going to handle the events occurring when a particular cell of JTable is selected or when it's value is changed, to handle such events, we will implement TableModelListener interface and will provide implementation of its method tablechanged().

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;


public class Tree3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements TreeSelectionListener
{
Object [] index;
JFrame jf;
JTree tree1; 
DefaultTreeModel dTree;
JLabel label1, label2;

A()
{
index= new Object[]{"Local Disk (C:)", "Local Disk (D:)", "Local Disk (E:)"};


 
jf= new JFrame("JTree");
label1 = new JLabel("Displaying tree");
label2 = new JLabel();


DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("My Computer");

//Creating 3 children of the root node, My Computer.
DefaultMutableTreeNode cDrive = new DefaultMutableTreeNode("Local Disk (C:)");
DefaultMutableTreeNode dDrive = new DefaultMutableTreeNode("Local Disk (D:)");
DefaultMutableTreeNode eDrive = new DefaultMutableTreeNode("Local Disk (E:)");

//Adding 3 children - C, D, and E Drive to the root note, My Computer
rootNode.add(cDrive);
rootNode.add(dDrive);
rootNode.add(eDrive);


//Creating 3 children of C drive
DefaultMutableTreeNode cDriveChild1 = new DefaultMutableTreeNode("Documents and Settings");
DefaultMutableTreeNode cDriveChild2 = new DefaultMutableTreeNode("Windows");
DefaultMutableTreeNode cDriveChild3 = new DefaultMutableTreeNode("Java");

//Adding 3 children to C drive
cDrive.add(cDriveChild1);
cDrive.add(cDriveChild2);
cDrive.add(cDriveChild3);


//Creating two children of D Drive
DefaultMutableTreeNode dDriveChild1 = new DefaultMutableTreeNode("Games");
DefaultMutableTreeNode dDriveChild2 = new DefaultMutableTreeNode("Music");

//Adding two child of D Drive to D Drive.
dDrive.add(dDriveChild1);
dDrive.add(dDriveChild2);


//Creating and adding one child of E Drive.
DefaultMutableTreeNode eDriveChild1 = new DefaultMutableTreeNode("Movies");
eDrive.add(eDriveChild1);


dTree= new DefaultTreeModel(rootNode);

//Creating a JTree from DefaultTreeModel, implementer of TreeModel Interface.
JTree tree = new JTree(dTree);


//Registering A class to listen to JTree events.
tree.addTreeSelectionListener(this);

//Adding JTree to JScrollPane
JScrollPane scrollP = new JScrollPane(tree);

scrollP.setBorder(BorderFactory.createEmptyBorder()); //How to remove the border of JScrollPane.
scrollP.setPreferredSize(new Dimension(300, 190));


jf.add(label1);
jf.add(scrollP);
jf.add(label2);

jf.setLayout(new FlowLayout());
jf.setSize(400,300);
jf.setVisible(true);
}

public void valueChanged(TreeSelectionEvent treeEvent)
{
label2.setText("Path selected : " + treeEvent.getPath()); //Returns TreePath object, whose toString() method is called to display the selected path
System.out.println("path selected");
}

}
When you run the code, you are presented a window that shows a tree structure with the root My Computer and its children drives.

Figure 3


When you click on the each of these children drives, you are shown its contents and at the bottom of the JTree, you are also shown the path to this selected content.

Figure 4




Please share this article -





< Prev
Next >
< JList
JScrollPane >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement