-1

I have a JTable with MULTIPLE_INTERVAL_SELECTION. I need the CONTROL+CLICK to select addition rows. In my LAF, this does not happen automatically. I wrote the following code that uses a mouse listener:

addMouseListener(new MouseAdapter()
{
    @Override
    public void mousePressed(MouseEvent event)
    {
        ListSelectionModel listSelectionModel = getSelectionModel();

        if (listSelectionModel.getSelectionMode() ==
                ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
        {
            if (event.isControlDown())
            {
                int rowView  = rowAtPoint(event.getPoint());

                if (isRowSelected(rowView))
                {
                    System.out.println("rowView already selected");

                }

                listSelectionModel.addSelectionInterval(rowView, rowView);
            }
        }
    }
});

The problem is that the row selection event happens before the mouse listener is entered. The row that was clicked on is selected, but the previous selections were cleared.

My questions are:

How do I capture the selection event to by-pass automatic selection?

Could I capture that event to stop it from clearing the previous selections, use getModifiers() to see if the CONTROL key was pressed, and call addSelectionInterval?

I need assistance identifying and setting up a listener to do this.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kit
  • 19
  • 4
  • note ListSelectionModel in only 2D array, there can be limits without portions of customized code, then you have to play with getSelectedIndices().length and get - setAnchor / LeadSelectionIndex for example [code by](http://stackoverflow.com/a/10793911/714968) @aterai or [this one](http://stackoverflow.com/a/7620693/714968) – mKorbel May 05 '17 at 17:29
  • You probably need to [consume](http://docs.oracle.com/javase/8/docs/api/java/awt/event/InputEvent.html#consume--) the MouseEvent when Control is down, so JTable doesn’t try to interpret it. Be aware that Control-clicking should toggle a row’s selection, not just unilaterally select the row; that is how every look-and-feel I know of works. – VGR May 05 '17 at 17:44
  • I just need to have multiple selections. If one row is selected and Control click is pressed on another row, I need both rows selected and highlighted. – Kit May 08 '17 at 16:37

1 Answers1

-1

One of my mouse listeners was clearing the selections which made it appear that the control+click was not working. Thanks for reviewing my issue.

Kit
  • 19
  • 4