1

My image for logo doesnt show up on mac. there is no java logo in Jframe to begin with? what am I missing?

package test;

import javax.swing.*;

public class Demo {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("this is the frame title");
        frame.setSize(420,420);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        
        ImageIcon image = new ImageIcon("logo.png");
        frame.setIconImage(image.getImage());
    }       
}

enter image description here

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Ramzi Osta
  • 63
  • 6
  • 2
    Don't try to load images via file-system paths (even if it would work now, you would have troubles after you wrap your code in JAR). Instead try to load them as project *resource*. You may want to read [Different ways of loading a file as an InputStream](https://stackoverflow.com/q/676250), [Loading resources like images while running project distributed as JAR archive](https://stackoverflow.com/q/9864267) – Pshemo Apr 28 '21 at 19:09

1 Answers1

2

the mac OS will not show your icon, there's nothing wrong with your code, what you can do is enable the look and feel for cross platform. here at the docs you can see how achieve this and configure it. Basically before start your jframe do this:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName();
// you can even set a theme on it
MetalLookAndFeel.setCurrentTheme(new OceanTheme()); // since Metal is the L&F for cross platforms.
JFrame.setDefaultLookAndFeelDecorated(true);
Kaneda
  • 722
  • 5
  • 16