Setting the icon of a JFileChooser

showOpenDialog takes a component as its first argument. It is called the parent and used for several purposes. One of these it to find out which is the nearest JFrame, from which it will inherit the top-left icon. So the icon will be inherited. If you want your own customized icon, just create a dummy JFrame with another icon.

   JFrame dummy = new JFrame();
   ImageIcon icon = new ImageIcon("c:\test.gif");
   dummy.setIconImage(icon.getImage());
   ...
   fc.showOpenDialog(dummy);

Here’s a working code example:

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
 
public class Main extends JFrame
{
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
 
   public Main() {
      JButton fileButton = new JButton("Select File");
      fileButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            File file = getFileFromUser();
            if (file != null)
               System.out.println(file.getName());
         }
      });
      getContentPane().add(fileButton);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
 
      // set the icon
      ImageIcon icon = new ImageIcon("c:\test.gif");
      setIconImage(icon.getImage());
 
      pack();
   }
   
   public File getFileFromUser() {
      JFileChooser fc = new JFileChooser();
 
      // use current directory
      fc.setCurrentDirectory(new File("c:\"));
 
      // set default name
      fc.setSelectedFile(new File("autoexec.bat"));
 
      // show dialog for opening files
      int result = fc.showOpenDialog(this);
 
      if (result != fc.APPROVE_OPTION) 
         return null;
      
      return fc.getSelectedFile();
   }
}