Path: blob/master/test/jdk/javax/swing/JTable/JTableScrollTest.java
41149 views
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*/21import java.awt.BorderLayout;22import java.awt.FlowLayout;23import java.awt.Color;24import java.awt.Dialog;25import javax.swing.JDialog;26import javax.swing.JPanel;27import javax.swing.JFrame;28import javax.swing.JTable;29import javax.swing.JTextArea;30import javax.swing.JButton;31import javax.swing.table.TableModel;32import javax.swing.JScrollPane;33import javax.swing.table.AbstractTableModel;34import javax.swing.SwingUtilities;3536/**37* @test38* @bug 808149139* @summary Scrolling a JTable creates artifacts40* @run main/manual JTableScrollTest41*/42public class JTableScrollTest {43static JFrame frame = new JFrame();44public static void main(String[] args) throws Exception {45SwingUtilities.invokeAndWait(new Runnable() {4647@Override48public void run() {49doTest(JTableScrollTest::createTable);50}51});52}5354private static void createTable() {55// final56final String[] names = {57new String("first_name"),58new String("last_name"),59new String("favorite_color"),60new String("favorite_food")61};6263// Create the dummy data (a few rows of names)64final Object[][] data = {65{"Mike", "Albers", "green", "strawberry"},66{"Mark", "Andrews", "blue", "grapes"},67{"Brian", "Beck", "black", "raspberry"},68{"Lara", "Bunni", "red", "strawberry"},69{"Roger", "Brinkley", "blue", "peach"},70{"Brent", "Christian", "black", "broccoli"},71{"Mark", "Davidson", "darkgreen", "asparagus"},72{"Jeff", "Dinkins", "blue", "kiwi"},73{"Ewan", "Dinkins", "yellow", "strawberry"},74{"Amy", "Fowler", "violet", "raspberry"},75{"Hania", "Gajewska", "purple", "raspberry"},76{"David", "Geary", "blue", "watermelon"},77{"Ryan", "Gosling", "pink", "donut"},78{"Eric", "Hawkes", "blue", "pickle"},79{"Shannon", "Hickey", "green", "grapes"},80{"Earl", "Johnson", "green", "carrot"},81{"Robi", "Khan", "green", "apple"},82{"Robert", "Kim", "blue", "strawberry"},83{"Janet", "Koenig", "turquoise", "peach"},84{"Jeff", "Kesselman", "blue", "pineapple"},85{"Onno", "Kluyt", "orange", "broccoli"},86{"Peter", "Korn", "sunpurple", "sparegrass"},87{"Rick", "Levenson", "black", "raspberry"},88{"Brian", "Lichtenwalter", "blue", "pear"},89{"Malini", "Minasandram", "beige", "corn"},90{"Michael", "Martak", "green", "strawberry"},91{"David", "Mendenhall", "forestgreen", "peach"},92{"Phil", "Milne", "pink", "banana"},93{"Lynn", "Monsanto", "cybergreen", "peach"},94{"Hans", "Muller", "rustred", "pineapple"},95{"Joshua", "Outwater", "blue", "pineapple"},96{"Tim", "Prinzing", "blue", "pepper"},97{"Raj", "Premkumar", "blue", "broccoli"},98{"Howard", "Rosen", "green", "strawberry"},99{"Ray", "Ryan", "black", "banana"},100{"Georges", "Saab", "aqua", "cantaloupe"},101{"Tom", "Santos", "blue", "pepper"},102{"Rich", "Schiavi", "blue", "pepper"},103{"Nancy", "Schorr", "green", "watermelon"},104{"Keith", "Sprochi", "darkgreen", "watermelon"},105{"Matt", "Tucker", "eblue", "broccoli"},106{"Dmitri", "Trembovetski", "red", "tomato"},107{"Scott", "Violet", "violet", "banana"},108{"Kathy", "Walrath", "darkgreen", "pear"},109};110111// Create a model of the data.112TableModel dataModel = new AbstractTableModel() {113public int getColumnCount() { return names.length; }114public int getRowCount() { return data.length;}115public Object getValueAt(int row, int col) {return data[row][col];}116public String getColumnName(int column) {return names[column];}117public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}118public boolean isCellEditable(int row, int col) {return col != 5;}119public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; }120};121122// Create the table123JTable tableView = new JTable(dataModel);124tableView.setBackground(Color.WHITE);125tableView.setForeground(Color.BLACK);126tableView.setSize(600, 800);127JScrollPane scrollpane = new JScrollPane(tableView);128frame.add(scrollpane);129frame.pack();130frame.setVisible(true);131}132133private static void doTest(Runnable action) {134String description =135"JTable with rows will be displayed along with scrollbar.\n"136+ "Scroll the table. Verify no arifacts are shown and rows.\n"137+ " are correctly displayed.";138final JDialog dialog = new JDialog();139dialog.setTitle("ScrollArtifactTest ");140JTextArea textArea = new JTextArea(description);141textArea.setEditable(false);142final JButton testButton = new JButton("Create Table");143final JButton passButton = new JButton("PASS");144passButton.setEnabled(false);145passButton.addActionListener((e) -> {146dialog.dispose();147if (frame != null) {148frame.setVisible(false);149frame.dispose();150}151});152final JButton failButton = new JButton("FAIL");153failButton.setEnabled(false);154failButton.addActionListener((e) -> {155dialog.dispose();156if (frame != null) {157frame.setVisible(false);158frame.dispose();159}160throw new RuntimeException("Scrollbar artifact shown");161});162testButton.addActionListener((e) -> {163testButton.setEnabled(false);164action.run();165passButton.setEnabled(true);166failButton.setEnabled(true);167});168JPanel mainPanel = new JPanel(new BorderLayout());169mainPanel.add(textArea, BorderLayout.CENTER);170JPanel buttonPanel = new JPanel(new FlowLayout());171buttonPanel.add(testButton);172buttonPanel.add(passButton);173buttonPanel.add(failButton);174mainPanel.add(buttonPanel, BorderLayout.SOUTH);175dialog.add(mainPanel);176dialog.pack();177dialog.setVisible(true);178}179180181}182183184