jdk/test/javax/swing/JComboBox/8072767/bug8072767.java
changeset 30463 06e0b7759125
child 40128 e635645d2a8a
equal deleted inserted replaced
30462:507bcb03c954 30463:06e0b7759125
       
     1 /*
       
     2  * To change this license header, choose License Headers in Project Properties.
       
     3  * To change this template file, choose Tools | Templates
       
     4  /*
       
     5  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     7  *
       
     8  * This code is free software; you can redistribute it and/or modify it
       
     9  * under the terms of the GNU General Public License version 2 only, as
       
    10  * published by the Free Software Foundation.
       
    11  *
       
    12  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    15  * version 2 for more details (a copy is included in the LICENSE file that
       
    16  * accompanied this code).
       
    17  *
       
    18  * You should have received a copy of the GNU General Public License version
       
    19  * 2 along with this work; if not, write to the Free Software Foundation,
       
    20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    21  *
       
    22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    23  * or visit www.oracle.com if you need additional information or have any
       
    24  * questions.
       
    25  */
       
    26 
       
    27 import java.awt.Point;
       
    28 import java.awt.Rectangle;
       
    29 import java.awt.Robot;
       
    30 import java.awt.event.InputEvent;
       
    31 import java.awt.event.KeyEvent;
       
    32 import javax.swing.DefaultCellEditor;
       
    33 import javax.swing.JComboBox;
       
    34 import javax.swing.JFrame;
       
    35 import javax.swing.JTable;
       
    36 import javax.swing.SwingUtilities;
       
    37 
       
    38 /**
       
    39  * @test
       
    40  * @bug 8072767
       
    41  * @author Alexander Scherbatiy
       
    42  * @summary DefaultCellEditor for comboBox creates ActionEvent with wrong source
       
    43  *          object
       
    44  * @run main bug8072767
       
    45  */
       
    46 
       
    47 public class bug8072767 {
       
    48 
       
    49     private static final String TEST1 = "Test";
       
    50     private static final String TEST2 = TEST1 + 1;
       
    51 
       
    52     private static JFrame frame;
       
    53     private static JTable table;
       
    54     private static Point point;
       
    55     private static boolean testPass;
       
    56 
       
    57     public static void main(String[] args) throws Exception {
       
    58         Robot robot = new Robot();
       
    59         robot.setAutoDelay(50);
       
    60         SwingUtilities.invokeAndWait(bug8072767::createAndShowGUI);
       
    61         robot.waitForIdle();
       
    62         SwingUtilities.invokeAndWait(() -> {
       
    63             point = table.getLocationOnScreen();
       
    64             Rectangle rect = table.getCellRect(0, 0, true);
       
    65             point.translate(rect.width / 2, rect.height / 2);
       
    66         });
       
    67         robot.mouseMove(point.x, point.y);
       
    68         robot.mousePress(InputEvent.BUTTON1_MASK);
       
    69         robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
    70         robot.waitForIdle();
       
    71 
       
    72         robot.keyPress(KeyEvent.VK_1);
       
    73         robot.keyRelease(KeyEvent.VK_1);
       
    74         robot.waitForIdle();
       
    75 
       
    76         SwingUtilities.invokeAndWait(() -> {
       
    77             point = frame.getLocationOnScreen();
       
    78             point.translate(frame.getWidth() / 2, frame.getHeight() / 2);
       
    79         });
       
    80 
       
    81         robot.mouseMove(point.x, point.y);
       
    82         robot.mousePress(InputEvent.BUTTON1_MASK);
       
    83         robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
    84         robot.waitForIdle();
       
    85 
       
    86         SwingUtilities.invokeAndWait(() -> {
       
    87             testPass = TEST2.equals(table.getValueAt(0, 0));
       
    88             frame.dispose();
       
    89         });
       
    90 
       
    91         if (!testPass) {
       
    92             throw new RuntimeException("Table cell is not edited!");
       
    93         }
       
    94     }
       
    95 
       
    96     private static void createAndShowGUI() {
       
    97         frame = new JFrame();
       
    98         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    99         frame.setSize(200, 200);
       
   100         frame.setLocation(100, 100);
       
   101 
       
   102         table = new JTable(
       
   103                 new String[][]{{TEST1}}, new String[]{"Header"});
       
   104         JComboBox<String> comboBox = new JComboBox<>();
       
   105         comboBox.setEditable(true);
       
   106         table.getColumnModel().getColumn(0).setCellEditor(
       
   107                 new DefaultCellEditor(comboBox));
       
   108         frame.getContentPane().add(table);
       
   109         frame.setVisible(true);
       
   110     }
       
   111 }