Path: blob/master/test/jdk/javax/swing/AbstractButton/6711682/bug6711682.java
41152 views
/*1* Copyright (c) 2009, 2010, 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* @test25* @key headful26* @bug 671168227* @summary JCheckBox in JTable: checkbox doesn't alaways respond to the first mouse click28* @author Alexander Potochkin29* @run main bug671168230*/3132import javax.swing.*;33import javax.swing.event.CellEditorListener;34import javax.swing.table.TableCellEditor;35import javax.swing.table.TableCellRenderer;36import java.awt.*;37import java.awt.event.InputEvent;38import java.awt.event.KeyEvent;39import java.util.EventObject;4041public class bug6711682 {42private static JCheckBox editorCb;43private static JCheckBox rendererCb;44private static JTable table;45private static JFrame f;4647public static void main(String[] args) throws Exception {48try {49Robot robot = new Robot();50robot.setAutoDelay(50);51SwingUtilities.invokeAndWait(new Runnable() {52public void run() {53createAndShowGUI();54}55});56robot.waitForIdle();57Point l = table.getLocationOnScreen();58int h = table.getRowHeight();59for (int i = 0; i < 3; i++) {60robot.mouseMove(l.x + 5, l.y + 5 + i * h);61robot.mousePress(InputEvent.BUTTON1_MASK);62robot.mouseRelease(InputEvent.BUTTON1_MASK);63}64// Without pressing F2 the last table's cell65// reported <code>false</code> value66// note that I can't press it inside the for loop67// because it doesn't reproduce the bug68robot.keyPress(KeyEvent.VK_F2);69robot.keyRelease(KeyEvent.VK_F2);7071for (int i = 0; i < 3; i++) {72if (!Boolean.TRUE.equals(table.getValueAt(i, 0))) {73throw new RuntimeException("Row #" + i + " checkbox is not selected");74}75}76for (int i = 2; i >= 0; i--) {77robot.mouseMove(l.x + 5, l.y + 5 + i * h);78robot.mousePress(InputEvent.BUTTON1_MASK);79robot.mouseRelease(InputEvent.BUTTON1_MASK);80}81robot.keyPress(KeyEvent.VK_F2);82robot.keyRelease(KeyEvent.VK_F2);83for (int i = 0; i < 3; i++) {84if (Boolean.TRUE.equals(table.getValueAt(i, 0))) {85throw new RuntimeException("Row #" + i + " checkbox is selected");86}87}88} finally {89if (f != null) SwingUtilities.invokeAndWait(() -> f.dispose());90}91}9293private static void createAndShowGUI() {94editorCb = new JCheckBox();95rendererCb = new JCheckBox();96f = new JFrame("Table with CheckBox");97Container p = f.getContentPane();98p.setLayout(new BorderLayout());99table = new JTable(new Object[][]{{false}, {false}, {false}}, new Object[]{"CheckBox"});100TableCellEditor editor = new TableCellEditor() {101int editedRow;102103public Component getTableCellEditorComponent(JTable table,104Object value, boolean isSelected, int row, int column) {105this.editedRow = row;106editorCb.setSelected(Boolean.TRUE.equals(value));107editorCb.setBackground(UIManager.getColor("Table.selectionBackground"));108return editorCb;109}110111public void addCellEditorListener(CellEditorListener l) {112}113114public void cancelCellEditing() {115}116117public Object getCellEditorValue() {118return editorCb.isSelected();119}120121public boolean isCellEditable(EventObject anEvent) {122return true;123}124125public void removeCellEditorListener(CellEditorListener l) {126}127128public boolean shouldSelectCell(EventObject anEvent) {129return true;130}131132public boolean stopCellEditing() {133table.getModel().setValueAt(editorCb.isSelected(), editedRow, 0);134return true;135}136};137table.getColumnModel().getColumn(0).setCellEditor(editor);138139TableCellRenderer renderer = new TableCellRenderer() {140public Component getTableCellRendererComponent(JTable table,141Object value, boolean isSelected, boolean hasFocus,142int row, int column) {143rendererCb.setSelected(Boolean.TRUE.equals(value));144return rendererCb;145}146};147table.getColumnModel().getColumn(0).setCellRenderer(renderer);148149p.add(table, BorderLayout.CENTER);150151f.pack();152f.setVisible(true);153}154}155156157