javaswingjfilechooserjdialog

JFileChooser title icon not changing


I have seen answers saying that the icon is taken from the parent, but it is not working for me. I have a JDialog class where I set the Icon: this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass() .getClassLoader().getResource("Icons/favicon.png"))); I load it this way because I will use a Jar.

This is how I initialize the FileChooser: fc = new MyFileChooser();

this is the constructor of the MyFileChooser class:

public MyFileChooser() {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "Archivo wav (*.wav)", "wav");
        this.setDialogTitle("Elige la cancion a añadir");
        this.setFileFilter(filter);
        this.setAcceptAllFileFilterUsed(false);
    }

And this is how I show the FileChooser when clicking on a button:

if (chooseFileButton == e.getSource()) {
  // got it from https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java
  int returnVal = fc.showOpenDialog(this);
}

I though 'this' refers to the Dialog class.

Edit: I have also tried 'AddDialog.this' being AddDialog the name of the class for the JDialog


Solution

  • Here is a complete example that works on Windows. I showed the file dialog inside of an action listener, so I had to use the outter classname with this, Worms.this

    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import java.awt.Graphics2D;
    import java.util.List;
    import java.awt.Color;
    
    public class Worms extends JFrame{
            public void buildGui(){
                    BufferedImage img = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g2d = img.createGraphics();
                    g2d.setColor(Color.RED);
                    g2d.fillOval(0, 0, 64, 64);
                    g2d.dispose();
                    setIconImage( img );
                    JButton click = new JButton("click");
                    click.addActionListener( evt ->{
                        JFileChooser chooser = new JFileChooser();
                            int value = chooser.showOpenDialog(Worms.this);
                    });
    
    
                    add(click);
                    pack();
                    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    setVisible(true);
            }
    
        public static void main(String[] args){
                new Worms().buildGui();
    
            }
    
    }
    

    Maybe you can check that since it is complete, compilable. Your problem could be an OS/JVM quirk so you might have to include more information.