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");

Here’s a working code example:

import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame
   public static void main(String []args) {
      Main main = new Main();;
   public Main() {
      JButton fileButton = new JButton("Select File");
      fileButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            File file = getFileFromUser();
            if (file != null)
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
      // set the icon
      ImageIcon icon = new ImageIcon("c:\test.gif");
   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();