Hi, first of all thank ou for this piece of software, it will be very useful for me :)
However, I think I found a small bug.
It occurs for me, running Ubuntu Linux 21.10 Impish (I don't know if that makes a difference anywhere).
How it normally works
When I use FXFileChooserDialog#showOpenDialog
this way, it works:
- select the file I want
- close the dialog with the "OK" button
- resullt: the file got chosen, it is returned as an Optional
What I think is broken
When I instead do this I get an empty optional as the result:
- select the flie I want
- double-click the file
- resullt: an empty Optional gets returned
I don't exactly know why this happens - I debugged a bit, and double-click handling in net.raumzeitfalle.fx.filechooser.FileChooserController#handleDoubleClickInFilesList
definitely gets triggered.
Just javafx.scene.control.Dialog#getResult
does not seem to get the result then.
Workaround
I can work around the issue by getting FXFileChooserDialog.model.getSelectedFile()
using reflection:
private Optional<Path> getSelectedFile(FXFileChooserDialog fileChooser) {
Optional<Path> selectedFile = fileChooser.showOpenDialog(getWindow());
if (selectedFile.isEmpty()) {
// workaround for bug https://github.com/Oliver-Loeffler/FXFileChooser/issues/44
try {
Field modelField = FXFileChooserDialog.class.getDeclaredField("model");
modelField.setAccessible(true);
Object model = modelField.get(fileChooser);
Method getSelectedFileMethod = model.getClass().getDeclaredMethod("getSelectedFile");
getSelectedFileMethod.setAccessible(true);
Path result = (Path) getSelectedFileMethod.invoke(model);
return Optional.ofNullable(result);
} catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
return selectedFile;
}
(the model is working, as verified by a test case)
- Drawback:
- With workaround in place, a file is selected even if the user clicks "Cancel" ๐
- A breakpoint in
net.raumzeitfalle.fx.filechooser.FileChooserController#cancelAction
gets no hits, I would have expected that code to prevent this
- it seems usually the result converter defined in
net.raumzeitfalle.fx.filechooser.FXFileChooserDialog#FXFileChooserDialog
does the work to get things to the result, but it does not get called in case of the double click:
setResultConverter(dialogButton -> {
if (dialogButton == okay) {
this.hide();
return model.getSelectedFile();
}
return null;
});
More context
So far, I'm using FxFileChooserDialog in a playground project only. Find the latest code here as part of this PR:
https://github.com/ArchibaldBienetre/javaFxTestGradle/pull/1/files