Path: blob/master/test/jdk/javax/swing/JFileChooser/8021253/bug8021253.java
41153 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* @key headful38* @bug 802125339* @author Alexander Scherbatiy40* @summary JFileChooser does not react on pressing enter since java 741* @run main bug802125342*/4344public class bug8021253 {4546private static volatile boolean defaultKeyPressed;47private static JFileChooser fileChooser;48private static File file;49private static JFrame frame;5051public static void main(String[] args) throws Exception {52try {53Robot robot = new Robot();54robot.setAutoDelay(100);5556SwingUtilities.invokeAndWait(new Runnable() {57public void run() {58createAndShowGUI();59}60});6162robot.waitForIdle();63robot.delay(1000);6465SwingUtilities.invokeAndWait(new Runnable() {66public void run() {67fileChooser.setSelectedFile(file);68}69});7071robot.waitForIdle();7273robot.keyPress(KeyEvent.VK_ENTER);74robot.keyRelease(KeyEvent.VK_ENTER);75robot.waitForIdle();7677if (!defaultKeyPressed) {78throw new RuntimeException("Default button is not pressed");79}80} finally {81if (frame != null) {82SwingUtilities.invokeAndWait(frame::dispose);83}84}85}8687private static void createAndShowGUI() {8889file = getTempFile();9091frame = new JFrame("Test");92frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);93frame.setSize(200, 300);9495fileChooser = new JFileChooser(file.getParentFile());96fileChooser.addActionListener(new ActionListener() {97@Override98public void actionPerformed(ActionEvent e) {99defaultKeyPressed = true;100frame.dispose();101}102});103104frame.getContentPane().add(BorderLayout.CENTER, fileChooser);105frame.setSize(fileChooser.getPreferredSize());106frame.setLocationRelativeTo(null);107frame.setVisible(true);108}109110private static File getTempFile() {111try {112File temp = File.createTempFile("test", ".txt");113temp.deleteOnExit();114return temp;115} catch (IOException ex) {116throw new RuntimeException(ex);117}118}119}120121122