JavaFX file listview with icon and file name

后端 未结 2 1453
鱼传尺愫
鱼传尺愫 2020-12-21 04:43

In a JavaFX dialog, I would like to show a list of files with their icons and file names.

It was easy to find how to get an icon for a file extension:



        
2条回答
  •  囚心锁ツ
    2020-12-21 05:09

    I came up with this code, which seems to work for converting the icon returned by getSystemIcon to a format which JavaFX can understand. It does this by using a combo of SwingUtilities.invokeLater with Platform.runLater to try to mitigate any potential threading issues between the two projects. The javax.swing.Icon is painted to java.awt.BufferedImage which is converted by SwingFXUtils to a JavaFX Image.

    So the Swing Icon => JavaFX image portion of the code you were asking about seems to work. The only thing is, when I tested the application on OS X 10.7 running Oracle Java8u20, every file extension type I tried gave the exact same icon. So it would seem that your method for getting an icon from the system via the swing FileSystemView isn't really supported on OS X (as far as I can tell). I guess you could try it on another OS and see if it works for you there (presuming that supporting this icon lookup feature on OS X is not necessary for your task).

    So googling around brought up the following question:

    • Access file type icons Mac OSX

    And in that, Sam Barnum recommends a FileView works much better than a FileSystemView - and so it did for me. I switched your code to use a FileView and after that started getting different icons for different file types on OS X. The icons were still really small 16x16 icons (I wouldn't know how to retrieve the hi-res retina icons for instance), but at least the icons which were retrieved appeared correct and file type specific.

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.embed.swing.SwingFXUtils;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    public class FileIconViewer extends Application {
    
        @Override
        public void start(Stage stage) throws IOException {
            Runnable fetchIcon = () -> {
                File file = null;
                try {
                    file = File.createTempFile("icon", ".png");
    
                    // commented code always returns the same icon on OS X...
                    // FileSystemView view = FileSystemView.getFileSystemView();
                    // javax.swing.Icon icon = view.getSystemIcon(file);
    
                    // following code returns different icons for different types on OS X...
                    final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
                    javax.swing.Icon icon = fc.getUI().getFileView(fc).getIcon(file);
    
                    BufferedImage bufferedImage = new BufferedImage(
                        icon.getIconWidth(), 
                        icon.getIconHeight(), 
                        BufferedImage.TYPE_INT_ARGB
                    );
                    icon.paintIcon(null, bufferedImage.getGraphics(), 0, 0);
    
                    Platform.runLater(() -> {
                        Image fxImage = SwingFXUtils.toFXImage(
                            bufferedImage, null
                        );
                        ImageView imageView = new ImageView(fxImage);
                        stage.setScene(
                            new Scene(
                                new StackPane(imageView), 
                                200, 200
                            )
                        );
                        stage.show();
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                    Platform.exit();
                } finally {
                    if (file != null) {
                        file.delete();
                    }
                }
            };
    
            javax.swing.SwingUtilities.invokeLater(fetchIcon);
        }
    
        public static void main(String[] args) { launch(args); }
    }
    

    Note: there is an existing request in the JavaFX issue tracker to add this functionality to JavaFX (it is currently not scheduled for implementation, though you could log into the issue tracker and vote for the issue, comment on it, link it back to this StackOverflow question, etc):

    • RT-19583 Possibility to get native icons, on different sizes.

提交回复
热议问题