From e75caf583363e1630e3530b2bb51c6cfa1e5fdc1 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 15 May 2020 17:20:52 +0200 Subject: [PATCH] FileChooser: use system icons (issue #100) --- CHANGELOG.md | 1 + .../formdev/flatlaf/ui/FlatFileChooserUI.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7966edd6..ece1ccd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ FlatLaf Change Log `flatlaf-swingx-.jar`. - CheckBox and RadioButton: Flag `opaque` is no longer ignored when checkbox or radio button is used as table cell renderer. (issue #77) +- FileChooser: Use system icons. (issue #100) - FileChooser: Fixed missing labels in file chooser when running on Java 9 or later. (issue #98) - PasswordField: Do not apply minimum width if `columns` property is greater diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFileChooserUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFileChooserUI.java index 5f92bf38..95b7852a 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFileChooserUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFileChooserUI.java @@ -17,8 +17,11 @@ package com.formdev.flatlaf.ui; import java.awt.Dimension; +import java.io.File; +import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.JFileChooser; +import javax.swing.filechooser.FileView; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.metal.MetalFileChooserUI; import com.formdev.flatlaf.util.UIScale; @@ -117,6 +120,8 @@ import com.formdev.flatlaf.util.UIScale; public class FlatFileChooserUI extends MetalFileChooserUI { + private final FlatFileView fileView = new FlatFileView(); + public static ComponentUI createUI( JComponent c ) { return new FlatFileChooserUI( (JFileChooser) c ); } @@ -134,4 +139,37 @@ public class FlatFileChooserUI public Dimension getMinimumSize( JComponent c ) { return UIScale.scale( super.getMinimumSize( c ) ); } + + @Override + public FileView getFileView( JFileChooser fc ) { + return fileView; + } + + @Override + public void clearIconCache() { + fileView.clearIconCache(); + } + + //---- class FlatFileView ------------------------------------------------- + + private class FlatFileView + extends BasicFileView + { + @Override + public Icon getIcon( File f ) { + Icon icon = getCachedIcon( f ); + if( icon != null ) + return icon; + + if( f != null ) { + icon = getFileChooser().getFileSystemView().getSystemIcon( f ); + if( icon != null ) { + cacheIcon( f, icon ); + return icon; + } + } + + return super.getIcon( f ); + } + } }