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

📄 3_display.html

📁 XML_JAVA指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 377 KB
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<new>{</new>   <new>...<b></b></new>   <new><b>// Set up the tree</b></new>   <new><b>JTree tree = new JTree();</b></new>   <new><b>// Build left-side view</b></new>   <new><b>JScrollPane treeView = new JScrollPane(tree);</b></new>   <new><b>treeView.setPreferredSize(  </b></new>      <new><b>new Dimension( leftWidth, windowHeight ));</b></new><new>}</new> // Constructor</pre></blockquote><p>Now create a non-editable JEditPane that will eventually hold the contents   pointed to by selected JTree nodes:</p><blockquote>   <p></p>  <p></p>  <pre><new>public DomEcho02()</new><new>{</new>   <new>....</new><new></new>   <new><b>// Build right-side view</b></new>   <new><b>JEditorPane htmlPane = new JEditorPane("text/html","");</b></new>   <new><b>htmlPane.setEditable(false);</b></new>   <new><b>JScrollPane htmlView = new JScrollPane(htmlPane);</b></new>   <new><b>htmlView.setPreferredSize( </b></new>       <new><b>new Dimension( rightWidth, windowHeight ));</b></new><new>}</new>  // Constructor</pre> </blockquote>With the left-side JTree and the right-side JEditorPane constructed, create a JSplitPane to hold them: <blockquote>   <p></p>  <pre><new>public DomEcho02()</new><new>{</new>   <new>....</new><new></new>   <new><b>// Build split-pane view</b></new>   <new><b>JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,</b></new>                                          <new><b>treeView,</b></new>                                          <new><b>htmlView );</b></new>   <new><b>splitPane.setContinuousLayout( true );</b></new>   <new><b>splitPane.setDividerLocation( leftWidth );</b></new>   <new><b>splitPane.setPreferredSize( </b></new>       <new><b>new Dimension( windowWidth + 10, windowHeight+10 ));</b></new><new>}</new>  // Constructor</pre> </blockquote><p>With this code, you set up the JSplitPane so with a vertical divider. That   produces a &quot;horizontal split&quot; between the tree and the editor pane.   (More of a horizontal layout, really.) You also set the location of the divider   so that the tree got the width it prefers, with the remainder of the window   width allocated to the editor pane.</p><p> Finally, specify the layout for the panel and add the split pane: </p><blockquote>   <p></p>  <p></p>  <pre><new>public DomEcho02()</new><new>{</new>   <new>...</new><new></new>   <new><b>// Add GUI components</b></new>   <new><b>this.setLayout(new BorderLayout());</b></new>   <new><b>this.add("Center", splitPane );</b></new><new>}</new> // Constructor </pre>  </blockquote><p>Congratulations! The program is now a GUI app. You can run it now to see what   the general layout will look like on screen. For reference, here is the completed   constructor:</p><blockquote>   <p></p>  <pre>    <new>public DomEcho02()</new>    <new>{</new>       <new>// Make a nice border</new>       <new>EmptyBorder eb = new EmptyBorder(5,5,5,5);</new>       <new>BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);</new>       <new>CompoundBorder cb = new CompoundBorder(eb,bb);</new>       <new>this.setBorder(new CompoundBorder(cb,eb));</new>       <new>// Set up the tree</new>       <new>JTree tree = new JTree();</new>       <new>// Build left-side view</new>       <new>JScrollPane treeView = new JScrollPane(tree);</new>       <new>treeView.setPreferredSize(  </new>           <new>new Dimension( leftWidth, windowHeight ));</new>       <new>// Build right-side view</new>       <new>JEditorPane htmlPane = new JEditorPane("text/html","");</new>       <new>htmlPane.setEditable(false);</new>       <new>JScrollPane htmlView = new JScrollPane(htmlPane);</new>       <new>htmlView.setPreferredSize( </new>           <new>new Dimension( rightWidth, windowHeight ));</new>       <new>// Build split-pane view</new>       <new>JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,</new>                                              <new>treeView,</new>                                              <new>htmlView );</new>       <new>splitPane.setContinuousLayout( true );</new>       <new>splitPane.setDividerLocation( leftWidth );</new>       <new>splitPane.setPreferredSize( </new>            <new>new Dimension( windowWidth + 10, windowHeight+10 ));</new>       <new>// Add GUI components</new>       <new>this.setLayout(new BorderLayout());</new>       <new>this.add("Center", splitPane );</new>    <new>}</new> // Constructor </pre></blockquote><h3><a name="adapters"></a>Create Adapters to Display the DOM in a JTree<br></h3><p>Now that you have a GUI framework to display a JTree in, the next step is get   the JTree to display the DOM. But a JTree wants to display a TreeModel. A DOM   is a tree, but it's not a TreeModel. So you'll need to create an adapter class   that makes the DOM look like a TreeModel to a JTree. <p>Now, when the TreeModel passes nodes to the JTree, JTree uses the <tt>toString</tt>   function of those nodes to get the text to display in the tree. The standard   <tt>toString</tt> function isn't going to be very pretty, so you'll need to   wrap the DOM nodes in an AdapterNode that returns the text we want. What the   TreeModel gives to the JTree, then, will in fact be AdapterNode objects that   wrap DOM nodes. <blockquote>   <p><b>Note:</b><br>    The classes that follow are defined as inner classes. If you are coding for     the 1.1 platform, you will need to define these class as external classes.   </p></blockquote><h4><a name="AdapterNode"></a>Define the AdapterNode Class</h4><p>Start by importing the tree, event, and utility classes you're going to need   to make this work: </p><blockquote>   <p></p>  <pre><new><b>// For creating a TreeModel</b></new><new><b>import javax.swing.tree.*;</b></new><new><b>import javax.swing.event.*;</b></new><new><b>import java.util.*;</b></new>public class DomEcho02 <new>extends JPanel</new><new></new>{  </pre></blockquote><p> Moving back down to the end of the program, define a set of strings for the   node element types: </p><blockquote>   <pre><new><b>    </b>  ...    }</new> // makeFrame    <new><b>// An array of names for DOM node-types</b></new>    <new><b>static final String[] typeName = {</b></new>        <new><b>"none",</b></new>        <new><b>"Element",</b></new>        <new><b>"Attr",</b></new>        <new><b>"Text",</b></new>        <new><b>"CDATA",</b></new>        <new><b>"EntityRef",</b></new>        <new><b>"Entity",</b></new>        <new><b>"ProcInstr",</b></new>        <new><b>"Comment",</b></new>        <new><b>"Document",</b></new>        <new><b>"DocType",</b></new>        <new><b>"DocFragment",</b></new>        <new><b>"Notation",</b></new>    <new><b>};</b></new> } // DomEcho</pre></blockquote><blockquote>   <p> </blockquote><blockquote> </blockquote><p>These are the strings that will be displayed in the JTree. The specification   of these nodes types can be found in the <a href="../../api/org/w3c/dom/Node.html">org.w3c.dom.Node</a>   class comments.<p>Next, define the AdapterNode wrapper for DOM nodes: <blockquote>   <pre>    <new>static final String[] typeName = {</new>        ...    <new>};</new><new></new>    <new></new><new></new><new><b>public class AdapterNode </b></new>    <new><b>{ </b></new>      <new><b>org.w3c.dom.Node domNode;</b></new>      <new><b>// Construct an Adapter node from a DOM node</b></new>      <new><b>public AdapterNode(org.w3c.dom.Node node) {</b></new>        <new><b>domNode = node;</b></new>      <new><b>}</b></new>      <new><b>// Return a string that identifies this node in the tree</b></new>      <new><b>// *** Refer to table at top of org.w3c.dom.Node ***</b></new>      <new><b>public String toString() {</b></new>        <new><b>String s = typeName[domNode.getNodeType()];</b></new>        <new><b>String nodeName = domNode.getNodeName();</b></new>        <new><b>if (! nodeName.startsWith("#")) {</b></new>           <new><b>s += ": " + nodeName;</b></new>        <new><b>}</b></new>        <new><b>if (domNode.getNodeValue() != null) {</b></new>           <new><b>if (s.startsWith("ProcInstr")) </b></new>              <new><b>s += ", "; </b></new>           <new><b>else </b></new>              <new><b>s += ": ";</b></new>           <new><b>// Trim the value to get rid of NL's at the front</b></new>           <new><b>String t = domNode.getNodeValue().trim();</b></new>           <new><b>int x = t.indexOf("\n");</b></new>           <new><b>if (x &gt;= 0) t = t.substring(0, x);</b></new>           <new><b>s += t;</b></new>        <new><b>}</b></new>        <new><b>return s;</b></new>      <new><b>}</b></new>    <new><b>}</b></new><b> // AdapterNode</b>} // DomEcho </pre>  </blockquote><p><new></new><new></new>This class declares a variable to hold the DOM node,   and requires it to be specified as a constructor argument. It then defines the   <code>toString</code> operation, which returns the node type from the String   array, and then adds to that additional information from the node, to further   identify it.</p><p>As you can see in the table of node types in <a href="../../api/org/w3c/dom/Node.html">org.w3c.dom.Node</a>,   every node has a type, and name, and a value, which may or may not be empty.   In those cases where the node name starts with &quot;#&quot;, that field duplicates   the node type, so there is in point in including it. That explains the lines   that read:</p><blockquote>   <pre><new>if (! nodeName.startsWith("#")) {</new>    <new>s += ": " + nodeName;</new><new>}</new> </pre></blockquote><p>The remainder of the <tt>toString</tt> method deserves a couple of notes, as   well. For instance, these lines:</p><blockquote>   <pre><new>if (s.startsWith("ProcInstr")) </new>    <new>s += ", "; </new><new>else </new>    <new>s += ": ";</new></pre> </blockquote><p>Merely provide a little &quot;syntactic sugar&quot;. The type field for a Processing   Instructions end with a colon (:) anyway, so those codes keep from doubling   the colon.</p><p>The other interesting lines are:</p><blockquote>   <pre><new></new><new>String t = domNode.getNodeValue().trim();</new><new>int x = t.indexOf("\n");</new><new>if (x &gt;= 0) t = t.substring(0, x);</new><new>s += t;</new></pre></blockquote><p>Those lines trim the value field down to the first newline (linefeed) character   in the field. If you leave those lines out, you will see some funny characters   (square boxes, typically) in the JTree. </p><blockquote>   <p><b>Note:</b><br>    Recall that XML stipulates that all line endings are normalized to newlines, 

⌨️ 快捷键说明

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