JTree select node by clicking anywhere on the row
I have code [taken from here][1] that would allow selection of a JTree Row by clicking anywhere on the row.  it works fine in single row selection mode.   However, I am not sure how to modify it in order to handle multiple row selections.    how do I distinguish the case when user is make a multiple selection(eg. by holding down the shift or control button while making a left mouse click on a row)?

<pre>
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;


@SuppressWarnings(&quot;serial&quot;)
public class NavTree extends JTree {

	private boolean					fWholeRowSelectionEnabled;
	private MouseListener			fRowSelectionListener;
	final NavTree					fThis;
	
	public NavTree(TreeNode rootNode) {
		super(rootNode);
		fThis = this;
		init();
	}
	public NavTree() {
		fThis = this;
		init();
	}
	
	private void init() {
		//setCellRenderer(new NavTreeCellRenderer());
		fRowSelectionListener = new MouseAdapter() {

			public void mousePressed(MouseEvent e) {
				if (SwingUtilities.isLeftMouseButton(e)) {
					int closestRow = fThis.getClosestRowForLocation(
							e.getX(), e.getY());
					Rectangle closestRowBounds = fThis.getRowBounds(closestRow);
					if(e.getY() &gt;= closestRowBounds.getY() &amp;&amp; 
							e.getY() &lt; closestRowBounds.getY() + 
							closestRowBounds.getHeight()) {
						if(e.getX() &gt; closestRowBounds.getX() &amp;&amp; 
								closestRow &lt; fThis.getRowCount()){
							
                                                    fThis.setSelectionRow(closestRow);
                                                }
                                                
					} else
						fThis.setSelectionRow(-1);
				}
			}

		};
		setWholeRowSelectionEnabled(true);
	}
	
	public void setWholeRowSelectionEnabled(boolean wholeRowSelectionEnabled) {
		fWholeRowSelectionEnabled = wholeRowSelectionEnabled;
		if (fWholeRowSelectionEnabled)
			addMouseListener(fRowSelectionListener);
		else
			removeMouseListener(fRowSelectionListener);
	}

	public boolean isWholeRowSelectionEnabled() {
		return fWholeRowSelectionEnabled;
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		DefaultMutableTreeNode root = new DefaultMutableTreeNode(&quot;Root&quot;);
		root.add(new DefaultMutableTreeNode(&quot;Child 1&quot;));
		root.add(new DefaultMutableTreeNode(&quot;Child 2&quot;));
		root.add(new DefaultMutableTreeNode(&quot;Child 3&quot;));
		NavTree tree = new NavTree(root);
		frame.add(tree);
		frame.setSize(200, 300);
		frame.setVisible(true);
	}
}
</pre>


  [1]: http://www.coderanch.com/t/461207/GUI/java/JTree-row-selection-mouseclick
JTree select node by clicking anywhere on the row
===

I have code [taken from here][1] that would allow selection of a JTree Row by clicking anywhere on the row.  it works fine in single row selection mode.   However, I am not sure how to modify it in order to handle multiple row selections.    how do I distinguish the case when user is make a multiple selection(eg. by holding down the shift or control button while making a left mouse click on a row)?

<pre>
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;


@SuppressWarnings(&quot;serial&quot;)
public class NavTree extends JTree {

	private boolean					fWholeRowSelectionEnabled;
	private MouseListener			fRowSelectionListener;
	final NavTree					fThis;
	
	public NavTree(TreeNode rootNode) {
		super(rootNode);
		fThis = this;
		init();
	}
	public NavTree() {
		fThis = this;
		init();
	}
	
	private void init() {
		//setCellRenderer(new NavTreeCellRenderer());
		fRowSelectionListener = new MouseAdapter() {

			public void mousePressed(MouseEvent e) {
				if (SwingUtilities.isLeftMouseButton(e)) {
					int closestRow = fThis.getClosestRowForLocation(
							e.getX(), e.getY());
					Rectangle closestRowBounds = fThis.getRowBounds(closestRow);
					if(e.getY() &gt;= closestRowBounds.getY() &amp;&amp; 
							e.getY() &lt; closestRowBounds.getY() + 
							closestRowBounds.getHeight()) {
						if(e.getX() &gt; closestRowBounds.getX() &amp;&amp; 
								closestRow &lt; fThis.getRowCount()){
							
                                                    fThis.setSelectionRow(closestRow);
                                                }
                                                
					} else
						fThis.setSelectionRow(-1);
				}
			}

		};
		setWholeRowSelectionEnabled(true);
	}
	
	public void setWholeRowSelectionEnabled(boolean wholeRowSelectionEnabled) {
		fWholeRowSelectionEnabled = wholeRowSelectionEnabled;
		if (fWholeRowSelectionEnabled)
			addMouseListener(fRowSelectionListener);
		else
			removeMouseListener(fRowSelectionListener);
	}

	public boolean isWholeRowSelectionEnabled() {
		return fWholeRowSelectionEnabled;
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		DefaultMutableTreeNode root = new DefaultMutableTreeNode(&quot;Root&quot;);
		root.add(new DefaultMutableTreeNode(&quot;Child 1&quot;));
		root.add(new DefaultMutableTreeNode(&quot;Child 2&quot;));
		root.add(new DefaultMutableTreeNode(&quot;Child 3&quot;));
		NavTree tree = new NavTree(root);
		frame.add(tree);
		frame.setSize(200, 300);
		frame.setVisible(true);
	}
}
</pre>


  [1]: http://www.coderanch.com/t/461207/GUI/java/JTree-row-selection-mouseclick