Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, in order to make a solution for both cases (JList and JTable) I made a custom listener that will be applied to the lists and table in question.</p> <p>The listener is :</p> <pre><code>public class DeselectOnClickListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); clearselection(e); } @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); clearselection(e); } @Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); clearselection(e); } public void clearselection (MouseEvent e){ if (e.getComponent() instanceof JTable){ Point pClicked = e.getPoint(); JTable table = (JTable) e.getSource(); int index = table.rowAtPoint(pClicked); if (index == -1){ table.clearSelection(); } } else if (e.getComponent() instanceof JList){ Point pClicked = e.getPoint(); JList&lt;?&gt; list = (JList&lt;?&gt;) e.getSource(); int index = list.locationToIndex(pClicked); Rectangle rec = list.getCellBounds(index, index); if (rec==null || !rec.contains(pClicked)){ list.clearSelection(); } } } } </code></pre> <p>Notice that this works well in <code>SINGLE_SELECCTION</code>, but if we want to use <code>MULTIPLE_INTERVAL_SELECTION</code> or <code>SINGLE_INTERVAL_SELECTION</code> I would recommend not to override <code>mouseReleased(MouseEvent e)</code> as it will clear selection if the mouse button is released outside the selection range.</p> <p>PD: for this last point I've done the following fix:</p> <pre><code>@Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); if (! isIntervalSelectionMode(e)){ clearselection(e); } } public boolean isIntervalSelectionMode (MouseEvent e){ boolean isIntervalSelection = false; if (e.getComponent() instanceof JTable){ isIntervalSelection = ((JTable)e.getSource()).getSelectionModel().getSelectionMode() != ListSelectionModel.SINGLE_SELECTION; }else if (e.getComponent() instanceof JList){ isIntervalSelection = ((JList&lt;?&gt;)e.getSource()).getSelectionModel().getSelectionMode() != ListSelectionModel.SINGLE_SELECTION; } return isIntervalSelection; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload