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