⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
 } </pre>					<p class="Body"><a name="pgfId-1087714"></a><em class="C-Code">JTree</em> <em class="Bold">Model. </em><a name="marker-1087712"></a>The <em class="CODE">JTree</em> <a name="marker-1087713"></a>class models and displays a vertical list of elements or nodes arranged in a tree-based hierarchy as shown in Figure 6.3.</p>				</div>				<div>					<h6 class="FC"><a name="pgfId-1087719"></a>Figure 6.3 <a name="70972"></a>Vertical list of elements in a tree</h6>					<div>						<img src="CH06-3.gif"></div>					<p class="Body"><a name="pgfId-1087720"></a>A <em class="CODE">JTree</em> object has one root node and one or more child nodes, which can contain further child nodes. Each parent node can be expanded to show all its children similar to directory trees familiar to Windows users.</p>					<p class="Body"><a name="pgfId-1087721"></a>Like the <em class="CODE">JList</em> and <em class="CODE">JTable</em> components, the <em class="CODE">JTree</em> consists of more than one model. The selection model is similar to the one detailed for the <em class="CODE">JList</em> model. The selection modes have the following slightly different names: <em class="CODE">SINGLE_TREE_SELECTION</em> <a name="marker-1087722"></a>, <em class="CODE">DISCONTIGUOUS_TREE_SELECTION</em> <a name="marker-1087723"></a>, and <em class="CODE">CONTIGUOUS_ TREE_SELECTION</em> <a name="marker-1087724"></a>.</p>					<p class="Body"><a name="pgfId-1087726"></a>While <em class="CODE">DefaultTreeModel</em> <a name="marker-1087725"></a>maintains the data in the tree and is responsible for adding and removing nodes, it is the <em class="CODE">DefaultTreeMutableTreeNode</em> <a name="marker-1087727"></a>class that defines the methods used for node traversal. The <em class="CODE">DefaultTreeModel</em> is often used to implement custom models because there is no <em class="CODE">AbstractTreeModel</em> in the <em class="CODE">JTree</em> package. However, if you use custom objects, you must implement <em class="CODE">TreeModel</em> . This code example creates a <em class="CODE">JTree</em> using the <em class="CODE">DefaultTreeModel</em> so the administrator can search and browse the auction numbers and details for reporting.</p>					<pre class="CODE"><a name="pgfId-1087731"></a><a name="marker-1087728"></a><a name="marker-1087729"></a><a name="marker-1087730"></a>import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;public class SimpleTree extends JFrame {  public SimpleTree() {    String[] treelabels =   {&quot;All Auctions&quot;, &quot;Closed Auction&quot;,                              &quot;Open Auctions&quot;};    Integer[] closedItems = {new Integer(500144), new Integer(500146),                             new Integer(500147)};    Integer[] openItems = { new Integer(500148), new Integer(500149)};</pre>					<pre class="CODE"><a name="pgfId-1087732"></a>    //Create an array of nodes based on the size of the labels and</pre>					<pre class="CODE"><a name="pgfId-1087733"></a>    //open and closed items    DefaultMutableTreeNode[] nodes = new                              DefaultMutableTreeNode[treelabels.length];    DefaultMutableTreeNode[] closednodes = new                             DefaultMutableTreeNode[closedItems.length];    DefaultMutableTreeNode[] opennodes = new                             DefaultMutableTreeNode[openItems.length];</pre>					<pre class="CODE"><a name="pgfId-1087734"></a>    //Populate the labels with the data label elements    for (int i=0; i &lt; treelabels.length; i++) {      nodes[i] = new DefaultMutableTreeNode(treelabels[i]);     }</pre>					<pre class="CODE"><a name="pgfId-1087735"></a>&nbsp;</pre>					<pre class="CODE"><a name="pgfId-1087736"></a>    //Arrange the tree so that from the top level All Auctions </pre>					<pre class="CODE"><a name="pgfId-1087737"></a>    //closed and open auctions are children from that top level    nodes[0].add(nodes[1]);    nodes[0].add(nodes[2]);</pre>					<pre class="CODE"><a name="pgfId-1087738"></a>    </pre>					<pre class="CODE"><a name="pgfId-1087739"></a>   for (int i=0; i &lt; closedItems.length; i++) {</pre>					<pre class="CODE"><a name="pgfId-1087740"></a>       //Populate the closed items nodes with the closed item data </pre>					<pre class="CODE"><a name="pgfId-1087741"></a>       //ie auction ids       closednodes[i] = new DefaultMutableTreeNode(closedItems[i]); </pre>					<pre class="CODE"><a name="pgfId-1087742"></a>       //Add the closed items under the Closed Auction parent node       nodes[1].add(closednodes[i]);    }</pre>					<pre class="CODE"><a name="pgfId-1087743"></a>    for (int i=0; i &lt; openItems.length; i++) {</pre>					<pre class="CODE"><a name="pgfId-1087744"></a>       //Populate the open items nodes with the open item data </pre>					<pre class="CODE"><a name="pgfId-1087745"></a>       //ie auction ids       opennodes[i] = new DefaultMutableTreeNode(openItems[i]); </pre>					<pre class="CODE"><a name="pgfId-1087746"></a>       //Add the open items under the Open Auction parent node       nodes[2].add(opennodes[i]);    }</pre>					<pre class="CODE"><a name="pgfId-1087747"></a>    //Finally create the tree model from the nodes created above</pre>					<pre class="CODE"><a name="pgfId-1087748"></a>    //and then create the tree    DefaultTreeModel model=new DefaultTreeModel(nodes[0]);    JTree tree = new JTree(model);    JScrollPane scroll = new JScrollPane(tree);    getContentPane().add(scroll, BorderLayout.CENTER);  } </pre>					<pre class="CODE"><a name="pgfId-1087749"></a>                                                                public static void main(String[] args) {    SimpleTree frame = new SimpleTree();    frame.addWindowListener( new WindowAdapter() {      public void windowClosing( WindowEvent e ) {        System.exit(0);      }    });    frame.setVisible(true);    frame.pack();    frame.setSize(150,150);  }</pre>					<pre class="CODE"><a name="pgfId-1087750"></a>}</pre>					<p class="Body"><a name="pgfId-1087751"></a>The <em class="CODE">toString</em> method is used to retrieve the value for the <em class="CODE">Integer</em> objects in the tree. And although the <em class="CODE">DefaultTreeModel</em> is used to maintain the data in the tree and to add or remove nodes, the <em class="CODE">DefaultMutableTreeNode</em> class defines the methods used to traverse through the nodes in the tree.</p>					<p class="Body"><a name="pgfId-1087755"></a><a name="marker-1087752"></a><a name="marker-1087753"></a>A primitive search of the nodes in a <em class="CODE">JTree</em> is accomplished with the <em class="CODE">depthFirstEnumeration</em> <a name="marker-1087754"></a>method, which is the same as the <em class="CODE">postorderEnumeration</em> method and works its way from the end points of the tree first. You can also call the <em class="CODE">preorderEnumeration</em> <a name="marker-1087756"></a>method, the reverse of the <em class="CODE">postorderEnumeration</em> <a name="marker-1087757"></a>method, which starts from the root and descends each tree in turn. Or you can call the <em class="CODE">breadthFirstEnumeration</em> <a name="marker-1087758"></a>method, which starts from the root and visits all the child nodes in one level before visiting the child nodes at a lower depth.</p>					<p class="Body"><a name="pgfId-1087759"></a>The following code expands the parent node if it contains a child node that matches the search field entered. It uses a call to <em class="CODE">Enumeration e = nodes[0].depthFirstEnumeration()</em> to return a list of all the nodes in the tree. Once it has found a match, it builds the <em class="CODE">TreePath</em> from the root node to the node that matched the search. It passes the <em class="CODE">TreePath</em> to the <em class="CODE">makeVisible</em> method in the <em class="CODE">JTree</em> class that ensures the node is expanded in the tree.</p>					<pre class="CODE"><a name="pgfId-1087763"></a><a name="marker-1087760"></a><a name="marker-1087761"></a><a name="marker-1087762"></a>import java.awt.*;import java.util.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;public class SimpleSearchTree extends JFrame {  JPanel findPanel;  JTextField findField;  JTree tree;  JButton findButton;  DefaultMutableTreeNode[] nodes;  public SimpleSearchTree() {    String[] treelabels =  { &quot;All Auctions&quot;, &quot;Closed Auction&quot;,                              &quot;Open Auctions&quot;};    Integer[] closedItems = { new Integer(500144), new Integer(500146),                               new Integer(500147) };    Integer[] openItems ={ new Integer(500148), new Integer(500149)}; </pre>					<pre class="CODE"><a name="pgfId-1087764"></a>    //Create an array of nodes based on the size of the labels and</pre>					<pre class="CODE"><a name="pgfId-1087765"></a>    //open and closed items</pre>					<pre class="CODE"><a name="pgfId-1087766"></a>    nodes = new DefaultMutableTreeNode[treelabels.length];</pre>					<pre class="CODE-caption"><a name="pgfId-1087768"></a>//API Ref: <a name="10273"></a>DefaultMutableTreeNode(Object node)</pre>					<pre class="CODE"><a name="pgfId-1087773"></a>    <a name="marker-1087769"></a><a name="javax.swing.tree.DefaultMutableTreeNode class"></a><a name="DefaultMutableTreeNode class"></a><a name="DefaultMutableTreeNode constructor"></a>DefaultMutableTreeNode[] closednodes = new </pre>					<pre class="CODE"><a name="pgfId-1087774"></a>                              DefaultMutableTreeNode[closedItems.length];    DefaultMutableTreeNode[] opennodes = new                               DefaultMutableTreeNode[openItems.length];</pre>					<pre class="CODE"><a name="pgfId-1087775"></a>    //Populate the labels with the data label elements    for (int i=0; i &lt; treelabels.length; i++) {       nodes[i] = new DefaultMutableTreeNode(treelabels[i]);     }</pre>					<pre class="CODE"><a name="pgfId-1087776"></a>    //Arrange the tree so that from the top level, all </pre>					<pre class="CODE"><a name="pgfId-1087777"></a>    //closed and open auctions are children from that top level    nodes[0].add(nodes[1]);    nodes[0].add(nodes[2]);</pre>					<pre class="CODE"><a name="pgfId-1087778"></a>    for (int i=0; i &lt; closedItems.length; i++) {</pre>					<pre class="CODE"><a name="pgfId-1087779"></a>       //Populate the closed items nodes with the closed item data,</pre>					<pre class="CODE"><a name="pgfId-1087780"></a>       //i.e., auction ids       closednodes[i] = new DefaultMutableTreeNode(closedItems[i]); </pre>					<pre class="CODE"><a name="pgfId-1087781"></a>       //Add the closed items under the Closed Auction parent node       nodes[1].add(closednodes[i]);    }</pre>					<pre class="CODE">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -