Setting icons in java

Setting an Icon for my Java Application [duplicate]

I am in the middle of a code/application and I’ve gotten to the point where I am releasing it publicly. Id like to know a simple example code of how I can «set» the ICON image for the application. Just a simple code where I can place at the top of my class that will grab the Icon image out of its directory [/res/Icon.png] Thanks

1 Answer 1

You can use Frame#setIconImage(Image) or if your want something that is a little more flexible, Window#setIconImages(List)

Credit to those authors please

Updated with simple example

Loading images by it’s nature can raise problems. You need to be prepared for the fact that it might fail. Hopefully, you’ve prepared the application well enough that under normal operations, this would be a rare situation

import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Image; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class FrameIconTest < public static void main(String[] args) < new FrameIconTest(); >public FrameIconTest() < EventQueue.invokeLater(new Runnable() < @Override public void run() < try < UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); >catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) < >JFrame frame = new JFrame("Testing"); try < Listicons = new ArrayList(5); icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon16x16.png"))); icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon24x24.png"))); icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon32x32.png"))); icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon64x64.png"))); icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon128x128.png"))); frame.setIconImages(icons); > catch (IOException exp) < exp.printStackTrace(); // Log the problem through your applications logger. >frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JLabel("Frame with icon")); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); > >); > > 

Источник

Changing Desktop Icon for Java Application

I wish to thank you in advance for taking the time to read my question, and I would greatly appreciate any comments, answers, insights, techniques and critiques that you may be able to provide. I’m looking for a useful method for changing the desktop icon for a Java application. I’ve looked into this for a few days now, but am not finding an accurate result. Before you mark this down and call it a duplicate, I have read: How do I change the default application icon in Java? to others who asked this question), but this does not address my specific problem. I know that their method utilizes a url location instead of an import, but I am trying to learn how to use this with the import(if that is, in fact, possible). When I attempt to use their method for changing by source location. Besides that, the url example doesn’t seem to work for a file stored on the computer. I get an «uncaught error» message when I attempt to run it. I use the following format to declare an image that I have imported into NetBeans:

Image image = new ImageIcon("imported.png").getImage(); frame.setIconImage(image); 

Now this works fine for the icon that displays in the toolbar and it also appears in the upper left-hand corner of the frame, but I still have the Java coffee-cup as the icon for the application when I clean and build it. For additional resources to the code that I am using to attempt this:

import java.awt.Image; import javax.swing.*; public class Check < JFrame frame; public static void main(String[] args) < new Check().go(); >private void go() < frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Image image = new ImageIcon("owl.gif").getImage(); frame.setIconImage(image); frame.setVisible(true); frame.setSize(300, 300); >> 

The «owl.gif» bit is what I imported into NetBeans by click and drag method (as described in one of the books that I read that focused on NetBeans). I’m looking for a way to make a file that I already have saved on my computer the desktop icon for my application after it is built.

Источник

Читайте также:  Python порядок выполнения арифметических операций

How to change a frame’s title bar icon (application icon) in Java AWT Toolkit

Carlos Delgado

Learn how to define a custom icon for a frame in the AWT Toolkit of Java.

How to change a frame's title bar icon (application icon) in Java AWT Toolkit

The customization of your application is essential to create a confidence feeling for your user. One of those tiny details is the usage of an icon, to leave at least the impression that you are really working dedicatedly in the application. In this article we’ll show you how you can quickly change the icon of your application with the code in Java AWT Toolkit.

The only thing you will need is an image to use as icon for your application, if you are lacking of imagination or you just want to test quickly, you can download some random icon from this website. Once you have some icon to use, just follow the next logic:

// Create some frame instance Frame window = new Frame(); // Create an image instance from the image that you want to use as icon for your app Image icon = Toolkit.getDefaultToolkit().getImage("C:\\some-directory\\icon.png"); // And set it window.setIconImage(icon);

You will need an instance of a Frame that allows the customization of the icon, then create an instance of an Image with the default Toolkit of Java AWT from a local path (note that if the file is inside your project resources, you can use a relative path or join it with the current application path). The getImage method of the toolkit returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. Finally call the setIconImage method from the frame and pass as first argument the instantiated image.

Читайте также:  Html form in div tag

Note

The recommended format is the PNG that allows transparency on your icon so it will look better on any screen.

Application context example

The following code represents the mentioned logic during the initialization of a frame within a structured application context:

package sandbox; import java.awt.*; public class Sandbox < Sandbox()< // Create a new frame Frame window = new Frame(); // Create an image instance from the image that you want to use as icon for your app. Image icon = Toolkit.getDefaultToolkit().getImage("C:\\some-directory\\icon.png"); window.setIconImage(icon); // Set other options of the frame . window.setLayout(null); window.setSize(400,400); window.setVisible(true); >/** * Initialize app. * * @param args */ public static void main(String[] args) < Sandbox app = new Sandbox(); >> 

Источник

Оцените статью