0
我已經使用XSOM API(https://xsom.dev.java.net)從XML模式文件加載了帶有節點的JTree。JTree的選擇路徑
文件選擇我做的如下因素每當:
schemaParser = new XSDParser(selectedFile.getAbsolutePath());
TreeModel model = schemaParser.generateTreeModel();
schemaTree.setModel(model);
schemaTree.setCellRenderer(new SchemaTreeTraverser.SchemaTreeCellRenderer());
schemaTree是JTree的變量名。
爲XSDParser的代碼如下:
package schemaparser;
import java.io.*;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.impl.util.SchemaTreeTraverser;
import com.sun.xml.xsom.impl.util.SchemaWriter;
import com.sun.xml.xsom.parser.XSOMParser;
import javax.swing.tree.TreeModel;
public class XSDParser {
private XSOMParser reader;
private XSSchemaSet xss;
public XSDParser(String parseFile){
try {
reader = new XSOMParser();
reader.parse(new File(parseFile));
xss = reader.getResult();
if (xss == null) {
System.out.println("error");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public TreeModel generateTreeModel() {
SchemaTreeTraverser stt = new SchemaTreeTraverser();
stt.visit(xss);
TreeModel model = stt.getModel();
System.out.println("Returning the tree model");
return model;
}
}
現在只要一個節點在JTree的選擇我做到以下幾點:
private void schemaTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
TreePath path = schemaTree.getSelectionPath();
if(path != null)
System.out.println(path.toString());
}
但是現在無論我在選擇哪個節點樹我得到像這樣的東西:[null,null,null,null](取決於我的層次結構有多遠)
對於簡單的JTree,通常會打印出節點的路徑,例如: [JTree,顏色,紅色]
任何任何想法如何解決這個問題?
感謝, 帕特里克
您需要提供有關XSDParser和SchemaTreeTraverser.SchemaTreeCellRenderer的更多信息。 – Clint 2009-04-17 16:17:45