bug8032874.java revision 11018:66682f651425
1/* 2 * Copyright (c) 2014, 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/* @test 25 * @bug 8032874 26 * @summary Test whether ArrayIndexOutOfBoundsException is thrown or not, 27 * once selected row is removed from JTable with Sorter and Filter 28 * @author Dmitry Markov 29 * @run main bug8032874 30 */ 31 32import java.awt.*; 33import java.util.ArrayList; 34import java.util.List; 35 36import javax.swing.*; 37import javax.swing.table.AbstractTableModel; 38import javax.swing.table.TableRowSorter; 39 40public class bug8032874 { 41 private static final int ROW_COUNT = 5; 42 private static JTable table; 43 private static TestTableModel tableModel; 44 45 public static void main(String args[]) throws Exception { 46 Robot robot = new Robot(); 47 48 SwingUtilities.invokeAndWait(new Runnable() { 49 @Override 50 public void run() { 51 createAndShowUI(); 52 } 53 }); 54 robot.waitForIdle(); 55 56 SwingUtilities.invokeAndWait(new Runnable() { 57 @Override 58 public void run() { 59 table.getRowSorter().toggleSortOrder(0); 60 table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 61 table.setRowSelectionInterval(1, 2); 62 } 63 }); 64 robot.waitForIdle(); 65 66 SwingUtilities.invokeAndWait(new Runnable() { 67 @Override 68 public void run() { 69 for (int i = 0; i < ROW_COUNT; i++) { 70 tableModel.remove(0); 71 table.getRowSorter().toggleSortOrder(0); 72 } 73 } 74 }); 75 } 76 77 public static void createAndShowUI() { 78 try { 79 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 80 } catch (Exception e) { 81 throw new RuntimeException(e); 82 } 83 84 JFrame frame = new JFrame("bug8032874"); 85 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 86 87 JPanel panel = new JPanel(); 88 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 89 90 tableModel = new TestTableModel(); 91 table = new JTable(tableModel); 92 table.setSurrendersFocusOnKeystroke(true); 93 94 final TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<TestTableModel>(tableModel); 95 rowSorter.setRowFilter(new RowFilter<TestTableModel, Integer>() { 96 @Override 97 public boolean include(Entry<? extends TestTableModel, ? extends Integer> entry) { 98 return entry.getIdentifier() % 2 == 0; 99 } 100 }); 101 table.setRowSorter(rowSorter); 102 103 JScrollPane jScrollPane = new JScrollPane(table); 104 panel.add(jScrollPane); 105 106 frame.setContentPane(panel); 107 frame.setSize(new Dimension(800, 600)); 108 frame.setVisible(true); 109 } 110 111 private static class TestTableModel extends AbstractTableModel { 112 private final List<Integer> data; 113 114 public TestTableModel() { 115 data = new ArrayList<Integer>(); 116 117 for (int i = 0; i < ROW_COUNT; i++) { 118 data.add(i); 119 } 120 } 121 122 @Override 123 public int getRowCount() { 124 return data.size(); 125 } 126 127 @Override 128 public int getColumnCount() { 129 return 1; 130 } 131 132 @Override 133 public Object getValueAt(int rowIndex, int columnIndex) { 134 return data.get(rowIndex); 135 } 136 137 public void remove(int row) { 138 data.remove(row); 139 fireTableRowsDeleted(row, row); 140 } 141 } 142} 143 144