Как заставить JFileChooser (DIRECTORIES_ONLY) начинаться с пустого имени папки?JAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Как заставить JFileChooser (DIRECTORIES_ONLY) начинаться с пустого имени папки?

Сообщение Гость »


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 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 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?

}

}


Источник: https://stackoverflow.com/questions/781 ... the-folder
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»