我需要将 a 绑定Hashmap
到 aMutableTreeNode
以便我可以在 a 中显示它JTree
。
我有以下代码:
static Map<String, String> form = new LinkedHashMap<String,String>();
我想这就是我需要实现的方式MutableTreeNode
。我只是不知道现在如何让“表单”的密钥出现在MutableTreeNode
.
public class MyNode implements MutableTreeNode {
@Override
public Enumeration children() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean getAllowsChildren() {
// TODO Auto-generated method stub
return false;
}
@Override
public TreeNode getChildAt(int childIndex) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getChildCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getIndex(TreeNode node) {
// TODO Auto-generated method stub
return 0;
}
@Override
public TreeNode getParent() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isLeaf() {
// TODO Auto-generated method stub
return false;
}
@Override
public void insert(MutableTreeNode arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void remove(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void remove(MutableTreeNode arg0) {
// TODO Auto-generated method stub
}
@Override
public void removeFromParent() {
// TODO Auto-generated method stub
}
@Override
public void setParent(MutableTreeNode arg0) {
// TODO Auto-generated method stub
}
@Override
public void setUserObject(Object arg0) {
// TODO Auto-generated method stub
}
在MutableTreeNode
正确显示正确的Key
值后,我希望能够做到:
final JTree tree = new JTree();
final DefaultMutableTreeNode CustomNode = new DefaultMutableTreeNode("Custom");
DefaultTreeModel tm = new DefaultTreeModel(CustomNode);
tree.setModel(tm);
for (String str : form.keySet())
{
MyNode nod = new MyNode(str);
CustomNode.add(node);
}
tm.reload();