Listing 16.1 JTree Displaying Its Nodes and Leaves (TJTree.java)
// Demonstrates how to create the Swing tree widget to
// represent hierarchical data.
/*
*
*/
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class TJTree extends JApplet {
Container container;
public void init() {
// 1. Get the handle on applet's content pane.
container = this.getContentPane();
// 2. Root node (col0 or root) of the tree.
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("C:\\");
// 3. Create the nodes in column 1.
DefaultMutableTreeNode col11 =
new DefaultMutableTreeNode("Docs");
DefaultMutableTreeNode col12 =
new DefaultMutableTreeNode("README");
// 4. Add the nodes to the root.
root.add(col11);
root.add(col12);
// 5. Create the nodes in column 2.
DefaultMutableTreeNode col21 =
new DefaultMutableTreeNode("API");
DefaultMutableTreeNode col22 =
new DefaultMutableTreeNode("index.html");
// 6. Add the nodes to the node 1 in column 1.
col11.add(col21);
col11.add(col22);
// 7. Create the nodes in column 3.
DefaultMutableTreeNode col31 =
new DefaultMutableTreeNode("Swing");
// 8. Add the node to the node 2 in column 2.
col21.add(col31);
// 9. Create the nodes in column 4.
DefaultMutableTreeNode col41 =
new DefaultMutableTreeNode("JComponent.html");
DefaultMutableTreeNode col42 =
new DefaultMutableTreeNode("JButton.html");
DefaultMutableTreeNode col43 =
new DefaultMutableTreeNode("JLabel.html");
// 10. Add the nodes to the node called Swing.
col31.add(col41);
col31.add(col42);
col31.add(col43);
// 11. Attach the root (of nodes) to the tree object.
JTree tree = new JTree(root);
// 12. Add the tree to a scroll pane and add the scroll pane
// to the container.
JScrollPane scrollPane = new JScrollPane(tree);
container.add(scrollPane);
}
}
Listing 16.2 JTree Displaying the File System Hierarchy Using the Custom Tree Model (TTreeModel.java)
// Demonstrates how to use custom data models in trees.
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
public class TTreeModel extends JFrame {
public TTreeModel() {
super("TTreeModel"); // Give a title to the frame
// 1. Create an object of FileSystemModel, and create a tree
// with that model. Add the tree to a scroll pane, and add
// the scroll pane to the frame.
FileSystemModel fileSystemDataModel = new FileSystemModel();
JTree tree = new JTree(fileSystemDataModel);
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane);
// 2. Configure the frame and display it.
addWindowListener(new WindowEventHandler());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(300, 250);
show();
}
// 3. The main method.
public static void main(String [] args) {
TTreeModel frame = new TTreeModel();
}
// 4. Define a listener class to close the frame.
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
// 5. Custom data model that represents the file system data.
class FileSystemModel implements TreeModel {
private String root; // The root identifier
private Vector listeners; // Declare the listeners vector
public FileSystemModel() {
// 6. Get the home directory on your system.
root = System.getProperty("user.home");
// 7. For Windows 95, root is set to C:\ ("usr.home"
// retrieves C:\WIN95).
// You may implement a snippet like this for other OS also
// like Windows NT, Macintosh, and so on.
if (System.getProperty("os.name").equals("Windows 95")) {
File tempFile = new File(root);
root = tempFile.getParent();
}
// 8. Define the listeners vector.
listeners = new Vector();
}
// 9. Retrieves the root of the tree hierarchy.
public Object getRoot() {
return (new File(root));
}
// 10. Retrieves the members in a directory based on an index.
public Object getChild(Object parent, int index) {
File directory = (File) parent;
String[] directoryMembers = directory.list();
return (new File(directory, directoryMembers[index]));
}
// 11. Retrieves the member count in a directory.
public int getChildCount(Object parent) {
File fileSystemMember = (File) parent;
// fileSystemMember is a directory or file.
// If a file system member is a directory.
if (fileSystemMember.isDirectory()) {
String[] directoryMembers = fileSystemMember.list();
// Get the count of members in the directory.
return directoryMembers.length;
}
// If the file system member is a file.
else {
// Return the member count as zero.
return 0;
}
}
// 12. Returns the index of a given member in its directory.
public int getIndexOfChild(Object parent, Object child) {
File directory = (File) parent;
File directoryMember = (File) child;
String[] directoryMemberNames = directory.list();
int result = -1;
for (int i = 0; i
*
*/
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class TTreeCellRenderer extends JApplet {
Container container;
public void init() {
// 1. Get the handle on applet's content pane.
container = this.getContentPane();
// 2. Root node (col0 or root) of the tree.
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("C:\\");
// 3. Create the nodes in column 1.
DefaultMutableTreeNode col11 =
new DefaultMutableTreeNode("Docs");
DefaultMutableTreeNode col12 =
new DefaultMutableTreeNode("Others");
// 4. Add the nodes to the root.
root.add(col11);
root.add(col12);
// 5. Create the nodes in column 2.
DefaultMutableTreeNode col21 =
new DefaultMutableTreeNode("API");
DefaultMutableTreeNode col22 =
new DefaultMutableTreeNode("Bla...");
// 6. Add these nodes to node 1 in column 1.
col11.add(col21);
col12.add(col22);
// 7. Create the nodes in column 3.
DefaultMutableTreeNode col31 =
new DefaultMutableTreeNode("Swing");
// 8. Add the node to node 2 in column 2.
col21.add(col31);
// 9. Create the nodes in column 4.
DefaultMutableTreeNode col41 =
new DefaultMutableTreeNode("JComponent.html");
DefaultMutableTreeNode col42 =
new DefaultMutableTreeNode("JButton.html");
DefaultMutableTreeNode col43 =
new DefaultMutableTreeNode("JLabel.html");
// 10. Add the nodes to the node called Swing.
col31.add(col41);
col31.add(col42);
col31.add(col43);
// 11. Attach the root (of nodes) to the tree object.
JTree tree = new JTree(root);
// 12. Get the reference to the existing (default) renderer.
DefaultTreeCellRenderer renderer =
(DefaultTreeCellRenderer) tree.getCellRenderer();
// 13. Prepare the cell height for the new icons.
tree.setRowHeight(30); // 30 pixels
// 14. Attach the new icons for the leaves nodes when opened,
// and nodes when closed.
renderer.setLeafIcon(new ImageIcon("leafIcon.gif"));
renderer.setOpenIcon(new ImageIcon("fileOpen.gif"));
renderer.setClosedIcon(new ImageIcon("fileClosed.gif"));
// 15. Customize the text colors.
renderer.setFont(new Font("Monospaced", // font name
Font.BOLD|Font.ITALIC, // font type
15)); // Font size.
renderer.setTextNonSelectionColor(Color.blue);
renderer.setTextSelectionColor(Color.white);
// 16. Customize the background of the tree node-cells.
renderer.setBackgroundNonSelectionColor(Color.white);
renderer.setBackgroundSelectionColor(Color.gray);
renderer.setBorderSelectionColor(Color.lightGray);
// 17. Finally, add the tree to a scroll pane and the scroll pane
// to the container.
JScrollPane scrollPane = new JScrollPane(tree);
container.add(scrollPane);
}
}