如何使JFileChooser在断开网络驱动器的情况下正常运行? [英] How can I make JFileChooser behave properly with disconnected network drives?

查看:57
本文介绍了如何使JFileChooser在断开网络驱动器的情况下正常运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在具有断开连接的网络驱动器的系统上创建JFileChooser时,要花很长时间才能显示JFileChooser.通过单个断开连接的网络驱动器,它可以阻止EDT大约20秒钟.

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:

  • 使用JFileDialog
  • 将FileSystemView替换为其中的getRoots()返回固定驱动器列表的一个
  • 在尝试打开JFileChooser之前切换到WAIT游标
  • 在启动时创建一个JFileChooser并将其永久保存
  • 尝试使用 xfile对话框
  • 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.

还有其他建议吗?

也许是在13年前提交错误报告的人在这里闲逛,现在已经知道了.

Maybe the guy who submitted this bug report 13 years ago is hanging around here and has it figured out by now.

这是我正在使用的代码.

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.

以下是上面的代码在Windows 7上与断开连接的网络驱动器一起运行时的日志记录输出.我正在通过使用 ExpanDrive (1.8.4)连接到Linux计算机,并在初始连接后关闭Linux计算机.

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.

推荐答案

您阻止了EDT,如果Network Drive不可访问或返回异常,则可能带有RepaintManager异常,您必须

you blocked EDT, maybe with RepaintManager exceptions in the case that Network Drive isn't accesible or returns an exception, your have to

  • 使用SwingWorkerRunnable#Thread

通知用户"Please wait, whatever ...."

如果成功,则显示JFileChooserFiles

否则,GUI冻结,直到网络驱动器返回内容或异常

otherwise you GUI freeze until network drive returns content or exception

这篇关于如何使JFileChooser在断开网络驱动器的情况下正常运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆