1 /* 2 * Copyright (c) 2009, 2010, 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 /* @test 25 @bug 6711682 26 @summary JCheckBox in JTable: checkbox doesn't alaways respond to the first mouse click 27 @author Alexander Potochkin 28 @run main bug6711682 29 */ 30 31 import javax.swing.*; 32 import javax.swing.event.CellEditorListener; 33 import javax.swing.table.TableCellEditor; 34 import javax.swing.table.TableCellRenderer; 35 import java.awt.*; 36 import java.awt.event.InputEvent; 37 import java.awt.event.KeyEvent; 38 import java.util.EventObject; 39 40 public class bug6711682 { 41 private static JCheckBox editorCb; 42 private static JCheckBox rendererCb; 43 private static JTable table; 44 45 public static void main(String[] args) throws Exception { 46 Robot robot = new Robot(); 47 robot.setAutoDelay(50); 48 SwingUtilities.invokeAndWait(new Runnable() { 49 public void run() { 50 createAndShowGUI(); 51 } 52 }); 53 robot.waitForIdle(); 54 Point l = table.getLocationOnScreen(); 55 int h = table.getRowHeight(); 56 for (int i = 0; i < 3; i++) { 57 robot.mouseMove(l.x + 5, l.y + 5 + i * h); 58 robot.mousePress(InputEvent.BUTTON1_MASK); 59 robot.mouseRelease(InputEvent.BUTTON1_MASK); 60 } 61 // Without pressing F2 the last table's cell 62 // reported <code>false</code> value 63 // note that I can't press it inside the for loop 64 // because it doesn't reproduce the bug 65 robot.keyPress(KeyEvent.VK_F2); 66 robot.keyRelease(KeyEvent.VK_F2); 67 68 for (int i = 0; i < 3; i++) { 69 if (!Boolean.TRUE.equals(table.getValueAt(i, 0))) { 70 throw new RuntimeException("Row #" + i + " checkbox is not selected"); 71 } 72 } 73 for (int i = 2; i >= 0; i--) { 74 robot.mouseMove(l.x + 5, l.y + 5 + i * h); 75 robot.mousePress(InputEvent.BUTTON1_MASK); 76 robot.mouseRelease(InputEvent.BUTTON1_MASK); 77 } 78 robot.keyPress(KeyEvent.VK_F2); 79 robot.keyRelease(KeyEvent.VK_F2); 80 for (int i = 0; i < 3; i++) { 81 if (Boolean.TRUE.equals(table.getValueAt(i, 0))) { 82 throw new RuntimeException("Row #" + i + " checkbox is selected"); 83 } 84 } 85 } 86 87 private static void createAndShowGUI() { 88 editorCb = new JCheckBox(); 89 rendererCb = new JCheckBox(); 90 JFrame f = new JFrame("Table with CheckBox"); 91 Container p = f.getContentPane(); 92 p.setLayout(new BorderLayout()); 93 table = new JTable(new Object[][]{{false}, {false}, {false}}, new Object[]{"CheckBox"}); 94 TableCellEditor editor = new TableCellEditor() { 95 int editedRow; 96 97 public Component getTableCellEditorComponent(JTable table, 98 Object value, boolean isSelected, int row, int column) { 99 this.editedRow = row; 100 editorCb.setSelected(Boolean.TRUE.equals(value)); 101 editorCb.setBackground(UIManager.getColor("Table.selectionBackground")); 102 return editorCb; 103 } 104 105 public void addCellEditorListener(CellEditorListener l) { 106 } 107 108 public void cancelCellEditing() { 109 } 110 111 public Object getCellEditorValue() { 112 return editorCb.isSelected(); 113 } 114 115 public boolean isCellEditable(EventObject anEvent) { 116 return true; 117 } 118 119 public void removeCellEditorListener(CellEditorListener l) { 120 } 121 122 public boolean shouldSelectCell(EventObject anEvent) { 123 return true; 124 } 125 126 public boolean stopCellEditing() { 127 table.getModel().setValueAt(editorCb.isSelected(), editedRow, 0); 128 return true; 129 } 130 }; 131 table.getColumnModel().getColumn(0).setCellEditor(editor); 132 133 TableCellRenderer renderer = new TableCellRenderer() { 134 public Component getTableCellRendererComponent(JTable table, 135 Object value, boolean isSelected, boolean hasFocus, 136 int row, int column) { 137 rendererCb.setSelected(Boolean.TRUE.equals(value)); 138 return rendererCb; 139 } 140 }; 141 table.getColumnModel().getColumn(0).setCellRenderer(renderer); 142 143 p.add(table, BorderLayout.CENTER); 144 145 f.pack(); 146 f.setVisible(true); 147 } 148 }