Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTable/8032874/bug8032874.java
38854 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/* @test24* @bug 803287425* @summary Test whether ArrayIndexOutOfBoundsException is thrown or not,26* once selected row is removed from JTable with Sorter and Filter27* @author Dmitry Markov28* @run main bug803287429*/3031import java.awt.*;32import java.util.ArrayList;33import java.util.List;3435import javax.swing.*;36import javax.swing.table.AbstractTableModel;37import javax.swing.table.TableRowSorter;3839public class bug8032874 {40private static final int ROW_COUNT = 5;41private static JTable table;42private static TestTableModel tableModel;4344public static void main(String args[]) throws Exception {45Robot robot = new Robot();4647SwingUtilities.invokeAndWait(new Runnable() {48@Override49public void run() {50createAndShowUI();51}52});53robot.waitForIdle();5455SwingUtilities.invokeAndWait(new Runnable() {56@Override57public void run() {58table.getRowSorter().toggleSortOrder(0);59table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);60table.setRowSelectionInterval(1, 2);61}62});63robot.waitForIdle();6465SwingUtilities.invokeAndWait(new Runnable() {66@Override67public void run() {68for (int i = 0; i < ROW_COUNT; i++) {69tableModel.remove(0);70table.getRowSorter().toggleSortOrder(0);71}72}73});74}7576public static void createAndShowUI() {77try {78UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");79} catch (Exception e) {80throw new RuntimeException(e);81}8283JFrame frame = new JFrame("bug8032874");84frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);8586JPanel panel = new JPanel();87panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));8889tableModel = new TestTableModel();90table = new JTable(tableModel);91table.setSurrendersFocusOnKeystroke(true);9293final TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<TestTableModel>(tableModel);94rowSorter.setRowFilter(new RowFilter<TestTableModel, Integer>() {95@Override96public boolean include(Entry<? extends TestTableModel, ? extends Integer> entry) {97return entry.getIdentifier() % 2 == 0;98}99});100table.setRowSorter(rowSorter);101102JScrollPane jScrollPane = new JScrollPane(table);103panel.add(jScrollPane);104105frame.setContentPane(panel);106frame.setSize(new Dimension(800, 600));107frame.setVisible(true);108}109110private static class TestTableModel extends AbstractTableModel {111private final List<Integer> data;112113public TestTableModel() {114data = new ArrayList<Integer>();115116for (int i = 0; i < ROW_COUNT; i++) {117data.add(i);118}119}120121@Override122public int getRowCount() {123return data.size();124}125126@Override127public int getColumnCount() {128return 1;129}130131@Override132public Object getValueAt(int rowIndex, int columnIndex) {133return data.get(rowIndex);134}135136public void remove(int row) {137data.remove(row);138fireTableRowsDeleted(row, row);139}140}141}142143144145