1

A JFileChooser used to select a directory is initialized using:

JFileChooser directoryChooser = new JFileChooser();
directoryChooser.setDialogTitle("Choose Directory");
directoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
directoryChooser.setAcceptAllFileFilterUsed(false);

and opened using:

directoryChooser.updateUI();
directoryChooser.showOpenDialog(null);
File selectedFile = directoryChooser.getSelectedFile();

which works and i can select a directory but i don't like it's appearance:

enter image description here

I would like it to have the same appearance as the DirectoryChooser in JavaFx, which is also the same appearance for example as the open/save dialog in Chrome & Firefox. That would also make it possible to enter the path manually.

enter image description here

Is it possible to achieve what i want without using JavaFx and if so how can i change it's appearance?

TheDoctor
  • 2,362
  • 4
  • 22
  • 39
  • 3
    Short answer is, yes, long answer is, probably not worth the effort. The reason I say that is because Swing is using it's own implementation to "replicate" the file dialog, where as JavaFX is probably using the native file dialog. What you're asking isn't impossible, it's just an awful lot of work, which probably won't offer you much more gain. If you're really hell bent on the idea, then you need to provide your own look and feel implementation of the file dialog (based on your requirements), which is a simple feat. – MadProgrammer Sep 16 '19 at 23:20
  • 3
    Instead, you could do something like [this](https://stackoverflow.com/questions/39819319/windows-native-file-chooser-in-java) or try one of the native implements, like [this](https://dzone.com/articles/native-dialogs-swing-little) or [this](https://github.com/steos/jnafilechooser) or [this](https://github.com/veluria/NativeJFileChooser) – MadProgrammer Sep 16 '19 at 23:21

1 Answers1

1

Update

I noticed that you edited your question to include the text "without using JavaFx". As this answer is using JavaFX, and you don't wish to use that technology, you can ignore it.


As you state "I would like it to have the same appearance as the DirectoryChooser in JavaFx", then you may as well just use the DirectoryChooser from JavaFX from within your Swing application.

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.DirectoryChooser;

import javax.swing.*;
import java.io.File;

public class SwingWithJavaFXDirectoryChooser {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // creating a new JFXPanel (even if we don't use it), will initialize the JavaFX toolkit.
        new JFXPanel();

        DirectoryChooser directoryChooser = new DirectoryChooser();

        JButton button = new JButton("Choose directory");
        button.addActionListener(e ->
            // runLater is called to create the directory chooser on the JavaFX application thread.
            Platform.runLater(() -> {
                File selectedDirectory = directoryChooser.showDialog(null);
                System.out.println(selectedDirectory);
            })
        );
        frame.getContentPane().add(button);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(SwingWithJavaFXDirectoryChooser::createAndShowGUI);
    }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406