Path: blob/master/test/jdk/javax/swing/JTable/7055065/bug7055065.java
41153 views
/*1* Copyright (c) 2012, 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* Portions Copyright (c) 2012 IBM Corporation25*/2627/*28* @test29* @key headful30* @bug 705506531* @summary NullPointerException when sorting JTable with empty cell32* @author Jonathan Lu33* @library ../../regtesthelpers/34* @build Util35* @run main bug705506536*/3738import java.awt.Dimension;39import java.awt.Point;40import java.awt.Rectangle;41import java.awt.Robot;42import java.awt.event.InputEvent;43import java.awt.event.KeyEvent;44import javax.swing.JFrame;45import javax.swing.JPanel;46import javax.swing.JScrollPane;47import javax.swing.JTable;48import javax.swing.SwingUtilities;49import javax.swing.table.AbstractTableModel;50import javax.swing.table.TableModel;51import javax.swing.table.TableRowSorter;52import java.util.concurrent.Callable;5354public class bug7055065 {5556private static JTable table;5758public static void main(String[] args) throws Exception {59Robot robot = new Robot();6061SwingUtilities.invokeAndWait(new Runnable() {6263public void run() {64createAndShowUI();65}66});6768robot.waitForIdle();69clickCell(robot, 1, 1);70Util.hitKeys(robot, KeyEvent.VK_BACK_SPACE, KeyEvent.VK_BACK_SPACE,71KeyEvent.VK_BACK_SPACE);7273robot.waitForIdle();74clickColumnHeader(robot, 1);7576robot.waitForIdle();77clickColumnHeader(robot, 1);78}7980private static void clickCell(Robot robot, final int row, final int column)81throws Exception {82Point point = Util.invokeOnEDT(new Callable<Point>() {83@Override84public Point call() throws Exception {85Rectangle rect = table.getCellRect(row, column, false);86Point point = new Point(rect.x + rect.width / 2, rect.y87+ rect.height / 2);88SwingUtilities.convertPointToScreen(point, table);89return point;90}91});9293robot.mouseMove(point.x, point.y);94robot.mousePress(InputEvent.BUTTON1_MASK);95robot.mouseRelease(InputEvent.BUTTON1_MASK);96}9798private static void clickColumnHeader(Robot robot, final int column)99throws Exception {100Point point = Util.invokeOnEDT(new Callable<Point>() {101@Override102public Point call() throws Exception {103Rectangle rect = table.getCellRect(0, column, false);104int headerHeight = table.getTableHeader().getHeight();105Point point = new Point(rect.x + rect.width / 2, rect.y106- headerHeight / 2);107SwingUtilities.convertPointToScreen(point, table);108return point;109}110});111112robot.mouseMove(point.x, point.y);113robot.mousePress(InputEvent.BUTTON1_MASK);114robot.mouseRelease(InputEvent.BUTTON1_MASK);115}116117private static void createAndShowUI() {118JFrame frame = new JFrame("SimpleTableDemo");119frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);120121JPanel newContentPane = new JPanel();122newContentPane.setOpaque(true);123frame.setContentPane(newContentPane);124125final String[] columnNames = { "String", "Number" };126final Object[][] data = { { "aaaa", new Integer(1) },127{ "bbbb", new Integer(3) }, { "cccc", new Integer(2) },128{ "dddd", new Integer(4) }, { "eeee", new Integer(5) } };129table = new JTable(data, columnNames);130131table.setPreferredScrollableViewportSize(new Dimension(500, 400));132table.setFillsViewportHeight(true);133134TableModel dataModel = new AbstractTableModel() {135136public int getColumnCount() {137return columnNames.length;138}139140public int getRowCount() {141return data.length;142}143144public Object getValueAt(int row, int col) {145return data[row][col];146}147148public String getColumnName(int column) {149return columnNames[column];150}151152public Class<?> getColumnClass(int c) {153return getValueAt(0, c).getClass();154}155156public boolean isCellEditable(int row, int col) {157return col != 5;158}159160public void setValueAt(Object aValue, int row, int column) {161data[row][column] = aValue;162}163};164table.setModel(dataModel);165TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(166dataModel);167table.setRowSorter(sorter);168169JScrollPane scrollPane = new JScrollPane(table);170newContentPane.add(scrollPane);171172frame.pack();173frame.setLocation(0, 0);174frame.setVisible(true);175}176}177178179