Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jFilechooser显示文件夹_Java_Swing_Jfilechooser - Fatal编程技术网

Java jFilechooser显示文件夹

Java jFilechooser显示文件夹,java,swing,jfilechooser,Java,Swing,Jfilechooser,我想在jfilechooser中显示当前目录的父文件夹。 我想用..显示该文件夹。。引用父文件夹的构造函数使用以文件路径为参数的构造函数,如下所示 JFileChooser jfc=新的JFileChooser.\\ 签出。使用以文件路径为参数的构造函数,如下所示 JFileChooser jfc=新的JFileChooser.\\ 检查。这是一次尝试实现您请求的功能,我的问题是不可能完全复制系统正在执行的操作 基本上,对于Windows,目录组合框需要某种本机文件对象,即sun.awt.she

我想在jfilechooser中显示当前目录的父文件夹。
我想用..显示该文件夹。。引用父文件夹的构造函数使用以文件路径为参数的构造函数,如下所示

JFileChooser jfc=新的JFileChooser.\\


签出。

使用以文件路径为参数的构造函数,如下所示

JFileChooser jfc=新的JFileChooser.\\


检查。

这是一次尝试实现您请求的功能,我的问题是不可能完全复制系统正在执行的操作

基本上,对于Windows,目录组合框需要某种本机文件对象,即sun.awt.shell.Win32ShellFolder2。但是似乎没有任何方法可以从提供的API中创建它们,您也不希望手动创建它们,因为这会破坏外观和跨平台功能

import core.util.MethodInvoker;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.plaf.ComponentUI;

public class TestFileChooser {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(UIManager.getSystemLookAndFeelClassName());
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFileChooser fc = new JFileChooser(new MyFileSystemView());
                fc.showOpenDialog(null);

            }
        });
    }

    public static class MyFileSystemView extends FileSystemView {

        @Override
        public File[] getFiles(File dir, boolean useFileHiding) {
            File[] files = super.getFiles(dir, useFileHiding);

            List<File> fileList = new ArrayList<>(Arrays.asList(files));
            if (!isFileSystemRoot(dir)) {
                File newPath = FileSystemView.getFileSystemView().createFileObject(dir, "/..");
                fileList.add(0, newPath);
            }
            files = fileList.toArray(files);

            return files;
        }

        @Override
        public File createNewFolder(File containingDir) throws IOException {
            File newFolder = new File(containingDir + File.separator + "New Folder");
            if (!newFolder.mkdir()) {
                newFolder = null;
            }
            return newFolder;
        }
    }
}

这是为了实现您请求的功能,我的问题是不可能完全复制系统正在做的事情

基本上,对于Windows,目录组合框需要某种本机文件对象,即sun.awt.shell.Win32ShellFolder2。但是似乎没有任何方法可以从提供的API中创建它们,您也不希望手动创建它们,因为这会破坏外观和跨平台功能

import core.util.MethodInvoker;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.plaf.ComponentUI;

public class TestFileChooser {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(UIManager.getSystemLookAndFeelClassName());
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFileChooser fc = new JFileChooser(new MyFileSystemView());
                fc.showOpenDialog(null);

            }
        });
    }

    public static class MyFileSystemView extends FileSystemView {

        @Override
        public File[] getFiles(File dir, boolean useFileHiding) {
            File[] files = super.getFiles(dir, useFileHiding);

            List<File> fileList = new ArrayList<>(Arrays.asList(files));
            if (!isFileSystemRoot(dir)) {
                File newPath = FileSystemView.getFileSystemView().createFileObject(dir, "/..");
                fileList.add(0, newPath);
            }
            files = fileList.toArray(files);

            return files;
        }

        @Override
        public File createNewFolder(File containingDir) throws IOException {
            File newFolder = new File(containingDir + File.separator + "New Folder");
            if (!newFolder.mkdir()) {
                newFolder = null;
            }
            return newFolder;
        }
    }
}

jfilechooser.setCurrentDirectoryjfilechooser.getCurrentDirectory.getParent??这将引发一个空指针异常,如果你在一个根级别的文件夹中…就像其他文件夹一样,当我们进入任何目录时,我也要显示父目录…当任何人单击该文件夹时。。它将移出当前目录并进入其父目录。因此,您想显示类似于树的内容,以父文件夹作为根节点,其中的文件作为子节点?@MadProgrammer我猜OP想显示该文件夹,并使用。。它指向父文件夹。@问题我可以使它工作,但您将从俯视图中丢失扩展目录选择。jfilechooser.setCurrentDirectoryjfilechooser.getCurrentDirectory.getParent??这将引发一个空指针异常,如果你在一个根级别的文件夹中…就像其他文件夹一样,当我们进入任何目录时,我也要显示父目录…当任何人单击该文件夹时。。它将移出当前目录并进入其父目录。因此,您想显示类似于树的内容,以父文件夹作为根节点,其中的文件作为子节点?@MadProgrammer我猜OP想显示该文件夹,并使用。。它指的是父文件夹。@问题我可以让它工作,但您将丢失从顶部视图选择的扩展目录。我们现在使用Java 7,Java 1.4.2对大多数人来说有点过时…我知道我们现在使用Java 7,Java 1.4.2对大多数人来说有点过时…我知道