gpt4 book ai didi

java - 使用 Windows 外观时 JFileChooser 非常慢

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:06 25 4
gpt4 key购买 nike

当使用 JFileChooser 以及窗口外观时,我的加载时间约为 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);
}
}

最佳答案

我也遇到了同样的问题,至少就我而言,它与 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,它会尝试在额外线程中检索图标,并且仅返回那些立即可用的图标。

关于java - 使用 Windows 外观时 JFileChooser 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com