当使用JFileChooser以及Windows外观时,我的加载时间约为21-40秒。如果我删除外观并感觉代码运行得非常快。我想问题是在初始化new JFileChooser("path");
时。有人能帮我吗 ?这是我的代码
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MainClass {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
private static void createGUI() throws HeadlessException {
final JFrame frame = new JFrame("JFileChooser Demo");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File("./feedback"));
JButton btn1 = new JButton("Show Dialog");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fc.showDialog(frame, "Choose");
}
});
Container pane = frame.getContentPane();
pane.setLayout(new GridLayout(3, 1, 10, 10));
pane.add(btn1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
答案 0 :(得分:0)
多年前的一个解决方案是使用java.awt FileChooser。 好像似乎在后台预加载共享的JFileChooser:
带有未来的字段:
FutureTask<JFileChooser> futureFileChooser = new FutureTask<>(JFileChooser::new);
然后在初始化时:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(futureFileChooser);
因此,当最终需要JFileChooser时,请从Future获取它。
JFileChooser fileChooser = futureFileChooser.get();
如果在加载时间内完成,它仍然会阻塞,但已经用了更短的时间。当第一件事就是打开JFileChooser时,这个解决方案将不起作用。
如果想要原生外观,我可能无法说服你切换到JavaFX。
答案 1 :(得分:0)
这段代码帮助了我,从那以后我没有发现任何问题。
WindowsFileChooserUI wui = new WindowsFileChooserUI(filechosser);
wui.installUI(filechosser);
答案 2 :(得分:0)
我遇到了同样的问题,至少在我看来,它与Windows或Java版本无关。
在创建JFileChooser期间, Windows外观已经尝试通过询问所有可见项目的图标(包括网络共享的图标)来计算当前目录组合框的大小。就我而言,我的某些网络共享不可用。
那是我在Java 8和Windows 10上的堆栈跟踪:
Thread [AWT-EventQueue-0]
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: 175
FutureTask<V>.awaitDone(boolean, long) line: 429
FutureTask<V>.get() line: 191
Win32ShellFolderManager2$ComInvoker.invoke(Callable<T>) line: 582
ShellFolder.invoke(Callable<T>, Class<E>) line: 518
ShellFolder.invoke(Callable<T>) line: 504
Win32ShellFolder2.getIcon(boolean) line: 968
WindowsFileSystemView(FileSystemView).getSystemIcon(File) line: 243
WindowsFileChooserUI$WindowsFileView.getIcon(File) line: 1300
JFileChooser.getIcon(File) line: 1609
WindowsFileChooserUI$DirectoryComboBoxRenderer.getListCellRendererComponent(JList, Object, int, boolean, boolean) line: 998
BasicListUI.updateLayoutState() line: 1361
BasicListUI.maybeUpdateLayoutState() line: 1311
BasicListUI$Handler.valueChanged(ListSelectionEvent) line: 2623
DefaultListSelectionModel.fireValueChanged(int, int, boolean) line: 184
DefaultListSelectionModel.fireValueChanged(int, int) line: 164
DefaultListSelectionModel.fireValueChanged() line: 211
DefaultListSelectionModel.changeSelection(int, int, int, int, boolean) line: 405
DefaultListSelectionModel.changeSelection(int, int, int, int) line: 415
DefaultListSelectionModel.setSelectionInterval(int, int) line: 459
BasicComboPopup$1(JList<E>).setSelectedIndex(int) line: 2216
BasicComboPopup.setListSelection(int) line: 1186
BasicComboPopup.access$300(BasicComboPopup, int) line: 65
BasicComboPopup$Handler.itemStateChanged(ItemEvent) line: 1015
WindowsFileChooserUI$2(JComboBox<E>).fireItemStateChanged(ItemEvent) line: 1223
WindowsFileChooserUI$2(JComboBox<E>).selectedItemChanged() line: 1280
WindowsFileChooserUI$2(JComboBox<E>).contentsChanged(ListDataEvent) line: 1330
WindowsFileChooserUI$DirectoryComboBoxModel(AbstractListModel<E>).fireContentsChanged(Object, int, int) line: 118
WindowsFileChooserUI$DirectoryComboBoxModel.setSelectedItem(Object) line: 1140
WindowsFileChooserUI$DirectoryComboBoxModel.addItem(File) line: 1111
WindowsFileChooserUI$DirectoryComboBoxModel.access$800(WindowsFileChooserUI$DirectoryComboBoxModel, File) line: 1041
WindowsFileChooserUI.doDirectoryChanged(PropertyChangeEvent) line: 730
WindowsFileChooserUI.access$1100(WindowsFileChooserUI, PropertyChangeEvent) line: 55
WindowsFileChooserUI$11.propertyChange(PropertyChangeEvent) line: 821
PropertyChangeSupport.fire(PropertyChangeListener[], PropertyChangeEvent) line: 335
PropertyChangeSupport.firePropertyChange(PropertyChangeEvent) line: 327
PropertyChangeSupport.firePropertyChange(String, Object, Object) line: 263
JFileChooser(Component).firePropertyChange(String, Object, Object) line: 8434
JFileChooser.setCurrentDirectory(File) line: 598
JFileChooser.<init>(File, FileSystemView) line: 344
JFileChooser.<init>(File) line: 326
FileChooser.<clinit>() line: 15
...
所以我看到了两种解决方案:
如Joop Eggen所述,很早就初始化文件选择器,或者安装自己的UI,该UI尝试在多余的线程中检索图标,并且仅返回立即可用的那些图标。
答案 3 :(得分:0)
对我来说,解决方案是从桌面上删除 LibreOffice Writer 和 Calc 的快捷方式,这些快捷方式是在安装过程中自动生成的。手动创建了另一个快捷方式(通过右键单击拖放),这些快捷方式并没有减慢 FileChooser 的打开速度。