I am using a JFileChooser
to let the user select their folder.
The user triggered execution somewhere they did not expect because the JFileChooser
provided a default value in the Folder Name:
field. Specifically, they were trying to click on something else, but the JFileChooser
popped up right under where they were planning to click, causing them to click on the Open
button.
Normally, I would work around this by simply checking if the directory they chose is the default directory chosen by JFileChooser
, but sometimes the user wants to run stuff in there too. Just not at that particular time.
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
. It becomes much more obvious that the user did not mean to select anything. Avoiding yet another popup is better for usability.
Alternatively, I will accept the ability to turn off the Open
button until they make some for of a selection on the dialog.
Finally, I am aware of the option to extend JFileChooser
to add some custom logic. That seems like what my final solution will be, and if you would like to provide an implementation, that would be icing on the cake. However, priority will be given to an answer that allows me to simply clear out the Folder Name:
field upon dialog popup (or to disable the button) without needing to do inheritance.
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?
}
}
If users are accidentally clicking on your dialog, it sounds like your dialog is taking them by surprise. Applications should almost never display dialogs unless in direct response to a user action.
Popping up a dialog on top of whatever else the user may have been working on is, well, rude. It’s like yanking a book out of their hands while they’re reading it. They were in the middle of something and another application decided to steal the focus and take over.
A good practice is to display a file chooser in response to the user’s pressing, for example, a Load or Save button (or menu item). In that case, your application already has focus, and the dialog is part of the very workflow (your application’s workflow) which already has the user’s full attention.