gpt4 book ai didi

java - 为源服务器列出具有 UNC 路径的文件时 UI 缓慢且无响应

转载 作者:行者123 更新时间:2023-11-29 10:15:34 25 4
gpt4 key购买 nike

我需要一份 UNC 路径上所有文件的完整列表。使用 java.io.File.listFiles() 方法列出文件的速度非常慢。当我的应用程序尝试使用 FileSystemView.getSystemIcon 获取文件图标以将其呈现到 JTable 时,它也会卡住。当我在 run 方法下评论特定代码时,它不会卡住。如何摆脱这个问题?

import java.io.File;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;

/**
*
* @author admin
*/
public class MySwingWorker implements Runnable {

private JLabel label;
private String strFile;
private Icon icon;

public MySwingWorker(String strFile, JLabel label) {
this.strFile = strFile;
this.label = label;
}

@Override
public void run() {
File f = new File(strFile);
try {
FileSystemView fw = FileSystemView.getFileSystemView();
if (f.exists() == true) {
icon = fw.getSystemIcon(f);
} else {
File fTemp = new File(System.getProperty("pro.temp.home"), "Temp");
if (fTemp.exists() == false) {
try {
fTemp.mkdirs();
} catch (Exception ex) {
//ignored
}
}
//Replaced "Temp" with fTemp
File fNewFile = new File(fTemp, f.getName());
if (fNewFile.createNewFile()) {
icon = fw.getSystemIcon(fNewFile);
}
}
} catch (Exception e) {
}
label.setIcon(icon);

}
}

最佳答案

I need to have a complete list of all the files over UNC path. Files are listing very slow using java.io.File.listFiles() method. My Application is also getting stuck when it trying to get file icon using FileSystemView.getSystemIcon to render it into JTable. When I comment the particular code under run method it does not get stuck. How to get rid of this issue?

  • 问题一定是在您的接收方的某个地方:-)

  • similair 渲染器概念(JList 和 JTable 之间没有显着变化),

  • 查看 MsExcelMsAccess 以及 *.ini 文件 的图标,这些图标在 JList< 中正确呈现

enter image description here

来自代码

import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

public class FilesInTheJList {

private static final int COLUMNS = 5;
private Dimension size;

public FilesInTheJList() {
final JList list = new JList(new File("C:\\").listFiles()) {

private static final long serialVersionUID = 1L;

@Override
public Dimension getPreferredScrollableViewportSize() {
if (size != null) {
return new Dimension(size);
}
return super.getPreferredScrollableViewportSize();
}
};
list.setFixedCellHeight(50);
list.setFixedCellWidth(150);
size = list.getPreferredScrollableViewportSize();
size.width *= COLUMNS;
list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
list.setCellRenderer(new MyCellRenderer());
list.setVisibleRowCount(0);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JFrame f = new JFrame("Files In the JList");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(list));
f.pack();
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
FilesInTheJList fITJL = new FilesInTheJList();
}
});
}

private static class MyCellRenderer extends JLabel implements ListCellRenderer {

private static final long serialVersionUID = 1L;

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof File) {
File file = (File) value;
setText(file.getName());
setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setPreferredSize(new Dimension(250, 25));
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}
return this;
}
}
}

关于java - 为源服务器列出具有 UNC 路径的文件时 UI 缓慢且无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866466/

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