Last active
April 6, 2019 10:18
-
-
Save navopw/7a7ae4bb7e3c07365095a6c4d324fde1 to your computer and use it in GitHub Desktop.
FileChooser Java 8 - Consumer Style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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