Sunday, September 1, 2013

JTree

JTree is a component that can be used to display data hierarchically. Below are some useful constructors and methods of the JTree class.

Constructors of the JTree class:
-JTree()
creates an empty tree.
-JTree(TreeModel tm)
creates a tree that its data are from TreeModel, tm.
-JTree(TreeNode rootNode)
creates a tree with the specified root node.
Some useful methods of the JTree class:
-getSelectedPath(): TreePath
returns the selected path of the tree.
-getLastSelectedPathComponent(): TreeNode
returns the last selected node of the tree.
-setModel(TreeModel tm):void
sets the data model of the tree.

A tree can have many nodes. The tree always starts with a root node. A root node is parent of all its child nodes. A node can have at most one parent and two child nodes. A node that does not have a child is call leaf node. To create node, you can use the DefaultMutableTreeNode(Object object) class. The instance of this class is node object that can be added to the tree. To determine whether or not a node is a leaf, a root, you can use the isLeaf(), isRoot() methods respectively. You can also add a child node to its parent node by using the add(TreeNode childNode) method.

A tree generate tree selection event. So you need to implement the TreeSelectionListener interface and override the valueChanged(TreeSelectionEvent e) method to perform action.

The example code below creates a tree to display a hierarchical files and directories in a drive. When the program firstly starts, the tree displays the files and directories in the drive determined by the first item of the combobox. You can change the drive so that it leads to the change of the files and directories by selecting a drive from this combobox. When the user selects a node of the tree, the program checks whether this node of the tree is a directory or a file. If it is a directory, its sub-directories and files are listed.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

class JTreeShow extends JFrame implements TreeSelectionListener{
JTree tree;
DefaultMutableTreeNode rootNode;
JLabel lblpath;
JComboBox cboDrives;
JTreeShow(String title){
setTitle(title);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setBackground(Color.WHITE);
contpane.setLayout(new BorderLayout());
cboDrives=new JComboBox(getDrives().toArray());
cboDrives.addItemListener(new ItemList());
tree=new JTree();
tree.addTreeSelectionListener(this);
JScrollPane scroll=new JScrollPane(tree);
JPanel p=new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(cboDrives);
lblpath=new JLabel();
p.add(lblpath);
contpane.add(p,BorderLayout.NORTH);
contpane.add(scroll,BorderLayout.CENTER);
lstShow();

/*
Enumeration iterator=rootNode.preorderEnumeration();
while(iterator.hasMoreElements()){
System.out.println(iterator.nextElement().toString());
}
*/
setVisible(true);

}

class ItemList implements ItemListener{
public void itemStateChanged(ItemEvent e){
lstShow();
}
}

public void lstShow(){
String pathRoot=cboDrives.getSelectedItem().toString();
rootNode=getTree(new DefaultMutableTreeNode(pathRoot),pathRoot);
DefaultTreeModel tmodel=new DefaultTreeModel(rootNode);
tree.setModel(tmodel);

}

public ArrayList<String> getDrives(){
ArrayList<String> drlist=new ArrayList<String>();
File[] dr=File.listRoots();
for(File d:dr){
drlist.add(d.toString());

}
return drlist;
}
public void valueChanged(TreeSelectionEvent te){
try{

TreePath path=tree.getSelectionPath();
DefaultMutableTreeNode selnode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
Object[] paths=path.getPath();
String pat="";
for(Object o:paths){
pat+="/"+o.toString();
}
pat=pat.substring(1);
DefaultMutableTreeNode tempnode=getTree(selnode,pat);
lblpath.setText(pat);
tempnode=null;
}catch(Exception err){}

}


public DefaultMutableTreeNode getTree(DefaultMutableTreeNode rootNode, String path){
File f=new File(path);
if(f.isDirectory()){
rootNode.setAllowsChildren(true);
File[] fs=f.listFiles();
for(File myf:fs){
DefaultMutableTreeNode node=new DefaultMutableTreeNode(myf.getName());
node.setAllowsChildren(true);
if(myf.isDirectory()){
node.setAllowsChildren(true);

}
else{
node.setAllowsChildren(false);

}
rootNode.add(node);

}
}
return rootNode;
}

}


public class JFrameSwing {
public static void main(String[] args){
new JTreeShow("JTree");
}
}
JTree Swing

No comments:

Post a Comment