如何在java jtree中动态地(从列表中)添加节点到我的父节点?

时间:2016-02-04 21:20:21

标签: java swing jtree

之前我问过这个问题,但我觉得我没有提供太多的代码来解释我的问题。

这就是我想要做的事情:

我有一个名为User的对象列表。每个对象包含两个字段 - usernamepassword。我创建了一个用这样的用户名构建jtree的方法:

public class UserTreeBuilder implements TreeWillExpandListener {
    private JTree userTree;
    DefaultMutableTreeNode dummyParent;

    public JTree build(List<User> list){
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("");
        createNodes(top, list);
        userTree = new JTree(top);
        userTree.getSelectionModel().setSelectionMode
            (TreeSelectionModel.SINGLE_TREE_SELECTION);
        userTree.setRootVisible(false);
        userTree.addTreeWillExpandListener(this);

        return userTree;
    }

    public void createNodes(DefaultMutableTreeNode top, List<User> list) {
        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode user = null;

        category = new DefaultMutableTreeNode("User");
        top.add(category);


        for(User ui : list){
            user = new DefaultMutableTreeNode(ui.getUserName());
            category.add(user);
        }
    }

    @Override
    public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) userTree.getLastSelectedPathComponent();

        String nodeName = node.toString();
        System.out.println("NODE TO STRING: "+nodeName);

        if (node == null)
            //Nothing is selected.      
        return;

    }

    @Override
    public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

效果很好,我将列表视为:

(folder icon) users
    (dot icon) user1
    (dot icon) user2
    (dot icon) user3
    etc.

因此,帐户user1user2等是最低的节点。

我有一个方法可以获取用户名作为参数,并返回属于该用户的事物列表。它看起来像这样:

public ArrayList<Things> fetchThings(User user){
    List<Things> list = new ArrayList<Things>();
   .
   .
   .
return list;

我的问题是 - 当有人点击原始jtree上的给定用户然后将这些内容显示为分配给该用户的子节点时,如何为特定用户获取内容(调用我的方法fetchThings)?

例如,有人点击了user2,这就是他所看到的:

(folder icon) users
    (folder icon) user1
    (folder icon) user2
        (dot icon) thing1
        (dot icon) thing2
        (dot icon) thing3
        .
        .
        .
    (folder icon) user3
  etc.

我尝试使用此页面https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html中的孩子延迟加载,但我无法使其正常工作。你能帮我解决一下我的实施吗?

1 个答案:

答案 0 :(得分:5)

如果您已阅读JTree JavaDocs,您就会知道可以通过使用类似的内容获取被点击的节点。

MouseListener ml = new MouseAdapter() {
     public void mousePressed(MouseEvent e) {
         int selRow = tree.getRowForLocation(e.getX(), e.getY());
         TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
         if(selRow != -1) {
             if(e.getClickCount() == 1) {
                 mySingleClick(selRow, selPath);
             }
             else if(e.getClickCount() == 2) {
                 myDoubleClick(selRow, selPath);
             }
         }
     }
 };

您可以使用TreePathgetLastPathComponent,这将返回Object,在您的情况下,您可以将其投放到DefaultMutableTreeNode

如果您已阅读TreeWillExpandListenerTreeeExpansionEventTreePath的JavaDoc,您将知道在调用treeWillExpand时,您可以获得将要扩展的节点使用TreeExpansionEvent#getPath#getLastPathComponent,类似......

@Override
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();
    System.out.println(treeNode);
}

从那里,您可以简单地调用您需要的任何功能,以获取填充问题DefaultMutableTreeNode所需的信息

您的user DefaultMutableTreeNode需要覆盖isLeaf方法并返回false,这将允许节点“可扩展”,类似于...... / p>

for (User ui : list) {
    user = new DefaultMutableTreeNode(ui.getUserName()) {
        @Override
        public boolean isLeaf() {
            return false;
        }
    };
    category.add(user);
}