title: JTree select node with mouse event
body: <p>I'm trying to track the node the user clicks on with a mouse listener on a JTree.
  The click event works, but I'm not able to select the node in the tree.</p>
  
  <pre><code>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");
              }
          }
      }
  }
  </code></pre>
  
  <p>Here's my own class which is represented in the JTree and which contains all the information.</p>
  
  <pre><code>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;
      }
  }
  </code></pre>