Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/AbstractButton/6711682/bug6711682.java
38855 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/* @test24@bug 671168225@summary JCheckBox in JTable: checkbox doesn't alaways respond to the first mouse click26@author Alexander Potochkin27@run main bug671168228*/2930import javax.swing.*;31import javax.swing.event.CellEditorListener;32import javax.swing.table.TableCellEditor;33import javax.swing.table.TableCellRenderer;34import java.awt.*;35import java.awt.event.InputEvent;36import java.awt.event.KeyEvent;37import java.util.EventObject;3839public class bug6711682 {40private static JCheckBox editorCb;41private static JCheckBox rendererCb;42private static JTable table;4344public static void main(String[] args) throws Exception {45Robot robot = new Robot();46robot.setAutoDelay(50);47SwingUtilities.invokeAndWait(new Runnable() {48public void run() {49createAndShowGUI();50}51});52robot.waitForIdle();53Point l = table.getLocationOnScreen();54int h = table.getRowHeight();55for (int i = 0; i < 3; i++) {56robot.mouseMove(l.x + 5, l.y + 5 + i * h);57robot.mousePress(InputEvent.BUTTON1_MASK);58robot.mouseRelease(InputEvent.BUTTON1_MASK);59}60// Without pressing F2 the last table's cell61// reported <code>false</code> value62// note that I can't press it inside the for loop63// because it doesn't reproduce the bug64robot.keyPress(KeyEvent.VK_F2);65robot.keyRelease(KeyEvent.VK_F2);6667for (int i = 0; i < 3; i++) {68if (!Boolean.TRUE.equals(table.getValueAt(i, 0))) {69throw new RuntimeException("Row #" + i + " checkbox is not selected");70}71}72for (int i = 2; i >= 0; i--) {73robot.mouseMove(l.x + 5, l.y + 5 + i * h);74robot.mousePress(InputEvent.BUTTON1_MASK);75robot.mouseRelease(InputEvent.BUTTON1_MASK);76}77robot.keyPress(KeyEvent.VK_F2);78robot.keyRelease(KeyEvent.VK_F2);79for (int i = 0; i < 3; i++) {80if (Boolean.TRUE.equals(table.getValueAt(i, 0))) {81throw new RuntimeException("Row #" + i + " checkbox is selected");82}83}84}8586private static void createAndShowGUI() {87editorCb = new JCheckBox();88rendererCb = new JCheckBox();89JFrame f = new JFrame("Table with CheckBox");90Container p = f.getContentPane();91p.setLayout(new BorderLayout());92table = new JTable(new Object[][]{{false}, {false}, {false}}, new Object[]{"CheckBox"});93TableCellEditor editor = new TableCellEditor() {94int editedRow;9596public Component getTableCellEditorComponent(JTable table,97Object value, boolean isSelected, int row, int column) {98this.editedRow = row;99editorCb.setSelected(Boolean.TRUE.equals(value));100editorCb.setBackground(UIManager.getColor("Table.selectionBackground"));101return editorCb;102}103104public void addCellEditorListener(CellEditorListener l) {105}106107public void cancelCellEditing() {108}109110public Object getCellEditorValue() {111return editorCb.isSelected();112}113114public boolean isCellEditable(EventObject anEvent) {115return true;116}117118public void removeCellEditorListener(CellEditorListener l) {119}120121public boolean shouldSelectCell(EventObject anEvent) {122return true;123}124125public boolean stopCellEditing() {126table.getModel().setValueAt(editorCb.isSelected(), editedRow, 0);127return true;128}129};130table.getColumnModel().getColumn(0).setCellEditor(editor);131132TableCellRenderer renderer = new TableCellRenderer() {133public Component getTableCellRendererComponent(JTable table,134Object value, boolean isSelected, boolean hasFocus,135int row, int column) {136rendererCb.setSelected(Boolean.TRUE.equals(value));137return rendererCb;138}139};140table.getColumnModel().getColumn(0).setCellRenderer(renderer);141142p.add(table, BorderLayout.CENTER);143144f.pack();145f.setVisible(true);146}147}148149150