1

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?

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);

    }
}
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88

3 Answers3

3

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?

  • issue must be somewhere on your receivers side :-)

  • similair renderers concept (there isn't significant changes between JList and JTable),

  • see icons for MsExcell and MsAccess and *.ini file, those icons are correctly rendered in JList

enter image description here

from code

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;
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I have replied to my own answer. The question is still open. You are requested to look and give your thoughts on my answer. Thanks – Ashish Pancholi Sep 18 '13 at 13:34
3

As a practical mater, concrete implementations of FileSystemView may leak resources managed by the host platform. Instead, create a single instance and use its icons in your renderer, as shown here and here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Yes. It's true - `As a practical mater, concrete implementations of FileSystemView may leak resources managed by the host platform.` and thanks for your suggestion to use plain same icon for all the files. – Ashish Pancholi Sep 18 '13 at 13:31
  • I have replied to my own answer. The question is still open. You are requested to look and give your thoughts on my answer. Thanks – Ashish Pancholi Sep 18 '13 at 13:33
2

One of my issue in which UI was unresponsive while getting the file icon, looks like it get solved. It was getting stuck because, may be it takes time, when it's trying to get the file icon which is on the remote machine. So I have created the file into the temp with the same name & extension and call icon = fw.getSystemIcon(f); on that temp file to render icon into the my JTable.

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 fTemp = new File(System.getProperty("pro.temp.home"), "Temp");
        if (fTemp.exists() == false) {
            try {
                fTemp.mkdirs();
            } catch (Exception ex) {
                //ignored
            }
        }
        File f = new File(fTemp, strFile);
        FileSystemView fw = FileSystemView.getFileSystemView();
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            icon = fw.getSystemIcon(f);

            try {
                org.apache.commons.io.FileUtils.forceDelete(f);
            } catch (Exception ex) {
            }
        } catch (Exception e) {
            //ignore
        }
        // set a default icon in case if unable to get the icon
        if (icon == null) {
            f = new File(fTemp, String.valueOf("Hello.txt"));
            try {
                if (!f.exists()) {
                    f.createNewFile();
                }
                icon = fw.getSystemIcon(f);

                try {
                    org.apache.commons.io.FileUtils.forceDelete(f);
                } catch (Exception ex) {
                    //ignore
                }
            } catch (Exception e) {
                //ignore
            }
        }
        label.setIcon(icon);

    }
}
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88