Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/8021253/bug8021253.java
38853 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.IOException;25import java.awt.BorderLayout;26import java.awt.Robot;27import java.awt.Toolkit;28import java.awt.event.ActionEvent;29import java.awt.event.ActionListener;30import java.awt.event.KeyEvent;31import javax.swing.JFileChooser;32import javax.swing.JFrame;33import javax.swing.SwingUtilities;3435/**36* @test37* @bug 802125338* @author Alexander Scherbatiy39* @summary JFileChooser does not react on pressing enter since java 740* @run main bug802125341*/4243public class bug8021253 {4445private static volatile boolean defaultKeyPressed;46private static JFileChooser fileChooser;47private static File file;4849public static void main(String[] args) throws Exception {5051Robot robot = new Robot();52robot.setAutoDelay(50);5354SwingUtilities.invokeAndWait(new Runnable() {55public void run() {56createAndShowGUI();57}58});5960robot.waitForIdle();6162SwingUtilities.invokeAndWait(new Runnable() {63public void run() {64fileChooser.setSelectedFile(file);65}66});6768robot.waitForIdle();6970robot.keyPress(KeyEvent.VK_ENTER);71robot.keyRelease(KeyEvent.VK_ENTER);72robot.waitForIdle();7374if (!defaultKeyPressed) {75throw new RuntimeException("Default button is not pressed");76}77}7879private static void createAndShowGUI() {8081file = getTempFile();8283final JFrame frame = new JFrame("Test");84frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);85frame.setSize(200, 300);8687fileChooser = new JFileChooser(file.getParentFile());88fileChooser.addActionListener(new ActionListener() {89@Override90public void actionPerformed(ActionEvent e) {91defaultKeyPressed = true;92frame.dispose();93}94});9596frame.getContentPane().add(BorderLayout.CENTER, fileChooser);97frame.setSize(fileChooser.getPreferredSize());98frame.setVisible(true);99}100101private static File getTempFile() {102try {103File temp = File.createTempFile("test", ".txt");104temp.deleteOnExit();105return temp;106} catch (IOException ex) {107throw new RuntimeException(ex);108}109}110}111112113