简体   繁体   English

具有鼠标事件的JTree选择节点

[英]JTree select node with mouse event

I'm trying to track the node the user clicks on with a mouse listener on a JTree. 我试图跟踪用户在JTree上使用鼠标侦听器单击的节点。 The click event works, but I'm not able to select the node in the tree. click事件有效,但是我无法选择树中的节点。

public FileTreeController(JTree t) {
    this.myTree = t;
    this.myTree.setCellRenderer(new FileTreeCellRenderer());

    this.myTree.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            doMouseClicked(me);
        }
    });

    this.renderTree();
}

private void doMouseClicked(MouseEvent me) {
    int selRow = this.myTree.getRowForLocation(me.getX(), me.getY());
    TreePath selPath = this.myTree.getPathForLocation(me.getX(), me.getY());
    if (selRow != -1) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) selPath.getLastPathComponent());
        TreeNode selectedTreeNode = (TreeNode) selectedNode.getUserObject();

        //Doesn't work
        this.myTree.getSelectionModel().addSelectionPath(selPath);
        this.myTree.setSelectionRow(selRow);

        if (SwingUtilities.isLeftMouseButton(me)) {
            if (me.getClickCount() == 1) {

            } else if (me.getClickCount() == 2) {
                System.out.println(selectedTreeNode.getText());
            }
        } else if (SwingUtilities.isRightMouseButton(me)) {
            if (me.getClickCount() == 1) {
                System.out.println("Right");
            }
        }
    }
}

Here's my own class which is represented in the JTree and which contains all the information. 这是我自己的类,它在JTree中表示,并且包含所有信息。

class TreeNode {
    private String text = "";
    private String icon = "";
    private String path = "";

    public TreeNode(String txt, String iconpath, String path) {
        this.text = txt;
        this.icon = iconpath;
        this.path = path;
    }

    public TreeNode(String txt, IconType iconpath, String path) {
        this.text = txt;
        this.icon = iconpath.toString();
        this.path = path;
    }

    public String getText() {
        return this.text;
    }

    public String getIcon() {
        return Validator.validatePath(this.icon);
    }

    public String getPath(){
        return Validator.validatePath(this.path);
    }
}

class FileTreeCellRenderer implements TreeCellRenderer {
    private JLabel label;

    FileTreeCellRenderer() {
        label = new JLabel();
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        Object o = ((DefaultMutableTreeNode) value).getUserObject();
        if (o instanceof TreeNode) {
            TreeNode treeNode = (TreeNode) o;
            label.setIcon(new ImageIcon(treeNode.getIcon()));
            label.setText(treeNode.getText());
        } else {
            label.setIcon(null);
            label.setText("" + value);
        }
        return label;
    }
}

Seems you don't see your selection because you don't implement color change in your FileTreeCellRenderer : 似乎没有看到选择,因为您没有在FileTreeCellRenderer实现颜色更改:

  1. Change constructor like next: 像下面那样更改构造函数:

     FileTreeCellRenderer() { label = new JLabel(); label.setOpaque(true); } 
  2. In getTreeCellRendererComponent() change color like next: getTreeCellRendererComponent()更改颜色,如下所示:

     label.setBackground(selected ? Color.BLUE : tree.getBackground()); label.setForeground(selected ? Color.WHITE : tree.getForeground()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM