Path: blob/master/test/jdk/javax/swing/JTable/8032874/bug8032874.java
41153 views
/*1* Copyright (c) 2014, 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 803287427* @summary Test whether ArrayIndexOutOfBoundsException is thrown or not,28* once selected row is removed from JTable with Sorter and Filter29* @author Dmitry Markov30* @run main bug803287431*/3233import java.awt.*;34import java.util.ArrayList;35import java.util.List;3637import javax.swing.*;38import javax.swing.table.AbstractTableModel;39import javax.swing.table.TableRowSorter;4041public class bug8032874 {42private static final int ROW_COUNT = 5;43private static JTable table;44private static TestTableModel tableModel;4546public static void main(String args[]) throws Exception {47Robot robot = new Robot();4849SwingUtilities.invokeAndWait(new Runnable() {50@Override51public void run() {52createAndShowUI();53}54});55robot.waitForIdle();5657SwingUtilities.invokeAndWait(new Runnable() {58@Override59public void run() {60table.getRowSorter().toggleSortOrder(0);61table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);62table.setRowSelectionInterval(1, 2);63}64});65robot.waitForIdle();6667SwingUtilities.invokeAndWait(new Runnable() {68@Override69public void run() {70for (int i = 0; i < ROW_COUNT; i++) {71tableModel.remove(0);72table.getRowSorter().toggleSortOrder(0);73}74}75});76}7778public static void createAndShowUI() {79try {80UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");81} catch (Exception e) {82throw new RuntimeException(e);83}8485JFrame frame = new JFrame("bug8032874");86frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);8788JPanel panel = new JPanel();89panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));9091tableModel = new TestTableModel();92table = new JTable(tableModel);93table.setSurrendersFocusOnKeystroke(true);9495final TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<TestTableModel>(tableModel);96rowSorter.setRowFilter(new RowFilter<TestTableModel, Integer>() {97@Override98public boolean include(Entry<? extends TestTableModel, ? extends Integer> entry) {99return entry.getIdentifier() % 2 == 0;100}101});102table.setRowSorter(rowSorter);103104JScrollPane jScrollPane = new JScrollPane(table);105panel.add(jScrollPane);106107frame.setContentPane(panel);108frame.setSize(new Dimension(800, 600));109frame.setVisible(true);110}111112private static class TestTableModel extends AbstractTableModel {113private final List<Integer> data;114115public TestTableModel() {116data = new ArrayList<Integer>();117118for (int i = 0; i < ROW_COUNT; i++) {119data.add(i);120}121}122123@Override124public int getRowCount() {125return data.size();126}127128@Override129public int getColumnCount() {130return 1;131}132133@Override134public Object getValueAt(int rowIndex, int columnIndex) {135return data.get(rowIndex);136}137138public void remove(int row) {139data.remove(row);140fireTableRowsDeleted(row, row);141}142}143}144145146147