Search code examples
javaswingjtree

JTree select node by clicking anywhere on the row


I have code taken from here 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)?

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("serial")
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() >= closestRowBounds.getY() && 
                            e.getY() < closestRowBounds.getY() + 
                            closestRowBounds.getHeight()) {
                        if(e.getX() > closestRowBounds.getX() && 
                                closestRow < 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("Root");
        root.add(new DefaultMutableTreeNode("Child 1"));
        root.add(new DefaultMutableTreeNode("Child 2"));
        root.add(new DefaultMutableTreeNode("Child 3"));
        NavTree tree = new NavTree(root);
        frame.add(tree);
        frame.setSize(200, 300);
        frame.setVisible(true);
    }
}

Solution

  • Use the modifier key information of the MouseEvent. See MouseEvent#getModifiersEx for more information