{"id":87,"date":"2009-09-17T13:35:14","date_gmt":"2009-09-17T11:35:14","guid":{"rendered":"http:\/\/bininda.com\/blog\/2009\/09\/image-preview-in-jfilechooser-2\/"},"modified":"2009-10-07T09:09:31","modified_gmt":"2009-10-07T07:09:31","slug":"image-preview-in-jfilechooser-2","status":"publish","type":"post","link":"http:\/\/bininda.com\/blog\/2009\/09\/image-preview-in-jfilechooser-2\/","title":{"rendered":"Image Preview in JFileChooser"},"content":{"rendered":"<p>I just put together a little code to show an image preview in the <a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/javax\/swing\/JFileChooser.html\">JFileChooser<\/a>. It shows a scaled image on the right side of the file chooser. The image is scaled to completely fit inside the box to the right.<\/p>\n<p><a href=\"http:\/\/bininda.com\/blog\/wp-content\/uploads\/2009\/09\/imagesel.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-94\" title=\"ImageFileChooser\" src=\"http:\/\/bininda.com\/blog\/wp-content\/uploads\/2009\/09\/imagesel.png\" alt=\"ImageFileChooser\" width=\"535\" height=\"323\" srcset=\"http:\/\/bininda.com\/blog\/wp-content\/uploads\/2009\/09\/imagesel.png 723w, http:\/\/bininda.com\/blog\/wp-content\/uploads\/2009\/09\/imagesel-500x302.png 500w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/a><br \/>\nThe file chooser does not filter files. It allows the user to select any file but if it can be opened as an image, the preview is shown.<br \/>\nI created a component based on <a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/javax\/swing\/JPanel.html\">JPanel<\/a> for the preview which overrides the <a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/javax\/swing\/JComponent.html#paintComponent%28java.awt.Graphics%29\">paintComponent<\/a> method:<\/p>\n<pre class=\"brush: java; collapse:true\">package x.y.z.util;\r\n\r\nimport java.awt.Graphics;\r\nimport java.awt.Image;\r\nimport javax.swing.JPanel;\r\n\r\npublic class ImagePanel extends JPanel\r\n{\r\n\r\n    private Image image;\r\n    private Image scaledCache;\r\n\r\n    public ImagePanel()\r\n    {\r\n        setBorder(javax.swing.BorderFactory.createEtchedBorder());\r\n    }\r\n\r\n    public void setImage (Image image)\r\n    {\r\n        this.image = image;\r\n        scaledCache = null;\r\n        repaint();\r\n    }\r\n\r\n    private Image getScaled()\r\n    {\r\n        int iw = image.getWidth(this);\r\n        int ih = image.getHeight(this);\r\n        int pw = getWidth();\r\n        int ph = getHeight();\r\n        double scale;\r\n        if (1.0 * pw \/ iw &lt; 1.0 * ph \/ ih)\r\n        {\r\n            scale = 1.0 * pw \/ iw;\r\n        }\r\n        else\r\n        {\r\n            scale = 1.0 * ph \/ ih;\r\n        }\r\n        int scaledw = (int) (iw * scale);\r\n        int scaledh = (int) (ih * scale);\r\n        if (scaledCache != null)\r\n        {\r\n            if (scaledCache.getWidth(this) == scaledw &amp;&amp;\r\n                    scaledCache.getHeight(this) == scaledh)\r\n            {\r\n                return scaledCache;\r\n            }\r\n        }\r\n        scaledCache = image.getScaledInstance(scaledw, scaledh, Image.SCALE_DEFAULT);\r\n        return scaledCache;\r\n    }\r\n\r\n    @Override\r\n    public void paintComponent(Graphics g)\r\n    {\r\n        if (g != null)\r\n        {\r\n            Graphics scratch = g.create();\r\n            scratch.setColor(getBackground());\r\n            scratch.fillRect(0, 0, getWidth(), getHeight());\r\n            if (image != null)\r\n            {\r\n                Image scaled = getScaled();\r\n                scratch.drawImage(scaled, getWidth() \/ 2 - scaled.getWidth(this) \/ 2,\r\n                        getHeight() \/ 2 - scaled.getHeight(this) \/ 2, this);\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>Then I created a the class ImageFileChooser derived from <a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/javax\/swing\/JFileChooser.html\">JFileChooser<\/a>:<\/p>\n<pre class=\"brush: java; collapse:true\">package x.y.z.util;\r\n\r\nimport java.awt.Dimension;\r\nimport java.awt.Image;\r\nimport java.beans.PropertyChangeEvent;\r\nimport java.beans.PropertyChangeListener;\r\nimport java.io.File;\r\nimport javax.swing.ImageIcon;\r\nimport javax.swing.JFileChooser;\r\n\r\npublic class ImageFileChooser extends JFileChooser {\r\n\r\n    public ImageFileChooser()\r\n    {\r\n        final ImagePanel preview = new ImagePanel();\r\n        preview.setPreferredSize(new Dimension (150, 150));\r\n        setAccessory(preview);\r\n        addPropertyChangeListener(new PropertyChangeListener() {\r\n            public void propertyChange(PropertyChangeEvent e)\r\n            {\r\n                String propertyName = e.getPropertyName();\r\n                if (propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))\r\n                {\r\n                    File selection = (File) e.getNewValue();\r\n                    String name;\r\n                    if (selection == null)\r\n                    {\r\n                        return;\r\n                    }\r\n                    else\r\n                    {\r\n                        name = selection.getAbsolutePath();\r\n                    }\r\n                    ImageIcon icon = new ImageIcon(name);\r\n                    Image newImage = icon.getImage();\r\n                    preview.setImage (newImage);\r\n                }\r\n            }\r\n        });\r\n    }\r\n}<\/pre>\n<p>Works great.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just put together a little code to show an image preview in the JFileChooser. It shows a scaled image on the right side of the file chooser. The image is scaled to completely fit inside the box to the right. The file chooser does not filter files. It allows the user to select any [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[20,33],"_links":{"self":[{"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":6,"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":93,"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/93"}],"wp:attachment":[{"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bininda.com\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}