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 import sun.awt.SunToolkit; 36 37 /** 38 * @test 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 45 public 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 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 54 Robot robot = new Robot(); 55 robot.setAutoDelay(50); 56 57 SwingUtilities.invokeAndWait(new Runnable() { 58 public void run() { 59 createAndShowGUI(); 60 } 61 }); 62 63 toolkit.realSync(); 64 65 SwingUtilities.invokeAndWait(new Runnable() { 66 public void run() { 67 fileChooser.setSelectedFile(file); 68 } 69 }); 70 71 toolkit.realSync(); 72 73 robot.keyPress(KeyEvent.VK_ENTER); 74 robot.keyRelease(KeyEvent.VK_ENTER); 75 toolkit.realSync(); 76 77 if (!defaultKeyPressed) { 78 throw new RuntimeException("Default button is not pressed"); 79 } 80 } 81 82 private static void createAndShowGUI() { 83 84 file = getTempFile(); 85 86 final JFrame frame = new JFrame("Test"); 87 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 88 frame.setSize(200, 300); 89 90 fileChooser = new JFileChooser(file.getParentFile()); 91 fileChooser.addActionListener(new ActionListener() { 92 @Override 93 public void actionPerformed(ActionEvent e) { 94 defaultKeyPressed = true; 95 frame.dispose(); 96 } 97 }); 98 99 frame.getContentPane().add(BorderLayout.CENTER, fileChooser); 100 frame.setSize(fileChooser.getPreferredSize()); 101 frame.setVisible(true); 102 } 103 104 private static File getTempFile() { 105 try { 106 File temp = File.createTempFile("test", ".txt"); 107 temp.deleteOnExit(); 108 return temp; 109 } catch (IOException ex) { 110 throw new RuntimeException(ex); 111 } 112 } 113 }