Web server - Java Swing O Reilly treeModel.insertNodeInto(subroot, root, 0); treeModel.insertNodeInto(leaf1,
Java Swing O Reilly treeModel.insertNodeInto(subroot, root, 0); treeModel.insertNodeInto(leaf1, subroot, 0); treeModel.insertNodeInto(leaf2, root, 1); // And display it getContentPane().add(tree, BorderLayout.CENTER); } public static void main(String args[]) { TestTree tt = new TestTree(); tt.init(); tt.setVisible(true); } } As you can see here, all of the action happens in the init() method. We create several nodes using the DefaultMutableTreeNode class. The DefaultTreeModel class provides us with a basis for working with the tree, and we add our nodes into that model. All of the trees we will look at in this chapter contain the same basic steps gathering nodes, creating a tree, and populating the tree though again, not necessarily in that order. We will also look at the other types of things you can do with trees, including how to catch selection events and how to change the presentation of the nodes and leaves. And just to prove that it’s not hard to listen to selections from a tree, Figure 17.2 shows an expanded example that displays the most recently selected item in a JLabel at the bottom of the application. Figure 17.2. The TestTree program with a TreeSelectionListener label active To make this work, we add a listener directly to the JTree object. The listener looks and behaves much like a ListSelectionListener, but is slightly modified to handle the specifics of tree selections. (For example, selection intervals may have to cross over an expanded node, and all the nodes under the expanded entry must also be selected.) Even though we allow multiple entries to be selected, we only show the lead entry of the selection, to keep output simple. Here’s the chunk of code we need to add to the init() method in the TestTree class: // Create and add our message label for the selection outputfinal JLabel messageLabel = new JLabel(”Nothing selected.”); add(messageLabel, BorderLayout.SOUTH); // Add in our selection listener and have it report to // our messageLabel tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent tse) { TreePath tp = tse.getNewLeadSelectionPath(); messageLabel.setText(”Selected: ” + tp.getLastPathComponent()); } } ); - 468 -
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.