Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I've implemented it in the JTable.</p> <p>Note that there are several drawbacks to this solution:</p> <ul> <li>You cannot use the <em>table.getListSelectionModel</em> to select rows, you need to call <em>table.addRowSelectionInterval</em></li> <li>trying to select another column next to a selected block will unselect the rows</li> <li>I haven't tested column selection but my guess is that it wouldn't work</li> <li>changing direction when selecting block does not work always</li> </ul> <p>but for the rest it does pretty much what I wanted</p> <pre><code>/* * Copyright 2013 Japplis. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.awt.Point; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; import javax.swing.JTable; import javax.swing.table.TableModel; /** * The JTable used to display data. * This class is only to fix bugs or improve existing functionalities. * * @author Anthony Goubard - Japplis */ public class SheetTable extends JTable { private Map&lt;Integer, Set&lt;Integer&gt;&gt; selectedCells = new HashMap&lt;&gt;(); private Point firstExtendCell; public SheetTable(TableModel tableModel) { super(tableModel); } @Override public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) { if (toggle &amp;&amp; isCellSelected(rowIndex, columnIndex) &amp;&amp; !extend) { selectedCells.get(rowIndex).remove(columnIndex); } else { if (!toggle &amp;&amp; !extend) { selectedCells.clear(); } Set&lt;Integer&gt; selectedColumns = selectedCells.get(rowIndex); if (selectedColumns == null) { selectedColumns = new TreeSet&lt;&gt;(); selectedCells.put(rowIndex, selectedColumns); } selectedColumns.add(columnIndex); if (!extend) { firstExtendCell = new Point(rowIndex, columnIndex); } else { for (int i = Math.min(firstExtendCell.x, rowIndex); i &lt;= Math.max(firstExtendCell.x, rowIndex); i++) { for (int j = Math.min(firstExtendCell.y, columnIndex); j &lt;= Math.max(firstExtendCell.y, columnIndex); j++) { selectedCells.get(i).add(j); } } } } super.changeSelection(rowIndex, columnIndex, toggle, extend); } @Override public void addRowSelectionInterval(int index0, int index1) { for (int i = index0; i &lt; index1; i++) { selectedCells.remove(i); } super.addRowSelectionInterval(index0, index1); } @Override public void removeRowSelectionInterval(int index0, int index1) { for (int i = index0; i &lt; index1; i++) { selectedCells.remove(i); } super.removeRowSelectionInterval(index0, index1); } @Override public void selectAll() { selectedCells.clear(); super.selectAll(); } @Override public void clearSelection() { if (selectedCells != null) { selectedCells.clear(); } super.clearSelection(); } @Override public boolean isCellSelected(int row, int column) { if (!getSelectionModel().isSelectedIndex(row)) { return false; } if (getSelectionModel().isSelectedIndex(row) &amp;&amp; selectedCells.get(row) == null) { return true; } return selectedCells.get(row).contains(column); } } </code></pre> <p>The last version is in Joeffice Mercurial repository on <a href="https://bitbucket.org/agoubard/joeffice/src/f292ca49106fd03905997ac4362c8175507ad79c/Spreadsheet/src/org/joeffice/spreadsheet/SheetTable.java?at=default" rel="nofollow" title="Bitbucket">bitbucket.org</a></p>
    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