Skip to content

Instantly share code, notes, and snippets.

@navopw
Last active April 6, 2019 10:18
Show Gist options
  • Save navopw/7a7ae4bb7e3c07365095a6c4d324fde1 to your computer and use it in GitHub Desktop.
Save navopw/7a7ae4bb7e3c07365095a6c4d324fde1 to your computer and use it in GitHub Desktop.
FileChooser Java 8 - Consumer Style
package de.navo.anything;
import java.awt.EventQueue;
import java.io.File;
import java.util.function.Consumer;
import javax.swing.JFileChooser;
public class FileChooser {
/**
* The shortest way to handle a file selection with Java 8
* @param callback A callback with the chosen file, null when aborted selecting.
*/
public static void open(Consumer<File> callback) {
EventQueue.invokeLater(() -> {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
callback.accept(fileChooser.getSelectedFile());
} else {
callback.accept(null);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment