Complicated
Complicated

Reputation: 117

jFilechooser show folder

I want to show the parent folder of current directory in jfilechooser.
I want to display that folder with .. which refers to the parent folder

Upvotes: 1

Views: 1482

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

This is an "attempt" to implement the functionality that you request, the problem I have is that it's not possible replicate entirely what the system is doing.

Basically, the directory combo box is expecting some kind of native File object (in the case of Windows, a sun.awt.shell.Win32ShellFolder2). But there doesn't seem to be any way by which we can create them from within the provided API (and you won't want to create them manually, as it will break the Look and Feel and cross platform functionality).

enter image description here

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

Upvotes: 2

Ravindra Gullapalli
Ravindra Gullapalli

Reputation: 9188

Use the constructor which takes file path as argument like this.

JFileChooser jfc = new JFileChooser(".\\..");

Check out JFileChooser(File currentDirectory).

Upvotes: 3

Related Questions