Java 8 may finally bring a solution, but unfortunately (for Swing applications), it only comes as a JavaFX FileChooser class:
I checked this code here and it really produces a modern dialog (here Windows 7):
FileChooser fileChooser = new FileChooser(); //Set extension filter FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG"); FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG"); fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG); //Show open file dialog File file = fileChooser.showOpenDialog(null);
To integrate this into a Swing application, you need to run it in the javafx stream via Platform.runLater (as shown here ).
Note that you will need to initialize the javafx stream (in the example, this is done when the scene is initialized in new JFXPanel() ).
To summarize, a ready-to-launch solution in a swing application would look like this:
new JFXPanel(); // used for initializing javafx thread (ideally called once) Platform.runLater(() -> { FileChooser fileChooser = new FileChooser(); //Set extension filter FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG"); FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG"); fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG); //Show open file dialog File file = fileChooser.showOpenDialog(null); });
Luke Usherwood Aug 22 '14 at 7:28 2014-08-22 07:28
source share