右键单击jtree只选择一个节点

时间:2013-06-05 12:51:39

标签: java swing jtree selected right-click

我有一个JTree,我可以(ctrl)选择多个节点。 当我右键单击时,我会弹出一个可以选择“刷新”的弹出窗口。 (本网站还有其他问题解释如何执行此操作)

问题是,当我选择多个节点并且我右键单击时,只有我右键单击的节点被选中而其他节点被取消选择。

我想选择例如3个节点(叶子),右键单击,选择“刷新”并仍然选择这3个节点。

有什么建议吗?谢谢!

示例:

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JTreeMultiSelect extends JFrame{

    public JTreeMultiSelect() {
        super("Test");

        JTree myTree = new JTree();
        myTree.getSelectionModel()
                .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        // add MouseListener to tree
        MouseAdapter ma = new MouseAdapter() {
            private void myPopupEvent(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                JTree tree = (JTree)e.getSource();
                TreePath path = tree.getPathForLocation(x, y);
                if (path == null)
                    return;

                tree.setSelectionPath(path);

                DefaultMutableTreeNode rightClickedNode =
                        (DefaultMutableTreeNode)path.getLastPathComponent();

                if(rightClickedNode.isLeaf()){
                    JPopupMenu popup = new JPopupMenu();
                    final JMenuItem refreshMenuItem = new JMenuItem("refresh");
                    refreshMenuItem.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            System.out.println("refresh!");
                        }
                    });
                    popup.add(refreshMenuItem);
                    popup.show(tree, x, y);
                }
            }
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) myPopupEvent(e);
            }
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) myPopupEvent(e);
            }
        };

        myTree.addMouseListener(ma);

        JPanel myPanel = new JPanel();
        myPanel.add(myTree);
        this.add(myPanel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);

    }

    public static void main(String[] args) {
        new JTreeMultiSelect();
    }

}

2 个答案:

答案 0 :(得分:2)

以下一行

tree.setSelectionPath(path);

将您的树选择重置为单个项目。您可能希望删除此行以获得所需的行为,或者甚至更好地将其置于处理无选择案例的条件中:

if (tree.isSelectionEmpty()) {
    tree.setSelectionPath(path);
}

答案 1 :(得分:2)

以下是评论中承诺的工作示例:

    import javax.swing.*;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreePath;
    import javax.swing.tree.TreeSelectionModel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;

    public class JTreeMultiSelect extends JFrame{

        public JTreeMultiSelect() {
            super("Test");

            JTree myTree = new JTree();
            myTree.getSelectionModel()
                    .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
            // add MouseListener to tree
            MouseAdapter ma = new MouseAdapter() {
                private void myPopupEvent(MouseEvent e) {
                    int x = e.getX();
                    int y = e.getY();
                    JTree tree = (JTree)e.getSource();
                    TreePath path = tree.getPathForLocation(x, y);
                    if (path == null)
                        return;

                    DefaultMutableTreeNode rightClickedNode =
                            (DefaultMutableTreeNode)path.getLastPathComponent();

                    TreePath[] selectionPaths = tree.getSelectionPaths();

                    //check if node was selected
                    boolean isSelected = false;
                    if (selectionPaths != null) {
                        for (TreePath selectionPath : selectionPaths) {
                            if (selectionPath.equals(path)) {
                                isSelected = true;
                            }
                        }
                    }
                    //if clicked node was not selected, select it
                    if(!isSelected){
                        tree.setSelectionPath(path);
                    }

                    //todo : create custom JMenuItem that takes the selectionPaths as parameter
                    //       and do the action for each node (using getLastPathComponent)
                    if(rightClickedNode.isLeaf()){
                        JPopupMenu popup = new JPopupMenu();
                        final JMenuItem refreshMenuItem = new JMenuItem("refresh");
                        refreshMenuItem.addActionListener(new ActionListener(){
                            @Override
                            public void actionPerformed(ActionEvent actionEvent) {
                                System.out.println("refresh!");
                            }
                        });
                        popup.add(refreshMenuItem);
                        popup.show(tree, x, y);
                    }
                }
                public void mousePressed(MouseEvent e) {
                    if (e.isPopupTrigger()) myPopupEvent(e);
                }
                public void mouseReleased(MouseEvent e) {
                    if (e.isPopupTrigger()) myPopupEvent(e);
                }
            };

            myTree.addMouseListener(ma);

            JPanel myPanel = new JPanel();
            myPanel.add(myTree);
            this.add(myPanel);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.pack();
            this.setVisible(true);

        }

        public static void main(String[] args) {
            new JTreeMultiSelect();
        }

    }