【问题标题】:Adding Child Nodes to the SelectionPaths[]将子节点添加到 SelectionPaths[]
【发布时间】:2016-10-31 05:56:29
【问题描述】:

我有一棵显示网络设备的树。过滤树,以便按位置和设备类型显示设备。例如,节点“Office”将具有子节点“计算机”和“打印机”,每个节点下都有设备。

当我选择位置节点时,我想选择它下面的所有节点并将其添加到我的树的 selectionPaths[] 中,以便它们都被突出显示。下面是我的代码,但我不知道如何让它工作。

   private class DefaultDeviceTreeCellRenderer extends DefaultTreeCellRenderer {

    private JLabel label;

    DefaultDeviceTreeCellRenderer() {
        super();
        label = new JLabel();
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        Object o = node.getUserObject();
        if (o instanceof DefaultDevice) {
            DefaultDevice defaultDevice = (DefaultDevice) o;
            label.setIcon(defaultDevice.getIcon());
            label.setText(defaultDevice.toString());
        } else {
            System.out.println("Cell Class= " + o.getClass());
            //Set all children of this Component as selected. 
            label.setIcon(null);
            label.setText("" + value);
            TreePath[] selectionPaths = tree.getSelectionPaths();
            if (selectionPaths == null) {
                System.out.println("Nothing Selected. ");
            } else {
                //keep current selection path
                //if there are leaf nodes, add their selection paths. 
                int i = 0;
                while (i < node.getChildCount()) {
                    //add node.getNextLeaf().getPath() to selection Paths
                    ArrayList<TreePath> arrayList = new ArrayList<>(Arrays.asList(selectionPaths));
                    arrayList.add(new TreePath(((DefaultMutableTreeNode)node.getChildAt(i)).getPath()));
                    TreePath[] toArray = arrayList.toArray(new TreePath[arrayList.size()]);
                    this.printSelectionPath(selectionPaths);
                    this.printSelectionPath(toArray);
                    // tree.setSelectionPaths(toArray);
                    i++;
                }
            }
            //  System.out.println("Selection Paths.size="+selectionPaths.length);
            // ArrayList arrayList = new ArrayList(Arrays.asList(selectionPaths));
            //  System.out.println("ChildNode Path=" + node.getPath()[a]);

        }
        label.setOpaque(true);
        if (selected) {

            label.setBackground(this.backgroundSelectionColor);
            label.setForeground(this.textSelectionColor);
        } else {
            label.setBackground(this.backgroundNonSelectionColor);
            label.setForeground(this.textNonSelectionColor);
        }
        return label;
    }

    private void printSelectionPath(TreePath[] selectionPaths) {
        System.out.println("\nTreePath:");
        for (int i = 0; i < selectionPaths.length; i++) {
            System.out.println("Selection Path= " + selectionPaths[i]);
            //TreePath treePath = new TreePath(node.getPath());
           // System.out.println("TreePath= " + treePath);
        }
}

【问题讨论】:

  • 你真的应该创建一个自定义 TreeSelectionModel:docs.oracle.com/javase/7/docs/api/javax/swing/tree/…
  • 你能提供一个例子来完成这个吗?
  • I would like to select all nodes under it and add it to the selectionPaths[] of my tree so that they are all highlighted 您确定要通过更改 SelectionModel 来更改视图吗?根据父“位置”节点是否存在并被选中来更改视图可能会更容易。
  • 我一直在绞尽脑汁想办法做到这一点。基本上,如果我单击“DefaultDevice”以外的任何内容,那么它将是“Location”或“Model”节点。如果我单击其中任何一个,我希望它们下的所有节点都突出显示。
  • 我考虑从 selectionListener 更新 SelectionModel 但这似乎不是正确的方法。我一直在其他应用程序中看到这种行为。它是如何实现的?

标签: java swing treecellrenderer


【解决方案1】:

我通过扩展 DefaultTreeSelectionModel 并修改了一些方法来实现这一点。这是我的扩展课。

    public static class MyTreeSelectionModel extends DefaultTreeSelectionModel {

    public MyTreeSelectionModel() {
        super();
    }

    @Override
    public void removeSelectionPath(TreePath path) {
        //remove path and its children
        Object lastPathComponent = path.getLastPathComponent();
        ArrayList<TreePath> childPaths = new ArrayList();
        if (lastPathComponent instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode dn = (DefaultMutableTreeNode) lastPathComponent;
            this.getDecendents(dn, childPaths);
        } else {
            System.out.println("Not a DefaultMutableTreeNode");
        }
        childPaths.add(path);
        this.removeSelectionPaths(childPaths.toArray(new TreePath[childPaths.size()]));
    }

    @Override
    public void addSelectionPath(TreePath path) {
        //add the path and its children
        Object lastPathComponent = path.getLastPathComponent();
        ArrayList<TreePath> childPaths = new ArrayList();
        if (lastPathComponent instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode dn = (DefaultMutableTreeNode) lastPathComponent;
            System.out.println("Last Path Component= " + dn);
            //get decendents
            this.getDecendents(dn, childPaths);
        } else {
            System.out.println("Not a DefaultMutableTreeNode");
        }
        childPaths.add(path);
        this.addSelectionPaths(childPaths.toArray(new TreePath[childPaths.size()]));
        // super.addSelectionPath(path); //To change body of generated methods, choose Tools | Templates.
    }

    private void getDecendents(DefaultMutableTreeNode dn, ArrayList<TreePath> childPaths) {
        Enumeration children = dn.children();
        if (children != null) {
            while (children.hasMoreElements()) {
                DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
                System.out.println("Adding " + childNode);
                TreeNode[] path1 = childNode.getPath();
                childPaths.add(new TreePath(path1));
                getDecendents(childNode, childPaths);
            }
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多