0
public Vue(String title) {
    super(title);



    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    this.getContentPane().setLayout(new GridLayout(4, 1));
    JPanel p1 = createPanel1();
    this.getContentPane().add(p1);
    JPanel p2bis = createPanel2bis();
    this.getContentPane().add(p2bis);
    JPanel p3 = createPanel3();
    this.getContentPane().add(p3);




    this.setJMenuBar(createMenuBar());

    this.setPreferredSize(new Dimension(750,400));
    this.pack();
    this.setVisible(true);


    ImageIcon image = new ImageIcon(getClass().getResource("voile.png"));
    this.setIconImage(image.getImage());
    System.out.println(image.getDescription());

}

Hi,

I spent time to find the solution to display my image as a JFrame icon image... And tryed a lot of things like

ImageIcon image = new ImageIcon(Constants.LOGO_ABSOLUTE_PATH);
this.setIconImage(image.getImage());
System.out.println(image.getDescription());

I put "voile.png" file everywhere in my directories : root,src,bin and inside bin and src packages. I also tryed with an online logo and its URI.

However, it actually doesn't work. If anyone can help me ?

I'm running it on Eclipse Oxygen and Windows 10

PS : System.out.println(image.getDescription()); always showing a path where my image is located (Anyway I put it everywhere)

Alex T
  • 33
  • 6
  • What's the value of `Constants.LOGO_ABSOLUTE_PATH`? – apetranzilla Sep 11 '18 at 14:26
  • I tried "voile.png" "https://mywebsite.com/img/voile.png" it exists and absolute paths like "U:\activities\log AT\myapp\src\Vue\voile.png" "U:\activities\log AT\myapp\voile.png" . I also tryed to double backslash in order to escape \ but nothing good is happening – Alex T Sep 11 '18 at 14:30
  • Try `/voile.png` with the file in the `src` folder. – apetranzilla Sep 11 '18 at 14:42
  • `ImageIcon image = new ImageIcon(Constants.LOGO_ABSOLUTE_PATH); this.setIconImage(image.getImage()); System.out.println(image.getDescription());` and `public static final String LOGO_ABSOLUTE_PATH = "/voile.png";` in constants class doesn't work It prints "/voile.png" in eclipse console – Alex T Sep 11 '18 at 14:47
  • How about `this.setIconImage(image.getImage());this.setVisible(true);`? –  Sep 11 '18 at 15:21
  • It doesn't work anymore – Alex T Sep 12 '18 at 07:22
  • 1
    *"It doesn't work anymore"* What doesn't work any more? Tip: Add @apemanzilla (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Sep 15 '18 at 00:14

1 Answers1

1

ImageIcon doesn't support PNG transparency. I had to create a blank jpg image and put logo inside. My final code is

ImageIcon image = new ImageIcon(Constants.LOGO);
this.setIconImage(image.getImage());
Alex T
  • 33
  • 6
  • 1
    *"ImageIcon doesn't support PNG transparency."* Of course it does! See the code in [this answer](https://stackoverflow.com/a/10862262/418556) which uses 5 (image icons in 5) labels & 4 buttons to display sub-images of a PNG image [seen in the question](https://stackoverflow.com/q/10861852/418556). The PNG in the question is simply embedded in the web page, with the default white BG color of SO. The PNG once sub-divided and displayed by the code, has a black background color provided by the panel that lays out the buttons and labels. – Andrew Thompson Sep 15 '18 at 00:07