When I create a JFileChooser on a system with a disconnected network drive it takes forever before the JFileChooser gets displayed. It blocks the EDT for about 20s with a single disconnected network drive.
It seems like it would be a common problem, but I can't find a way to work around it. Possible options I've seen are:
- use JFileDialog
- replace FileSystemView with one where getRoots() returns a fixed list of drives
- switch to a WAIT cursor before trying to open JFileChooser(s)
- create a JFileChooser at startup and keep it around forever
- try using xfiledialog
My use case is so simple that I hate to resort to a third party library. The other options all seem pretty bad to me though.
Any other suggestions?
Maybe the guy who submitted this bug report 13 years ago is hanging around here and has it figured out by now.
Updated With Code and Logging Output
Here's the code I'm using for this.
private void initFileChooser() {
log.debug("Initializing fileChooser.");
fileChooser = new JFileChooser();
log.debug("FileChooser instantiation complete.");
fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
log.debug("File filter setup complete.");
fileChooser.setMultiSelectionEnabled(false);
log.debug("Multi-selection disabled.");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
log.debug("File selection mode set to files only.");
}
Here's the logging output when the above code runs with no disconnected network drives.
05:37:13.126 [AWT-EventQueue-0] DEBUG - Initializing fileChooser.
05:37:13.481 [AWT-EventQueue-0] DEBUG - FileChooser instantiation complete.
05:37:13.482 [AWT-EventQueue-0] DEBUG - File filter setup complete.
05:37:13.482 [AWT-EventQueue-0] DEBUG - Multi-selection disabled.
05:37:13.482 [AWT-EventQueue-0] DEBUG - File selection mode set to files only.
Here's the logging output when the above code runs with a disconnected network drive on Windows 7. I'm creating a disconnected network drive by using ExpanDrive (1.8.4) to connect to a Linux machine and shutting down the Linux machine after the initial connection is made.
05:37:58.953 [AWT-EventQueue-0] DEBUG - Initializing fileChooser.
05:38:11.108 [AWT-EventQueue-0] DEBUG - FileChooser instantiation complete.
05:38:11.110 [AWT-EventQueue-0] DEBUG - File filter setup complete.
05:38:11.110 [AWT-EventQueue-0] DEBUG - Multi-selection disabled.
05:38:11.110 [AWT-EventQueue-0] DEBUG - File selection mode set to files only.