FileChooser.showOpenDialog showing default java icon on top of the frame

夙愿已清 提交于 2019-12-11 02:36:22

问题


I have created a FileChooser in my swing application. when I click on open ,the open dialog box showing default image(java) on top of the frame instead of custom image which i was set for my JFrame.

 Sample Code:

         JFileChooser filec=new JFileChooser();
         int fileval=filec.showOpenDialog(myjframe);

I found some times it is working fine.please help me on this.


回答1:


You can set the image in the parent JFrame of the JFileChooser which will be reflected in the dialog:

Image image = ImageIO.read(getClass().getResource("MyImage.png"));
myjframe.setIconImage(image);



回答2:


It seems to work reliably here with this SSCCE. Does this code work reliably where you are?

import java.awt.*;
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() {
                Image image = 
                    new BufferedImage(32,32,BufferedImage.TYPE_INT_RGB);

                JFrame f = new JFrame("Demo");
                f.setIconImage(image);
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                f.pack();
                f.setSize(600,400);
                f.setVisible(true);

                JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(f);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}



回答3:


JFrame f = new JFrame("Edit Configure File");

//Use first two ways getting error: non-static method getClass() cannot be referenced from a static context

//(1) Image image = ImageIO.read(getClass().getResource("images/ctx.Icon"));
//f.setIconImage(image);

//(2) f.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/ctx.PNG")));
//(3) Use third way. It works for me
f.setIconImage(new ImageIcon("images/ctx.PNG").getImage());


来源:https://stackoverflow.com/questions/13893246/filechooser-showopendialog-showing-default-java-icon-on-top-of-the-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!