| | | | 上一页 1 2 3 4 下一页
接下来一步是创建一个叫 buildWelcomeTree()的方法。这个方法一次构建一个DefaultTreeModel变量,而不是通过分析一个现有的XML文字字符串。如果用户没有指定 XML文件就启动这个应用程序,将显示 DefaultTreeModel。见代码段1
代码段1:
private DefaultTreeModel buildWelcomeTree() { DefaultMutableTreeNode root; DefaultMutableTreeNode instructions, openingDoc, editingDoc, savingDoc; DefaultMutableTreeNode openingDocText, editingDocText, savingDocText; DefaultMutableTreeNode development, addingFeatures, contactingKyle;
root = new DefaultMutableTreeNode( "Welcome to XML View 1.0" ); instructions = new DefaultMutableTreeNode( "Instructions" ); openingDoc = new DefaultMutableTreeNode ( "Opening XML Documents" ); openingDocText = new DefaultMutableTreeNode ( "When invoking the XmlEditor from the command-line, you must specify the filename." ); editingDoc = new DefaultMutableTreeNode ( "Editing an XML Document" ); editingDocText = new DefaultMutableTreeNode ( "XML text in the right hand frame can be edited directly. The \"refresh\" button will rebuild the JTree in the left frame." ); savingDoc = new DefaultMutableTreeNode ( "Saving an XML Document" ); savingDocText = new DefaultMutableTreeNode ( "This iteration of the XmlEditor does not provide the ability to save your document. That will come with the next article." ); root.add( instructions ); instructions.add( openingDoc ); instructions.add( editingDoc ); openingDoc.add( openingDocText ); editingDoc.add( editingDocText ); return new DefaultTreeModel( root ); } | 接下来的我们需要添加一个新的构造程序来简化默认显示功能,我们将修改主构造程序,这样它就不能接受任何参数,创建一个新的能接收单一的 XML文本字符串的构造程序。这样以来,如果没有 XML文本被显示的话就会创建默认 XTree对象,而如果 XML文本被显示的话将创建一个唯一的 XTree对象。代码段2中给出了两个构造程序。
代码段2:
public XTree( String text ) throws ParserConfigurationException { this(); refresh( text ); } public XTree() throws ParserConfigurationException { super(); getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); setShowsRootHandles( true ); setEditable( false ); dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating( false ); db = dbf.newDocumentBuilder(); setModel( buildWelcomeTree() ); } |
上一页 1 2 3 4 下一页
【责任编辑:方舟】
| |