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