2

我有一个 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();
    }

}
4

2 回答 2

2

以下行

tree.setSelectionPath(path);

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

if (tree.isSelectionEmpty()) {
    tree.setSelectionPath(path);
}
于 2013-06-05T14:02:42.903 回答
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();
        }

    }
于 2013-06-13T07:31:19.767 回答