Application icon not following the exported project? - java

My application icon works fine when running the app from Eclipse, but once I export it as a runnable jar, then it doesn't appear.
This is the code I use to set the icon:
try {
setIconImage(ImageIO.read(new File("resources/icons/icon.png")));
}
catch (IOException exc) {
exc.printStackTrace();
}
The icon is in a source-folder called resources/icons.
Why doesn't the icon get included in the export?
Using URL now instead of looking for the icon with the File method. This works great!
Is it possible to change - by default - the file's icon? I think I have to use resource hacker or something like that in order to change it. But if it's possible via code, I'd love to learn it!

You can use getResource instead of new File(), like this:
Icon icon = new ImageIcon(getClass().getResource("resources/icons/icon.png"));
So your setIconImage should be like so:
setIconImage(new ImageIcon(getClass().getResource("resources/icons/icon.png")).getImage());
If your icon exit inside your package you can do like this:
Icon icon = new ImageIcon(getClass().getResource("/com/icons/icon.png"));
So your setIconImage should be like so :
setIconImage(new ImageIcon(getClass().getResource("/com/icons/icon.png")).getImage());
Hope this helped.

Related

Can't Change Default Java Image (JFrame.setIconImage)

I am trying to change the icon of the Java application but nothing seems to work.
I am trying to get image from resources path using:
getClass().getResource("/AppIcon.png")
Sometimes I get an error like URL not found.
The image that is used for the Form's icon can be any image but must be loaded as an Image type (not of ImageIcon type). The JFrame#setIconImage() method will auto-size the image loaded. Here just a couple ways. These examples assume that the code resides in a class which extends JFrame:
Example #1:
try {
/* For a Form's title bar icon....
Don't use this for animated .gif images otherwise your GUI will
freeze. I'm not exactly sure why but it seems as though the gif
frames continuously cycle as though in an infinite loop. If you
want to use an animated .gif image as a Form's title bar icon
then don't use Toolkit, use ImageIO.read() instead since it will
only utilize the first gif frame as the image. */
// Be sure to set the path string to your specific resources directory.
this.setIconImage(java.awt.Toolkit.getDefaultToolkit()
.getImage(getClass().getResource("/resources/images/Apple/png").getFile()));
}
catch (java.lang.NullPointerException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
Example #2:
try {
/* Can be used to also display an animated gif for the Form's Title
bar icon but only the first gif frame is utilized. */
// Be sure to set the path string to your specific resources directory.
File pathToFile = new File(getClass().getResource("/resources/images/Apple.png").getFile());
Image image = ImageIO.read(pathToFile);
this.setIconImage(image);
}
catch (IOException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
UPDATE:
As stated by #AndrewThompson in comments, the two above examples will not work as expected from a JAR file. They will work if run through the IDE however which in reality is no good except for testing. To use the two examples above in a distributive JAR file then also see Example #3 and Example #4:
Example #3:
try {
/* For a Form's title bar icon.... To be used in a distributive JAR.
Don't use this for animated .gif images otherwise your GUI will
freeze. I'm not exactly sure why but it seems as though the gif
frames continuously cycle as though in an infinite loop. If you
want to use an animated .gif image as a Form's title bar icon
then don't use Toolkit, use ImageIO.read() instead since it will
only utilize the first gif frame as the image. */
// Be sure to set the path string to your specific resources directory.
java.net.URL url = getClass().getResource("/resources/images/Apple.png");
this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(url));
}
catch (java.lang.NullPointerException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
Example #4:
try {
/* For a Form's title bar icon.... To be used in a distributive JAR.
Can be used to also display an animated gif for the Form's Title
bar icon but only the first gif frame is utilized. */
// Be sure to set the path string to your specific resources directory.
java.net.URL url = getClass().getResource("/resources/images/Apple.png");
Image image = ImageIO.read(url);
this.setIconImage(image);
}
catch (IOException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
Don't even bother with Examples #1 and #2, they now just remain here for reference. Just use either Example #3 or Example #4. Both will work in the IDE or a distributive JAR file.
Solution;
Everything works for windows but I am using Mac :D
So I started to look around Taskbar class comes with awt package and found the solution (Thanks to flohall)
try {
var image = new ImageIcon(Objects.requireNonNull(Main.class.getResource("/AppIcon.png")));
frame.setIconImage(image.getImage());
if (System.getProperty("os.name").startsWith("Mac") || System.getProperty("os.name").startsWith("Darwin")) {
Taskbar taskbar = Taskbar.getTaskbar();
try {
taskbar.setIconImage(image.getImage());
} catch (final UnsupportedOperationException e) {
System.out.println("Can't set taskbar icon.");
} catch (final SecurityException e) {
System.out.println("Warning. Can't set taskbar icon due to security exceptions.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
So we are telling the taskbar to change its icon using built-in awt Taskbar class on taskbar.setIconImage(image.getImage());. And that solves most of the things I've needed for.

Why is my JFrame Icon not changing from the default java icon?

I have tried several solutions for changing the icon of my application, but none have worked! I do not get any error when i do the following but it still won't change!? Please, can someone tell me where I am going wrong because i don't see any error, and I am not receiving any error either!
I even made sure that the icon I want to use is a 20x20 pixel icon, because I read somewhere that is the maximum size for an icon.
frame.setIconImage(
new ImageIcon(getClass().getResource("/images/bfc_icon.png")).getImage());
Why is this not working? Any help would be greatly appreciated!
EDIT:
I am testing if the file exists, turns out it does but it still is not being set as the application icon...why is this??
URL url = getClass().getResource("src/images/bfc_icon.png");
if (url == null)
System.out.println( "Could not find image!" );
else
frame.setIconImage(new ImageIcon(url).getImage());
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
// TODO add your handling code here:
Image img=ImageIO.read(getClass().getResource("ur path"));
this.setIconImage(img);
} catch (IOException ex) {
}
this will work
It seems a bit too late, but I hope this helps.
This problem probably happens when you called the setIconImage() before you initialize the JFrame.
I also had this problem with the code below (w/ Eclipse IDE):
setIconImage(Toolkit.getDefaultToolkit().getImage(Apps.class.getResource("/ico.png")));
initComponents();
I accidentally solved the problem by swapping these two so it looks like this:
initComponents();
setIconImage(Toolkit.getDefaultToolkit().getImage(Apps.class.getResource("/ico.png")));
You should try to do it as well, at least call the setIconImage() after the JFrame has initialized if you didn't use any window builder tool.
Cheers!
In my case, I just simply copied the picture I want to use as my icon to the project folder and not the src folder (source code folder) and it worked.

Eclipse Plugin Set Image icon for a custom dialog

I made a custom Eclipse plugin that uses and displays multiple dialogs, and I want to know if I could set the top left icon image with the one I use in the plugin's icons folder. I want to get that icon and set it instead of the default one that Eclipse uses.
I'm overriding the configureShell() method to change the dialog title, and I also want to change the icon.
#Override
protected void configureShell(Shell parent){
super.configureShell(parent);
parent.setText("Choose variant...");
Image icon = new Image(parent.getDisplay(), "icons/best.gif"); - this method does not work as it cannot find the file
parent.setImage(icon);
}
I also tried using the getClass().getResource("best.gif") and having the image in the same package, still can't find the location I'm giving(FileNotFoundException), and also, the Image constructor does not accept URL objects.
#Override
protected void configureShell(Shell parent){
super.configureShell(parent);
parent.setText("Choose variant...");
Image icon = new Image(parent.getDisplay(), getClass().getResource("icons/best.gif"));
parent.setImage(icon);
}
Is there a way to use the icon that I already have in my eclipse plugin?
The main problem is getting the icon from the icons folder of the plugin and making it a Image object.
Thank you.
You can register the icon in your plugins activator class like this:
#Override
protected void initializeImageRegistry(final ImageRegistry reg) {
reg.put(IMAGE_PATH, imageDescriptorFromPlugin(PLUGIN_ID, IMAGE_PATH));
}
The image path is relative to your plugin, e.g. icons/icon.png.
You can access these images via the activator class as well:
final Image image = MyActivatorClass.getDefault().getImageRegistry().get(IMAGE_PATH);
myShell.setImage(image);
(Note that I used the image path as key in the image registry, you do not have to do it like this but it makes everything a little bit less complicated by just using the same static String.)
For an Eclipse plugin you use the FileLocator class to find resources in your plugin.
For an image use something like:
String path = "icons/best.gif";
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
Image image = desc.createImage();
Note: You must arrange for the image to be disposed when no longer needed.
If your activator extends AbstractUIPlugin you can also use the ImageRegistry available from that. The registry will deal with disposing.
Be sure that the icons directory is listed in the build.properties file. Missing this will causes issues when you export your plugin.

Frame image is not changing while creating installer in windows swt application

I am creating an windows desktop swt application.
I need to change the frame icon, for that I used
frame.setIconImage((new ImageIcon("C:\\Documents and Settings\\arjuns\\Desktop\\logo1 copy.png")).getImage());
The icon is displaying when I manually run the code from eclipse, but when I create an installer using Install4j the icon is not appearing.
Can anyone please help me.
URL url = ClassLoader.getSystemResource("ressources/logo.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
setIconImage(img);
This is similar to the previous answer, but I need to add a bit of information.
You can still use a direct path to your image (C:/User/logo.png) BUT imagine you give your program to someone else, he wont have the image in that specific path.
So I recemmend you insert it in your project like so:
(I usualy do a sperate package for any ressources).
so it will become ressources/logo.png and it will work for anybody opening your project.
import java.awt.*;
import javax.swing.*;
class set extends JFrame
{
set()
{
setSize(100,100);
setVisible(true);
setIconImage(new ImageIcon("navbit-home.png").getImage());
}
public static void main (String[] args) {
new set();
}
}
please set appropriate path.like C:/Documents and Settings/arjuns/Desktop/logo1copy.png
The image should be available in the JAR file you create. Then use getResource() to get the image from the jar file.
For example,
URL resource = this.getClass().getResource("resources/logo.png");
frame.setIconImage(new ImageIcon(resource).getImage());
Here the logo.png is located under 'resources' folder of the class file where this code is executed.

Netbeans ImageIcon not displaying

I am using the NetBeans GUIBuilder to make a JPanel Form. I added a JLabel and used NetBeans' interface to give it an icon from an external image (.png). The path is verified and the image shows up on the GUIBuilder screen. It even shows up when I click the "Preview Design" button. It DOES NOT show up when I RUN the project. The rest of the GUI appears as it should. Do any of you know why this happening and/or how to fix it?
A lot of you have been asking for an SSCCE. Since the code is generated by the NetBeans Form Builder, I have instead included the steps I took to make the JLabel. The areas of focus are circled in red.
Drag and drop a JLabel into the Form Builder.
Open up the JLabel's properties menu. Enter the empty string ("") for the text field. Click the ellipsis next to icon.
Select External Image and click the ellipsis.
Select the image of choice. In my case it's a .png.
Notice that the image appears in the icon preview.
Close the icon menu and the properties menu, and notice that the image appears as the JLabel's icon on the Form Builder.
Thank you for accepting an unorthodox SSCCE and thank you in advance for your help.
I found out the hard way that relying on Netbeans GUI builder to do everything for you is a mistake.
Just create an icon fetching class like the one below, put the icons in it's package, and use "Custom code" instead of "Image chooser". Sure the icons will not be visible inside NB. But if they show up when the app is running, who cares about that.
package com.example.resource.icons;
import javax.swing.ImageIcon;
public class IconFetch {
private static IconFetch instance;
private IconFetch(){
}
public static IconFetch getInstance() {
if (instance == null)
instance = new IconFetch();
return instance;
}
public ImageIcon getIcon(String iconName) {
java.net.URL imgUrl = getClass().getResource(iconName);
if (imgUrl != null) {
return new ImageIcon(imgUrl);
} else {
throw new IllegalArgumentException("This icon file does not exist");
}
}
public static final String MINESWEEPER_ONE = "one.png";
}
Usage:
IconFetch.getInstance().getIcon(IconFetch.MINESWEEPER_ONE);
If the icon still doesn't show up after trying this, then something might be wrong with the way you layed out components in your form (the label is there but you can't see it).
Hope this helps even though it's a long shot.
I had the same problem, and predi's solution wasn't working either. Then I created a package instead of a folder, and added the images there, and it works now.
I do have a same problem also. But I found the solution.
I create the package in project and put the images inside there.
When I build the project, Netbeans will create 'target' folder and build .class files.
I found that the images that I copied to the package, did not transfer to the 'target' folder.
Interim solution.
4. I copy all image to target folder with the same structure. Then I can run the project directly from Netbeans.
5. Incase you clean the project. Do no.4 again.

Categories

Resources