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