Path: blob/master/test/jdk/javax/swing/JTable/6894632/bug6894632.java
41153 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 689463227* @summary Removing rows from a DefaultTableModel with a RowSorter deselectes28* last row29* @author Semyon Sadetsky30*/3132import javax.swing.*;33import javax.swing.table.DefaultTableModel;34import javax.swing.table.TableModel;35import javax.swing.table.TableRowSorter;36import java.util.ArrayList;37import java.util.List;3839public class bug6894632 {40private static JTable table;4142public static void main(String[] args) throws Exception {43SwingUtilities.invokeAndWait(new Runnable() {44public void run() {4546//init table with empty sort order47test(new ArrayList<RowSorter.SortKey>());4849//init table as unsorted50List<RowSorter.SortKey> sortKeys = new ArrayList<>();51sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));52test(sortKeys);53}54});5556System.out.println("ok");57}5859static void test(final List<RowSorter.SortKey> sortKeys) {60final JFrame frame = new JFrame();61try {62frame.setUndecorated(true);63frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);64table = new JTable();65DefaultTableModel tableModel =66new DefaultTableModel(10, 1) {67public Object getValueAt(int row, int column) {68return row == getRowCount() - 1 ? row + "==last" :69row;70}71};72table.setModel(tableModel);73TableRowSorter<TableModel> sorter =74new TableRowSorter<TableModel>(tableModel);75sorter.setSortKeys(sortKeys);76table.setRowSorter(sorter);7778frame.setContentPane(table);79frame.pack();80frame.setLocationRelativeTo(null);81frame.setVisible(true);8283int lastRow = table.getRowCount() - 1;8485//select last row86table.setRowSelectionInterval(lastRow, lastRow);8788//remove row above the last89tableModel.removeRow(lastRow - 1);90lastRow = table.getRowCount() - 1;91if (lastRow != table.getSelectedRow()) {92throw new RuntimeException("last row must be still selected");93}9495//sort table96sortKeys.clear();97sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.ASCENDING));98sorter.setSortKeys(sortKeys);99//remove row above the last100lastRow = table.getRowCount() - 1;101tableModel.removeRow(lastRow - 1);102103if (!table.getValueAt(table.getSelectedRow(), 0).toString()104.endsWith("==last")) {105throw new RuntimeException(106"row ends with \"==last\" row must be still selected");107}108109//make table unsorted again110sortKeys.clear();111sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));112sorter.setSortKeys(sortKeys);113//remove row above the last114lastRow = table.getRowCount() - 1;115tableModel.removeRow(lastRow - 1);116117lastRow = table.getRowCount() - 1;118if (lastRow != table.getSelectedRow()) {119throw new RuntimeException(120"last row must be still selected");121}122} finally {123frame.dispose();124}125}126127128}129130131