JFileChooser远程查看

时间:2012-10-16 11:59:18

标签: java swing file-io filesystems jfilechooser

我一直在实现JFileChooser作为远程文件系统的视图。从getFiles()调用FileSystemView时,我向目标位置数据发送请求到远程系统。

然后我异步收到包含我正在浏览的目录中的所有文件的数据包,之后我将文件设置为更新视图(以便下次调用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;

}

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

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

fileChooser.rescanCurrentDirectory();

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

每次我问一个问题后我很快就会回答:/