1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * Portions Copyright (c) 2012 IBM Corporation 26 */ 27 28 /* 29 * @test 30 * @key headful 31 * @bug 7055065 32 * @summary NullPointerException when sorting JTable with empty cell 33 * @author Jonathan Lu 34 * @library ../../regtesthelpers/ 35 * @build Util 36 * @run main bug7055065 37 */ 38 39 import java.awt.Dimension; 40 import java.awt.Point; 41 import java.awt.Rectangle; 42 import java.awt.Robot; 43 import java.awt.Toolkit; 44 import java.awt.event.InputEvent; 45 import java.awt.event.KeyEvent; 46 import javax.swing.JFrame; 47 import javax.swing.JPanel; 48 import javax.swing.JScrollPane; 49 import javax.swing.JTable; 50 import javax.swing.SwingUtilities; 51 import javax.swing.table.AbstractTableModel; 52 import javax.swing.table.TableModel; 53 import javax.swing.table.TableRowSorter; 54 import sun.awt.SunToolkit; 55 import java.util.concurrent.Callable; 56 57 public class bug7055065 { 58 59 private static JTable table; 60 61 public static void main(String[] args) throws Exception { 62 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 63 Robot robot = new Robot(); 64 65 SwingUtilities.invokeAndWait(new Runnable() { 66 67 public void run() { 68 createAndShowUI(); 69 } 70 }); 71 72 toolkit.realSync(); 73 clickCell(robot, 1, 1); 74 Util.hitKeys(robot, KeyEvent.VK_BACK_SPACE, KeyEvent.VK_BACK_SPACE, 75 KeyEvent.VK_BACK_SPACE); 76 77 toolkit.realSync(); 78 clickColumnHeader(robot, 1); 79 80 toolkit.realSync(); 81 clickColumnHeader(robot, 1); 82 } 83 84 private static void clickCell(Robot robot, final int row, final int column) 85 throws Exception { 86 Point point = Util.invokeOnEDT(new Callable<Point>() { 87 @Override 88 public Point call() throws Exception { 89 Rectangle rect = table.getCellRect(row, column, false); 90 Point point = new Point(rect.x + rect.width / 2, rect.y 91 + rect.height / 2); 92 SwingUtilities.convertPointToScreen(point, table); 93 return point; 94 } 95 }); 96 97 robot.mouseMove(point.x, point.y); 98 robot.mousePress(InputEvent.BUTTON1_MASK); 99 robot.mouseRelease(InputEvent.BUTTON1_MASK); 100 } 101 102 private static void clickColumnHeader(Robot robot, final int column) 103 throws Exception { 104 Point point = Util.invokeOnEDT(new Callable<Point>() { 105 @Override 106 public Point call() throws Exception { 107 Rectangle rect = table.getCellRect(0, column, false); 108 int headerHeight = table.getTableHeader().getHeight(); 109 Point point = new Point(rect.x + rect.width / 2, rect.y 110 - headerHeight / 2); 111 SwingUtilities.convertPointToScreen(point, table); 112 return point; 113 } 114 }); 115 116 robot.mouseMove(point.x, point.y); 117 robot.mousePress(InputEvent.BUTTON1_MASK); 118 robot.mouseRelease(InputEvent.BUTTON1_MASK); 119 } 120 121 private static void createAndShowUI() { 122 JFrame frame = new JFrame("SimpleTableDemo"); 123 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 124 125 JPanel newContentPane = new JPanel(); 126 newContentPane.setOpaque(true); 127 frame.setContentPane(newContentPane); 128 129 final String[] columnNames = { "String", "Number" }; 130 final Object[][] data = { { "aaaa", new Integer(1) }, 131 { "bbbb", new Integer(3) }, { "cccc", new Integer(2) }, 132 { "dddd", new Integer(4) }, { "eeee", new Integer(5) } }; 133 table = new JTable(data, columnNames); 134 135 table.setPreferredScrollableViewportSize(new Dimension(500, 400)); 136 table.setFillsViewportHeight(true); 137 138 TableModel dataModel = new AbstractTableModel() { 139 140 public int getColumnCount() { 141 return columnNames.length; 142 } 143 144 public int getRowCount() { 145 return data.length; 146 } 147 148 public Object getValueAt(int row, int col) { 149 return data[row][col]; 150 } 151 152 public String getColumnName(int column) { 153 return columnNames[column]; 154 } 155 156 public Class<?> getColumnClass(int c) { 157 return getValueAt(0, c).getClass(); 158 } 159 160 public boolean isCellEditable(int row, int col) { 161 return col != 5; 162 } 163 164 public void setValueAt(Object aValue, int row, int column) { 165 data[row][column] = aValue; 166 } 167 }; 168 table.setModel(dataModel); 169 TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>( 170 dataModel); 171 table.setRowSorter(sorter); 172 173 JScrollPane scrollPane = new JScrollPane(table); 174 newContentPane.add(scrollPane); 175 176 frame.pack(); 177 frame.setLocation(0, 0); 178 frame.setVisible(true); 179 } 180 }