import javax.swing.*; public class IconOnJFrameTitleBar extends JFrame { // While other methods can be used to display an icon on the title bar of a JFrame, // using 'getClass().getResource()' allows the graphic to be loaded and used directly // from an executable (runnable) .JAR file if one is created for a Java program // using a JFrame; without this approach, if making a .JAR file, the .CLASS file // would first have to be extracted from the .JAR file before it would be able to // read the icon graphic; note that this same technique applies to all graphics and // sounds loaded from within .JAR files; note, also, that no path is given here, and // the icon file needs to be in the same folder as the .CLASS file (the "bin" folder) // in Eclipse, and then in the same folder as the .CLASS file if everything is put // into a .JAR file; in addition, note that using a .ICO file does not seem to work // here; the file appears to need to be in .GIF, .JPG, or .PNG format; finally, note // that this line does not place the icon on the JFrame; it loads the image, but the // icon is placed on the JFrame with the 'setIconImage(icon.getImage())' line below private ImageIcon icon = new ImageIcon(getClass().getResource("daveicon.gif")); public static void main(String[] args) { new IconOnJFrameTitleBar(); } // Constructor public IconOnJFrameTitleBar() { // Allow the JFrame to be closed via the "X" setDefaultCloseOperation(EXIT_ON_CLOSE); // Set the size of the JFrame setSize(400, 100); // Load the JFrame icon setIconImage(icon.getImage()); // Set the title bar caption setTitle(" <--- Dave's Demo: Icon on the Title Bar"); // Position the JFrame in the center of the screen setLocationRelativeTo(null); // Make the JFrame visible setVisible(true); } }