import sun.awt.SunToolkit; import javax.swing.*; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableRowSorter; import java.awt.*; import java.util.ArrayList; import java.util.List; /* @test * @bug 6921688 * @summary If JTable has Sorter and Filter and selected row is removed ArrayIndexOutOfBoundsException will be thrown * @author Fedor Bobin * @run main bug6921688 */ public class bug6921688 { public static final int ROW_COUNT = 5; private static JTable table; private static JFrame frame; private static MyTableModel tableModel; public static void main(String args[]) throws Exception { SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { createGUI(); frame.setVisible(true); } }); toolkit.realSync(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { table.getRowSorter().toggleSortOrder(0); table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); table.setRowSelectionInterval(1, 2); } }); toolkit.realSync(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { for (int i = 0; i < ROW_COUNT; i++) { System.out.println("will remove"); tableModel.remove(0); table.getRowSorter().toggleSortOrder(0); } } }); toolkit.realSync(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { frame.setVisible(false); } }); } public static void createGUI() { frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); tableModel = new MyTableModel(); table = new JTable(tableModel); table.setSurrendersFocusOnKeystroke(true); final TableRowSorter rowSorter = new TableRowSorter(tableModel); rowSorter.setRowFilter(new RowFilter() { @Override public boolean include(Entry entry) { return entry.getIdentifier() % 2 == 0; } }); table.setRowSorter(rowSorter); JScrollPane jScrollPane = new JScrollPane(table); panel.add(jScrollPane); frame.setContentPane(panel); frame.setSize(new Dimension(800, 600)); } private static class MyTableModel extends AbstractTableModel { private final List data; public MyTableModel() { data = new ArrayList(); for (int i = 0; i < ROW_COUNT; i++) { data.add(i); } } @Override public int getRowCount() { return data.size(); } @Override public int getColumnCount() { return 1; } @Override public Object getValueAt(int rowIndex, int columnIndex) { return data.get(rowIndex); } public void remove(int row) { data.remove(row); fireTableRowsDeleted(row, row); } } }