Web proxy server - Java Swing O Reilly javax.swing.DefaultMutableTreeNode class serves as
Java Swing O Reilly javax.swing.DefaultMutableTreeNode class serves as our node class. You don’t have to worry about specifically making a node a leaf. If the node has no references to other nodes by the time you display it, it’s a leaf. [1] The appearance of an internal frame in the Metal look-and-feel has changed slightly since these screen shots were taken. Figure 17.1. A simple JTree in the Metal, Motif, and Windows look-and-feels This example works by building up a series of unconnected nodes (using the DefaultMutableTreeNode class) and then connecting them. As long as we stick to the default classes provided with the tree package, we can build a regular model out of our nodes quite quickly. In this example, we build the model based on an empty root node, and then populate the tree by attaching the other nodes to the root or to each other. You can also build the tree first, and then create the model from the root node. Both methods have the same result. With a valid tree model in place, we can make a real JTree object and display it. // TestTree.java // A Simple test to see how we can build a tree and populate it. // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; public class TestTree extends JFrame { JTree tree; DefaultTreeModel treeModel; public TestTree() { super(”Tree Test Example”); setSize(400, 300); addWindowListener(new BasicWindowMonitor()); } public void init() { // Build up a bunch of TreeNodes. We use DefaultMutableTreeNode because the// DefaultTreeModel can use that to build a complete tree. DefaultMutableTreeNode root = new DefaultMutableTreeNode(”Root”); DefaultMutableTreeNode subroot = new DefaultMutableTreeNode(”SubRoot”); DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode(”Leaf 1″); DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode(”Leaf 2″); // Build our tree model starting at the root node, and then make a JTree out // of that. treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); // Build the tree up from the nodes we created - 467 -
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.