JAVA swing中的JTreeModel中的帮助 [英] Help in JTreeModel in JAVA swing

查看:188
本文介绍了JAVA swing中的JTreeModel中的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树模型,可以在Jtree中预览XML文件

但它会在每个节点旁边写入节点名称和NULL值

我只希望出现节点的名称.
这是我的代码:

I have a tree model which preview XML file in Jtree

but it writes the name of nodes and NULL value beside each node

I want only the name of node appears.
this is my code:

public class XmlTreeDemo extends JFrame {
XmlTreeDemo(String title){
super(title);
try{
DocumentBuilderFactory IDocumentBuilderFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder IDocumentBuilder
= IDocumentBuilderFactory.newDocumentBuilder();
Document IDocument = IDocumentBuilder.parse("SRS_instance.xml");
Node root = IDocument.getDocumentElement();
XmlTreeModel model = new XmlTreeModel(root);
JTree IJTree = new JTree();
IJTree.setModel(model);
getContentPane().add(new JScrollPane(IJTree),BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (Exception e){
System.err.println(e);
}
}
public static void main(String[] args){
XmlTreeDemo IJTreeDemo = new XmlTreeDemo("Xml tree demo");
IJTreeDemo.pack();
IJTreeDemo.setVisible(true);
}
}
class XmlTreeModel implements TreeModel{
protected Node root;
public XmlTreeModel(Node root){
this.root = root;
}
public Object getRoot(){
return (Object)this.root;
}
public boolean isLeaf(Object node){
if ((((Node)node).getNodeType() == 7) || (((Node)node).getNodeType()
== 1)) return false;
return true;
}
public int getChildCount(Object parent){
return ((Node)parent).getChildNodes().getLength();
}
public Object getChild(Object parent,int index){
Node child = ((Node)parent).getChildNodes().item(index);
return (Object)child;
}
public int getIndexOfChild(Object parent, Object child){
NodeList childs = ((Node)parent).getChildNodes();
if (childs.getLength() == 0) return -1;
for (int i=0; i<childs.getLength(); i++){
if (childs.item(i) == (Node)child) return i;
}
return -1;
}
public void valueForPathChanged(TreePath path, Object newValue){
}
public void addTreeModelListener(TreeModelListener l){
}
public void removeTreeModelListener(TreeModelListener l){
}


plz Help =(


plz Help =(

推荐答案

"null"值最有可能出现在您要查看的文本旁边,因为默认情况下(至少根据我的了解) ,JTree的树单元格渲染器从对象的``toString()''方法获取要打印/显示的值.

在获得文档的根目录后,您可以通过一个简单的"println"来注意到这一点:
The ''null'' value most likely appears besides the text you want to see because, by default (at least from what I know), the JTree''s tree cell renderer gets the value to be printed/shown from the object''s ''toString()'' method.

You could notice this by a simple ''println'' after you get the root of the document:
Node root = IDocument.getDocumentElement();
System.out.println("Root node displayed value: "+root.toString());



因此,应该显示空值.

一个明显的解决方案是重写对象的toString()方法,但这并不总是很方便,尤其是因为您使用的不是您自己创建的类. (``节点'')

因此,您可能会选择的另一种选择是通过构建实现接口TreeCellRenderer的类来覆盖JTree的TreeCellRenderer,然后将其实例附加到与setCellRenderer()一起使用的JTree对象上.方法.

例如:



And, thus, the null value should appear.

An obvious solution would be to override the object''s toString() method, but that''s not always quite convenient, especially since you are using a class not made by you. ( ''Node'' )

So, another option you might go for might be to override the JTree''s TreeCellRenderer by building a class that implements the interface TreeCellRenderer and then attach an instance of it to the JTree object you are using with the setCellRenderer() method.

For example:

public class CustomTreeCellRenderer implements TreeCellRenderer {
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

		Component returnValue = null;

		// if the value is a valid object for our purposes
		if (value != null && value instanceof Node) {
			// for no real reason, I decided to use a JLabel, but any class that extends Component will do
			JLabel thisLabel;
			
			// if the node is a leaf, print the value
			if (((Node) value).getNodeType() != 7 && ((Node) value).getNodeType() != 1)
				thisLabel = new JLabel(((Node) value).getNodeValue());
			// else, print the name
			else
				thisLabel = new JLabel(((Node) value).getNodeName());
			
			// make the label the return value
			returnValue = thisLabel;
		} else {
			// else, do what the JTree would have done with the default TreeCellRenderer
			DefaultTreeCellRenderer thisDefaultRenderer = new DefaultTreeCellRenderer();
			returnValue = thisDefaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		}

		// return the value
		return returnValue;
	}
}



然后您可以像这样使用此类:



Then you can use this class like so:

// create the renderer
CustomTreeCellRenderer newRenderer = new CustomTreeCellRenderer();

// what you did earlier
JTree IJTree = new JTree();
IJTree.setModel(model);

// attach it to the JTree
IJTree.setCellRenderer(newRenderer);



当然,这只是一个例子.您可以根据自己的需要定制最终课程.

希望对您有所帮助.
干杯!



Of course, this is just an example. You can tailor the end class to your needs.

I hope this helps.
Cheers!


这篇关于JAVA swing中的JTreeModel中的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆