On Windows, Scanner interferes w/ JFileChooser -- why?
tl; dr , strictly speaking, is a user who interferes with JFileChooser , using Scanner JFileChooser for input from the system console ( System.in ). In any case, this concerns the focus of windows and the features of Java Dialog .
Explanation
The error occurs because the dialog appears in the background, because the nextLine() requirement in Scanner on System.in essentially forces the user to switch focus to the console. The application loses focus, so Dialog appears in the background. The code does not “freeze” on its own, it just waits until the user selects a file or makes some other dialog parameter — until he does, he does nothing. If there is some problem with the OS that prevents the background window from being displayed / displayed correctly (for example, a certain window “always in the foreground” prevents this) - well, your application hangs around, waiting for an input event, which is unlikely to happen due to fact no one cannot use the input dialog itself.
For anyone affected - Dialog . I tested this code on Java 8 in Windows XP, Java 8 in Windows 7 and some other configurations - in all cases Dialog was created, possibly hidden in the background of the desktop due to loss of focus. It does not appear in the taskbar, but it appears in the Alt + Tab list. Try to run it outside the IDE, they usually do strange things with the focus of the application when console input is required (due to the "real" console they are redirected to the IDE output window, etc.). Piping data through the pipe < instead of using a "real" console will prevent this behavior.
As far as my imagination is concerned, workarounds will require either focusing on the application framework or creating a permanent frame on the fly to maintain focus where it should be (in the dialog box). Since Dialog of JFileChooser (or any other similar dialogue that can be talked about) is itself a “fire and oblivion” object, it is difficult to get it to the fore without specifying a parent. Therefore, I think that the easiest way to go without a parent Dialog is to simply use the code, for example:
Sample solution
Scanner scan = new Scanner( System.in ); System.out.print( "Enter something and press Enter: " ); scan.nextLine(); scan.close(); SwingUtilities.invokeLater( new Runnable() { public void run() { JFrame jf = new JFrame( "Dialog" ); // added jf.setAlwaysOnTop( true ); // added JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog( jf ); // changed //System.out.print( "result: " + result ); jf.dispose(); // added } } );
or, if you prefer to interfere with Dialog itself, subclassing it and applying the above setAlwaysOnTop(true) in the createDialog() call (note that createDialog() has protected access in JFileChooser , which makes it impossible to change the behavior without extending the class, like all related Dialog stuff) e.g.
int result = new JFileChooser() { @Override protected JDialog createDialog( Component parent ) throws HeadlessException { JDialog jDialog = super.createDialog( parent ); jDialog.setAlwaysOnTop( true ); return jDialog; } }.showOpenDialog( null );
or even by creating a regular utility class that extends JFileChooser to do just that.
<sub> NB switching to / EDT does nothing here, as it is not related to the thread itself (although multithreading the UI is a source of a problem of sorts). Sub>