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