1

I'm working on a multi-platform java desktop application (using Swing framework) which needs user's interaction to choose files to load/save, and I'm strugglin' to implement a function that Windows OS provides in his own file Explorer view: the address bar with copy/cut/paste to go in a folder using its path.

What I want is to have an address bar that remains in sync with current directory shown by JFileChooser panel, and I want that any valid path copy-pasted in it will sync the current JFileChooser's viewed path (the current folder displayed, such as in windows file explorer). This behaviour should be triggered when user paste a valid address an then presses the "enter" key.

I've searched for hours but my dear google search it's not helping me this time :(

Can someone help me to implement this in Java? Is this even possible with JFileChooser?

Thanks in advance,

Alessioenter image description here

  • 1
    You may have better luck with [java.awt.FileDialog](https://docs.oracle.com/en/java/javase/13/docs/api/java.desktop/java/awt/FileDialog.html), which is a native file chooser. The trade-off is that it isn’t as customizable as a JFileChooser. – VGR Jan 24 '20 at 02:30

2 Answers2

0

Take a look at this StackOverflow answer, (Windows native File chooser in java) which has the answer you're looking for.

Rubydesic
  • 3,386
  • 12
  • 27
0

at the end I have not found an easy way to have the editable address bar.. so I've wrapped JFileChooser in a custom class, in order to set windows LAF before to open the file chooser, and resetting back the old LAF after closing the dialog.

If you want to have a look, you can find it on my gitHub repository under GuiCommons module (GenericFileChooserDialog.java). It's released under my custom EULA license, but it can be used freely as dependency (soon I want to publish it on maven central repo, and the license may be upgraded to MIT in the future).

enter image description here

Here is a sample of the method used to select a file with custom extension filer:

public File fileRead(Component parent, FileFilter fileFilter) {

    blockEventsModal.set(true);
    LookAndFeel oldLaf = backupLafNDefaultSystem();


    JFileChooser fileChooser = getFileChooser(fileFilter);
    int returnVal = fileChooser.showOpenDialog(parent);
    File correctFile = checkSelectedFile(returnVal, fileChooser);

    resetLaf(oldLaf);
    blockEventsModal.set(false);

    return correctFile;
}

private LookAndFeel backupLafNDefaultSystem() {
    LookAndFeel oldUI = UIManager.getLookAndFeel();
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return oldUI;
}

private void resetLaf(LookAndFeel oldLaf) {
    SwingUtilities.invokeLater(() -> {
        try {
            UIManager.setLookAndFeel(oldLaf);
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    });
}

private JFileChooser getFileChooser(FileFilter fileFilter) {

    JFileChooser fileChooser = new CustomFileChooserOnTop();
    updateFileFilters(fileChooser, fileFilter);
    fileChooser.setDialogTitle("Select input File");
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setCurrentDirectory(new File(currentConf.getFileOpt().getLastSrcFolderPath()));
    fileChooser.setFileView(fileView);
    fileChooser.setPreferredSize(GeneralConfig.FILE_CHOOSER_SIZE);

    return fileChooser;
}

private File checkSelectedFile(int returnVal, JFileChooser fileChooser) {
    File correctFile = null;
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        File selectedFile = fileChooser.getSelectedFile();
        if(selectedFile==null) {
            dialogHelper.error("Please select a file!", "File not selected");
        }else {
            correctFile = selectedFile;
            currentConf.getFileOpt().setLastSrcFolderPath(correctFile.getAbsolutePath());
        }
    }

    return correctFile;
}