Change the default icon of the Window



Following example showcases how to change the default icon of the window in Swing based application.

We are using the following APIs.

  • ImageIcon − To create an image icon.

  • JFrame.setIconImage() − To set the icon to the window.

Example - Changing Default Icon of Window in Swing Application

SwingTester.java

package com.tutorialspoint; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.LayoutManager; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingTester { public static void main(String[] args) { createWindow(); } private static void createWindow() { JFrame frame = new JFrame("Swing Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon arrowIcon = null; java.net.URL imgURL = SwingTester.class.getResource("arrow.jpg"); if (imgURL != null) { arrowIcon = new ImageIcon(imgURL); frame.setIconImage(arrowIcon.getImage()); } else { JOptionPane.showMessageDialog(frame, "Icon image not found."); } createUI(frame); frame.setSize(492, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } private static void createUI(JFrame frame){ JPanel panel = new JPanel(); LayoutManager layout = new FlowLayout(); panel.setLayout(layout); panel.add(new JLabel("Hello World!")); frame.getContentPane().add(panel, BorderLayout.CENTER); } }

Output

Compile and Run the program and verify the output −

look and feel of a current system
swingexamples_frames.htm
Advertisements