I was wondering if anyone knows why the following code to set the IconImage from my JFrame only works on windows but not on MacOS.
public ClientGUI(String title) {
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/ww_icon.png")));
setContentPane(contentPane);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
CodePudding user response:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Taskbar;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Image image = ImageIO.read(getClass().getResource("/images/16x16.png"));
Taskbar taskbar = Taskbar.getTaskbar();
taskbar.setIconImage(image);
JFrame frame = new JFrame();
frame.setIconImage(image);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(64, 64, 64, 64));
add(new JLabel("Hello World"));
}
}
}
I'd also consider having a look at the Desktop
class as well