Skip to content

Commit 96305e0

Browse files
committed
4466930: JTable.selectAll boundary handling
Reviewed-by: abhiscxk, tr
1 parent fb210e3 commit 96305e0

File tree

2 files changed

+148
-29
lines changed

2 files changed

+148
-29
lines changed

src/java.desktop/share/classes/javax/swing/JTable.java

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2146,42 +2146,54 @@ public boolean getCellSelectionEnabled() {
21462146
return getRowSelectionAllowed() && getColumnSelectionAllowed();
21472147
}
21482148

2149-
/**
2150-
* Selects all rows, columns, and cells in the table.
2151-
*/
2152-
public void selectAll() {
2153-
// If I'm currently editing, then I should stop editing
2154-
if (isEditing()) {
2155-
removeEditor();
2156-
}
2157-
if (getRowCount() > 0 && getColumnCount() > 0) {
2158-
int oldLead;
2159-
int oldAnchor;
2160-
ListSelectionModel selModel;
2149+
private void selectRows(int rowCount) {
2150+
ListSelectionModel selModel = selectionModel;
2151+
selModel.setValueIsAdjusting(true);
2152+
int oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), true);
2153+
int oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), true);
21612154

2162-
selModel = selectionModel;
2163-
selModel.setValueIsAdjusting(true);
2164-
oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), true);
2165-
oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), true);
2155+
setRowSelectionInterval(0, rowCount - 1);
21662156

2167-
setRowSelectionInterval(0, getRowCount()-1);
2157+
// this is done to restore the anchor and lead
2158+
SwingUtilities2.setLeadAnchorWithoutSelection(selModel, oldLead, oldAnchor);
2159+
2160+
selModel.setValueIsAdjusting(false);
2161+
}
21682162

2169-
// this is done to restore the anchor and lead
2170-
SwingUtilities2.setLeadAnchorWithoutSelection(selModel, oldLead, oldAnchor);
2163+
private void selectColumns(int columnCount) {
2164+
ListSelectionModel selModel = columnModel.getSelectionModel();
2165+
selModel.setValueIsAdjusting(true);
2166+
int oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), false);
2167+
int oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), false);
21712168

2172-
selModel.setValueIsAdjusting(false);
2169+
setColumnSelectionInterval(0, columnCount - 1);
21732170

2174-
selModel = columnModel.getSelectionModel();
2175-
selModel.setValueIsAdjusting(true);
2176-
oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), false);
2177-
oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), false);
2171+
// this is done to restore the anchor and lead
2172+
SwingUtilities2.setLeadAnchorWithoutSelection(selModel, oldLead, oldAnchor);
21782173

2179-
setColumnSelectionInterval(0, getColumnCount()-1);
2174+
selModel.setValueIsAdjusting(false);
2175+
}
21802176

2181-
// this is done to restore the anchor and lead
2182-
SwingUtilities2.setLeadAnchorWithoutSelection(selModel, oldLead, oldAnchor);
2177+
/**
2178+
* Selects all rows, columns, and cells in the table.
2179+
*/
2180+
public void selectAll() {
2181+
int rowCount = getRowCount();
2182+
int columnCount = getColumnCount();
21832183

2184-
selModel.setValueIsAdjusting(false);
2184+
// If I'm currently editing, then I should stop editing
2185+
if (isEditing()) {
2186+
removeEditor();
2187+
}
2188+
if (rowCount > 0 && columnCount > 0) {
2189+
selectRows(rowCount);
2190+
selectColumns(columnCount);
2191+
} else if (rowCount > 0 && columnCount == 0
2192+
&& getRowSelectionAllowed()) {
2193+
selectRows(rowCount);
2194+
} else if (columnCount > 0 && rowCount == 0
2195+
&& getColumnSelectionAllowed()) {
2196+
selectColumns(columnCount);
21852197
}
21862198
}
21872199

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4466930
27+
* @summary Verifies selectAll selects all available rows and columns
28+
* irrespective of presence of columns and rows respectively
29+
* @run main TestTableSelectAll
30+
*/
31+
32+
import javax.swing.JTable;
33+
import javax.swing.table.DefaultTableModel;
34+
35+
public class TestTableSelectAll {
36+
37+
public static void main(String[] args) throws Exception {
38+
39+
testColumnSelect();
40+
testRowSelect();
41+
}
42+
43+
private static void testColumnSelect() {
44+
boolean colSelNoRow;
45+
boolean colSelWithRow;
46+
47+
// TableModel with no rows, but 10 columns
48+
DefaultTableModel data = new DefaultTableModel(0, 10);
49+
50+
JTable table = new JTable(data);
51+
52+
// columns can be selected
53+
table.setColumnSelectionAllowed(true);
54+
table.setRowSelectionAllowed(false);
55+
56+
table.selectAll();
57+
58+
colSelNoRow = table.isColumnSelected(0);
59+
60+
// After selectAll(), I would expect all columns to be selected, no matter
61+
// whether there are rows or not.
62+
System.out.println("Column 0 is selected: " + colSelNoRow);
63+
64+
data.addRow(new Object[0]);
65+
66+
table.selectAll();
67+
68+
colSelWithRow = table.isColumnSelected(0);
69+
System.out.println("Column 0 is selected: " + colSelWithRow);
70+
71+
if (!(colSelNoRow && colSelWithRow)) {
72+
throw new RuntimeException("selectAll did not select column");
73+
}
74+
}
75+
76+
private static void testRowSelect() {
77+
boolean rowSelNoColumn;
78+
boolean rowSelWithColumn;
79+
80+
// TableModel with 10 rows, but no columns
81+
DefaultTableModel data = new DefaultTableModel(10, 0);
82+
83+
JTable table = new JTable(data);
84+
85+
// rows can be selected
86+
table.setRowSelectionAllowed(true);
87+
table.setColumnSelectionAllowed(false);
88+
89+
table.selectAll();
90+
91+
rowSelNoColumn = table.isRowSelected(0);
92+
93+
// After selectAll(), I would expect all rows to be selected, no matter
94+
// whether there are columns or not.
95+
System.out.println("Row 0 is selected: " + rowSelNoColumn);
96+
97+
data.addColumn(new Object[0]);
98+
table.selectAll();
99+
100+
rowSelWithColumn = table.isRowSelected(0);
101+
System.out.println("Row 0 is selected: " + rowSelWithColumn);
102+
103+
if (!(rowSelNoColumn && rowSelWithColumn)) {
104+
throw new RuntimeException("selectAll did not select row");
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)