1

我一直在实现一个JFileChooser作为远程文件系统的视图。当getFiles()FileSystemViewI 使用目录位置数据发送请求到远程系统时调用。

然后我异步接收到包含我正在浏览的目录中所有文件的数据包,之后我设置文件准备更新视图(这样下次getFiles()调用它将返回接收到的文件数组)但问题是我没有不知道如何更新JFileChoosers视图。

我试过了

fileChooser.updateUI();

但它会引发以下异常:

Exception in thread "pool-1-thread-31" java.lang.NullPointerException
    at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.setDirectorySelected(Unknown Source)
    at javax.swing.plaf.basic.BasicFileChooserUI$Handler.valueChanged(Unknown Source)
    at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
    at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.moveLeadSelectionIndex(Unknown Source)
    at sun.swing.FilePane.clearSelection(Unknown Source)
    at sun.swing.FilePane.doFilterChanged(Unknown Source)
    at sun.swing.FilePane.propertyChange(Unknown Source)
    at java.beans.PropertyChangeSupport.fire(Unknown Source)
    at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
    at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
    at java.awt.Component.firePropertyChange(Unknown Source)
    at javax.swing.JFileChooser.setFileFilter(Unknown Source)
    at javax.swing.JFileChooser.addChoosableFileFilter(Unknown Source)
    at javax.swing.JFileChooser.updateUI(Unknown Source)
    at com.ruuhkis.remoteserver.ui.RemoteView.updateFiles(RemoteView.java:252)
    at com.ruuhkis.remoteserver.ui.RemoteApplication.onFileListReceived(RemoteApplication.java:122)
    at com.ruuhkis.remoteserver.packets.impl.FileListPacket.handlePacket(FileListPacket.java:32)
    at com.ruuhkis.remoteserver.packets.PacketHandler$1.run(PacketHandler.java:57)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

当我在目录中并且我知道它已收到新文件列表并按 f5 时,它将显示刚刚加载的新文件,我怎样才能使它不必按 f5?此外,按 f5 会导致系统重新加载目录的预先存在的数据。

当前代码基本上是:

    view = new FileSystemView() {

        int c;

        @Override
        public File[] getFiles(File dir, boolean useFileHiding) {

            if(!auto) {
                PacketBuilder builder = new PacketBuilder(OpCodes.FILE_LIST_REQUEST_PACKET.getOpCode());
                root = dir.getAbsolutePath();
                builder.writeString(dir.getAbsolutePath());
                builder.write(RemoteView.this.remote.getChannel());
            }
            auto = false;



            if(dirContent == null)
                return new File[]{new File((c++) + ".txt")};
            else
                return dirContent;
        }

        @Override
        public Boolean isTraversable(File arg0) {
            return true;
        }

        @Override
        public File createNewFolder(File arg0) throws IOException {
            // TODO Auto-generated method stub
            return null;
        }
    };

c 基本上只是用于测试,所以我可以查看系统是否刷新

当我收到文件列表时,我会这样做:

public void updateFiles(String list) {
    String[] parts = list.split("" + ((char)10));
    File[] files = new File[parts.length];

    for(int i = 0 ; i < parts.length; i++) {
        files[i] = new File(root + File.separatorChar + parts[i]);

    }



    dirContent = files;

    fileChooser.setCurrentDirectory(new File(root));

    fileChooser.updateUI();

    auto = true;

}
4

2 回答 2

3
于 2012-10-16T12:08:22.300 回答
2

浏览其他 JFileChooser 项目后,我找到了方法

fileChooser.rescanCurrentDirectory();

这似乎更新了文件系统视图..

每次我问一个问题,我都会在不久之后找到答案:/

于 2012-10-16T12:07:48.083 回答