WTK提供的用户界面构件实在是太不给力了,应付从前的小屏幕还将就能用,现在手机的大屏能显示丰富的信息,大量复杂的层次信息常常需要使用TREEVIEW,既然没有现成的,就只好自己做一个。
以下代码是我从网上搜来的,然后根据自己的需要作了一番改造。
节点类,节点当中有一个存取子节点的向量,以及对子节点操作的一组方法
import java.util.Vector;
public class TreeNode {
public int x = 0;
public int y = 0;
public TreeNode parentNode = null;
public String label = null;
public String value = null;
public boolean expanded = false;
public String iconName = "";
public int index = 0;
Vector children = null;
public MenuNode(String label) {
this.label = label;
this.value = label;
this.children = new Vector();
}
public MenuNode(String label, String value) {
this.label = label;
this.value = value;
this.children = new Vector();
}
public void appendChild(MenuNode node) {
node.parentNode = this;
node.index = this.children.size();
this.children.addElement(node);
}
public int getChildrenNum() {
return this.children.size();
}
public MenuNode getNextSibling() {
if (parentNode != null) {
return parentNode.getChild(index + 1);
}
return null;
}
public MenuNode getPrevSibling() {
if (parentNode != null) {
return parentNode.getChild(index - 1);
}
return null;
}
public MenuNode getLastChild() {
return getChild(getChildrenNum() - 1);
}
public MenuNode getChild(int i) {
if (i >= 0 && i < this.children.size()) {
return (MenuNode) this.children.elementAt(i);
}
return null;
}
public void removeChildren(int index) {
this.children.removeElementAt(index);
for (int i = index; i < this.children.size(); i++) {
this.getChild(i).index--;
}
}
public boolean hasChildren() {
return this.getChildrenNum() > 0;
}
public void expand() {
if (this.getChildrenNum() > 0) {
this.expanded = true;
}
}
public void collapse() {
this.expanded = false;
}
}