Does Swing support Windows 7 style file support?

I just added the standard β€œ Open File ” on the small desktop application that I am writing, based on the JFileChooser Input Swing JFileChooser . It generates a window that looks like this:

screenshot of unwanted / XP-style window

but I would rather have a window that looks like this:

screenshot of desired / 7-style window

In other words, I want my file selection to have the style of Windows Vista / Windows 7, and not Windows XP. Is this possible in Swing? If so, how is this done? (For the purposes of this question, suppose that the code will work exclusively on computers running Windows 7.)

+46
java swing filechooser
Apr 18 2018-11-18T00:
source share
10 answers

This does not seem to be supported in Swing in Java 6.

Currently, the easiest way to open this dialog is through SWT, not Swing. SWT FileDialog ( javadoc ) invokes this dialog. The following is a modification of the SWT of the FileDialog fragment to use the open dialog box rather than saving. I know that this is not quite what you are looking for, but you can highlight this in the utility class and add swt.jar to your class for this function.

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class SWTFileOpenSnippet { public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell (display); // Don't show the shell. //shell.open (); FileDialog dialog = new FileDialog (shell, SWT.OPEN | SWT.MULTI); String [] filterNames = new String [] {"All Files (*)"}; String [] filterExtensions = new String [] {"*"}; String filterPath = "c:\\"; dialog.setFilterNames (filterNames); dialog.setFilterExtensions (filterExtensions); dialog.setFilterPath (filterPath); dialog.open(); System.out.println ("Selected files: "); String[] selectedFileNames = dialog.getFileNames(); for(String fileName : selectedFileNames) { System.out.println(" " + fileName); } shell.close(); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } } 
+20
May 28 '11 at 5:22 a.m.
source share

Even native Windows applications can get this type of dialog box displayed in Windows 7. This is usually controlled by the flags in OPENFILENAME and its size was passed when the WinAPI function GetOpenFileName . Swing (Java) uses hooks to receive events from the Open File dialog box; these events are broadcast differently between the version of Windows XP and Windows 7.

So the answer is: you cannot control the look of Swing's FileChooser. However, when Java gets support for this new look, you will automatically get a new style.

Another option is to use SWT as suggested in this answer . Alternatively, you can use JNA to call the Windows API or to write your own method.

+11
May 31 '11 at 6:20
source share

A bit of a hack and a bit smaller than the Swing version, but do you think you are using java.awt.FileDialog ? It should not just look like a Windows selection file, but in fact it is one.

+7
Apr 18 2018-11-18T00:
source share

I don’t think Swing will cover this, although if it isn’t, you might need to look at something like SWT that will use the actual native component or make a user interface user element, for example, something from the book "Dirty rich customers. "

+7
Apr 18 2018-11-18T00:
source share

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); }); 
+7
Aug 22 '14 at 7:28
source share

Good question +1, it looks like they "forgot" to implement something for Win7 (defaultLookAndFeel) in Java6, but for WinXP correlation works, and I also hope that there should be some Method / Properties for

Anyway, you can try this with this code,

 import java.io.File; import javax.swing.*; import javax.swing.filechooser.FileFilter; class ChooserFilterTest { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"}; for (String property : properties) { System.out.println(property + ": " + System.getProperty(property)); } JFileChooser jfc = new JFileChooser(); jfc.showOpenDialog(null); jfc.addChoosableFileFilter(new FileFilter() { @Override public boolean accept(File f) { return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj"); } @Override public String getDescription() { return "Wavefront OBJ (*.obj)"; } @Override public String toString() { return getDescription(); } }); int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?"); System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION)); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(jfc); } catch (Exception e) { e.printStackTrace(); } jfc.showOpenDialog(null); result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?"); System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION)); } }; SwingUtilities.invokeLater(r); } private ChooserFilterTest() { } } 
+2
May 25 '11 at 11:21
source share

Failed to do this job for directories! DirectoryDialog throws us back to the tree style selection list, which matches the one given in the question. The problem is that it does not allow me to select / select / open hidden folders. Also, it does not allow navigation through folders such as AppData, ProgramData, etc.

The Windows 7 style filedialog (swt) element allows navigation to these folders, but again does not allow you to select a folder: (

Update Use JFileChooser and setFileHidingEnabled(false) to view hidden folders. The only mandate for this is that users need to "show hidden files, folders and drives" selected in

Folder Options β†’ Browse

Windows Explorer

You will not get the flexibility of the address bar, but if you were looking for an alternative option for selecting files in Java, which also allows you to view / view hidden files / folder - then this should be enough

+1
Jun 08 '13 at 6:58 am
source share

John McCarthy's answer seems the best. Here are some suggestions.

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.graphics.Image; 

Add an image in the upper left corner. It is important that you use "getResourceAsStream", you will notice that after exporting as a Runnable jar:

 Display display = new Display(); Shell shell = new Shell(display); InputStream inputImage = getClass().getResourceAsStream("/app/launcher.png"); if (inputImage != null) { shell.setImage(new Image(display, inputImage)); } 

User home directory:

 String filterPath = System.getProperty("user.home"); 

Get the absolute path name instead of the filter-dependent path, which is not true on another drive.

 String absolutePath = dialog.open(); 
+1
Jul 28 '15 at 12:15
source share

Since Swing emulates various L & Fs, I believe that it is best to upgrade the JRE to the very latest and hope that the JFileChooser UI is updated.

0
Apr 18 2018-11-18T00:
source share

JFileChooser has always been a little weird looking with Swing, also a bit slow.

Try using SWT filechooser, or you can wrap C calls in JNA.

0
May 24 '11 at 21:46
source share



All Articles