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

时间:2015-02-05 09:45:52

标签: java swing jtree mouselistener

我试图通过JTree上的鼠标侦听器跟踪用户点击的节点。 点击事件有效,但我无法在树中选择节点。

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");
            }
        }
    }
}

这是我自己的类,它在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;
    }
}

1 个答案:

答案 0 :(得分:1)

似乎您没有看到您的选择,因为您未在FileTreeCellRenderer中实施颜色更改:

  1. 更改下一个构造函数:

    FileTreeCellRenderer() {
        label = new JLabel();
        label.setOpaque(true);
    }
    
  2. getTreeCellRendererComponent()改变颜色如下:

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