1 /*
2  * Copyright (c) 2015, 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  * @key headful
27  * @bug 6894632
28  * @summary Removing rows from a DefaultTableModel with a RowSorter deselectes
29  * last row
30  * @author Semyon Sadetsky
31  */
32 
33 import javax.swing.*;
34 import javax.swing.table.DefaultTableModel;
35 import javax.swing.table.TableModel;
36 import javax.swing.table.TableRowSorter;
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 public class bug6894632 {
41     private static JTable table;
42 
main(String[] args)43     public static void main(String[] args) throws Exception {
44         SwingUtilities.invokeAndWait(new Runnable() {
45             public void run() {
46 
47                 //init table with empty sort order
48                 test(new ArrayList<RowSorter.SortKey>());
49 
50                 //init table as unsorted
51                 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
52                 sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));
53                 test(sortKeys);
54             }
55         });
56 
57         System.out.println("ok");
58     }
59 
test(final List<RowSorter.SortKey> sortKeys)60     static void test(final List<RowSorter.SortKey> sortKeys) {
61         final JFrame frame = new JFrame();
62         try {
63             frame.setUndecorated(true);
64             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65             table = new JTable();
66             DefaultTableModel tableModel =
67                     new DefaultTableModel(10, 1) {
68                         public Object getValueAt(int row, int column) {
69                             return row == getRowCount() - 1 ? row + "==last" :
70                                     row;
71                         }
72                     };
73             table.setModel(tableModel);
74             TableRowSorter<TableModel> sorter =
75                     new TableRowSorter<TableModel>(tableModel);
76             sorter.setSortKeys(sortKeys);
77             table.setRowSorter(sorter);
78 
79             frame.setContentPane(table);
80             frame.pack();
81             frame.setLocationRelativeTo(null);
82             frame.setVisible(true);
83 
84             int lastRow = table.getRowCount() - 1;
85 
86             //select last row
87             table.setRowSelectionInterval(lastRow, lastRow);
88 
89             //remove row above the last
90             tableModel.removeRow(lastRow - 1);
91             lastRow = table.getRowCount() - 1;
92             if (lastRow != table.getSelectedRow()) {
93                 throw new RuntimeException("last row must be still selected");
94             }
95 
96             //sort table
97             sortKeys.clear();
98             sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.ASCENDING));
99             sorter.setSortKeys(sortKeys);
100             //remove row above the last
101             lastRow = table.getRowCount() - 1;
102             tableModel.removeRow(lastRow - 1);
103 
104             if (!table.getValueAt(table.getSelectedRow(), 0).toString()
105                     .endsWith("==last")) {
106                 throw new RuntimeException(
107                         "row ends with \"==last\" row must be still selected");
108             }
109 
110             //make table unsorted again
111             sortKeys.clear();
112             sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));
113             sorter.setSortKeys(sortKeys);
114             //remove row above the last
115             lastRow = table.getRowCount() - 1;
116             tableModel.removeRow(lastRow - 1);
117 
118             lastRow = table.getRowCount() - 1;
119             if (lastRow != table.getSelectedRow()) {
120                 throw new RuntimeException(
121                         "last row must be still selected");
122             }
123         } finally {
124             frame.dispose();
125         }
126     }
127 
128 
129 }
130