JavaVM Windows 7 64bit - JFileChooser() not showing dialog box
Asked Answered
V

1

1

I am trying to create a simple console based java application, which requires users to select files from their local filesystem.

The console prompts the user to select one of the available options and then switches on the input given.

public Client() throws UnknownHostException, IOException {
    printuseroptions();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    char userdecision = br.readLine().charAt(0);

    System.out.println(userdecision);

    switch(userdecision){
        case '1':
            System.out.println("Which file would you like to open?");
            openfile(br.readLine());
            break;
        case '2':
            System.out.println("Which file would you like to close?");
            closefile(br.readLine());
            break;
        }

private boolean openfile(String path){
    System.out.println("openfile("+path+")");
    return false;
}

private boolean closefile(String path){
    System.out.println("closefile("+path+")");
    new JFileChooser().showOpenDialog(null);
    return false;
}

No matter what I do, the JFileChooser pop up box will not open. No error is shown on the console, but a debug step-through shows the following error:

Blockquote Thread [main] (Suspended)
ClassNotFoundException(Throwable).(String, Throwable) line: 217
ClassNotFoundException(Exception).(String, Throwable) line: not available ClassNotFoundException.(String) line: not available
URLClassLoader$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction, AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available
Launcher$ExtClassLoader.findClass(String) line: not available
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader.loadClass(String, boolean) line: not available
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available
ResourceBundle$RBClassLoader.loadClass(String) line: not available
CoreResourceBundleControl(ResourceBundle$Control).newBundle(String, Locale, String, ClassLoader, boolean) line: not available
ResourceBundle.loadBundle(CacheKey, List, Control, boolean) line: not available ResourceBundle.findBundle(CacheKey, List, List, int, Control, ResourceBundle) line: not available
ResourceBundle.getBundleImpl(String, Locale, ClassLoader, ResourceBundle$Control) line: not available
ResourceBundle.getBundle(String, ResourceBundle$Control) line: not available
Toolkit$3.run() line: not available AccessController.doPrivileged(PrivilegedAction) line: not available [native method]
Toolkit.() line: not available
Component.() line: not available
Client.closefile() line: 90 Client.() line: 60
Client.main(String[]) line: 36

The same code runs perfectly on a Linux 32 bit machine, so I suspect the problem is Windows related.

The code below runs as expected on both Windows and Linux so I suspect might be due to the different ways in while console input is handled in Windows vs Linux (CR LF).

import javax.swing.JFileChooser;

public class Example {
    public static void main(String[] args) {
        new JFileChooser().showOpenDialog(null);
    }
}

Thanks

Vantassel answered 12/11, 2010 at 2:51 Comment(0)
C
1

It looks like you're just as new here as I am. ;) Let's see if I can help.

I made changes to your code to get it to compile, and ran it on a Windows Server 2003 x64 machine, and didn't see any problems - the file chooser dialog opens.

I suggest two things you can do to eliminate other possibilities:

1) Ensure the system's native look & feel is set. Set your look & feel to the system default by using this when your program starts: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

2) Ensure you only construct and open your JFileChooserDialog, and all other Swing components, inside the Event Dispatch Thread (EDT). If you know the current thread is the main thread or some other worker thread (and I assume it is because you're taking console input), you need to call SwingUtilities.invokeLater(Runnable) for correct execution.

Good luck with that.

Corvine answered 8/12, 2010 at 13:5 Comment(2)
Hi, The next day after posting the original question I figured out what the problem was. There was no problem! The dialog box was opening, but it was opening behind the IDE, but no java icon was being shown in the taskbar so I never realised that it was actually opening. Thanks again.Vantassel
Yes - I find that happens pretty often in Eclipse, especially with Dialogs and splash screens, which don't have taskbar icons. Calling requestFocusInWindow() on the JFileChooser might help, but I can't be certain.Corvine

© 2022 - 2024 — McMap. All rights reserved.