1 /*
   2  * Copyright (c) 2013, 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 import java.io.File;
  25 import java.io.IOException;
  26 import java.awt.BorderLayout;
  27 import java.awt.Robot;
  28 import java.awt.Toolkit;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.ActionListener;
  31 import java.awt.event.KeyEvent;
  32 import javax.swing.JFileChooser;
  33 import javax.swing.JFrame;
  34 import javax.swing.SwingUtilities;
  35 
  36 /**
  37  * @test
  38  * @bug 8021253
  39  * @author Alexander Scherbatiy
  40  * @summary JFileChooser does not react on pressing enter since java 7
  41  * @run main bug8021253
  42  */
  43 
  44 public class bug8021253 {
  45 
  46     private static volatile boolean defaultKeyPressed;
  47     private static JFileChooser fileChooser;
  48     private static File file;
  49 
  50     public static void main(String[] args) throws Exception {
  51 
  52         Robot robot = new Robot();
  53         robot.setAutoDelay(50);
  54 
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56             public void run() {
  57                 createAndShowGUI();
  58             }
  59         });
  60 
  61         robot.waitForIdle();
  62 
  63         SwingUtilities.invokeAndWait(new Runnable() {
  64             public void run() {
  65                 fileChooser.setSelectedFile(file);
  66             }
  67         });
  68 
  69         robot.waitForIdle();
  70 
  71         robot.keyPress(KeyEvent.VK_ENTER);
  72         robot.keyRelease(KeyEvent.VK_ENTER);
  73         robot.waitForIdle();
  74 
  75         if (!defaultKeyPressed) {
  76             throw new RuntimeException("Default button is not pressed");
  77         }
  78     }
  79 
  80     private static void createAndShowGUI() {
  81 
  82         file = getTempFile();
  83 
  84         final JFrame frame = new JFrame("Test");
  85         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86         frame.setSize(200, 300);
  87 
  88         fileChooser = new JFileChooser(file.getParentFile());
  89         fileChooser.addActionListener(new ActionListener() {
  90             @Override
  91             public void actionPerformed(ActionEvent e) {
  92                 defaultKeyPressed = true;
  93                 frame.dispose();
  94             }
  95         });
  96 
  97         frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
  98         frame.setSize(fileChooser.getPreferredSize());
  99         frame.setVisible(true);
 100     }
 101 
 102     private static File getTempFile() {
 103         try {
 104             File temp = File.createTempFile("test", ".txt");
 105             temp.deleteOnExit();
 106             return temp;
 107         } catch (IOException ex) {
 108             throw new RuntimeException(ex);
 109         }
 110     }
 111 }