I am using a
Код: Выделить всё
JFileChooser
The user triggered execution somewhere they did not expect because the
Код: Выделить всё
JFileChooser
Код: Выделить всё
Folder Name:
Код: Выделить всё
JFileChooser
Код: Выделить всё
Open
Normally, I would work around this by simply checking if the directory they chose is the default directory chosen by
Код: Выделить всё
JFileChooser
Technically, I could still pop up a warning if the chosen is the same as the default. But I feel like a cleaner solution would be to simply start off with blank until the user selects a folder on the
Код: Выделить всё
JFileChooser

Alternatively, I will accept the ability to turn off the
Код: Выделить всё
Open
Finally, I am aware of the option to extend
Код: Выделить всё
JFileChooser
Код: Выделить всё
Folder Name:
Feel free to suggest alternative solutions if you see a better way forward. I already have a working solution (popup a warning to the user, or use inheritance), but would like a better one if possible.
And here is a complete, runnable example.
Код: Выделить всё
import javax.swing.JFileChooser;
import java.io.File;
import java.nio.file.Path;
public class SOQ_20240309_132700
{
public static void main(String[] args)
{
JFileChooser c = new JFileChooser();
File dir = Path.of(".").toFile(); //Just pointing to my current directory for example sake. Replace if you like.
c.setCurrentDirectory(dir); //I need to set the location where the chooser starts from.
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.setSelectedFile(null); //This doesn't seem to clear out the box
c.showOpenDialog(null);
//How do I make the chooser have a starting directory without populating foldername?
}
}
Источник: https://stackoverflow.com/questions/781 ... the-folder