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