Search code examples
javaswingjfilechooseropenfiledialog

How change the image of open Dialog box and how to customize Dialog box in swing?


When I open file that show the dialog box, I need to change Java image and add my own image. How to customize dialog box?

For example, I need to add the Encoding to the dialog box and how to add different type of files to Files of type dropdown box. For Example, i add text, java, html to Files of type box.

Here is my code,

FileDialog fd = new FileDialog(OpenExample.this, "Select File", FileDialog.LOAD);
fd.setVisible(true);

Solution

  • To provide an icon for a file chooser or dialog, set an icon for the parent frame.

    enter image description here

    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class FileChooserIcon {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        // see nice icons in chooser!
                        UIManager.setLookAndFeel(
                                UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception useDefault) {}
                    JLabel ui = new JLabel("Big Space");
                    ui.setBorder(new javax.swing.EmptyBorder(40, 200, 40, 200));
    
                    JFrame f = new JFrame("Show file chooser icon");
                    f.setIconImage(new BufferedImage(
                            20, 20, BufferedImage.TYPE_INT_RGB));
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setContentPane(ui);
                    f.pack();
                    f.setLocationByPlatform(true);
                    f.setVisible(true);
    
                    JFileChooser jfc = new JFileChooser();
                    jfc.showOpenDialog(f); // use frame icon!
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

    .. how to add different type of files to Files of type dropdown box? For example: add text, java, html to Files of type box.

    See How to Use File Choosers: FileChooserDemo2 which offers a file filter for Just Images..

    enter image description here