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

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.

Related

Application icon not following the exported project?

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.

using #search parameter on a JWebBrowser grays out display field

I´ve already searched for related posts on stack, but didn´t find quite the right answer;
Im using THIS to display a .pdf file in my frame.
Now I want to use the JWebBrowser.navigate()+ (filePath + "#search=anyString") to search this .pdf file for the specific string.
Unfortunately I´m unable to reload the JWebBrowser afterwards. So the correct filepath is submitted (checked that in the pdf adress bar), but the JWebBrowser turns gray and nothing happens.
When using the navigate() to load another file and afterwards navigate to the old file again, it works just fine.
I tried revalidate(), repaint() and stuff like this but I cant get this to work.
Example:
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
browser.navigate(filePath + "#search=flower");
browser.revalidate();
browser.repaint();
}
});
I appreciate any kind of advice!
Thanks!
meanwhile I've found a solution, although it might be not the perfect one:
You can just remove the JWebbrowser object from your current Frame/Panel and just add it again.
This way it will work just fine.
Just in case someone is having a related problem to this one.
Greetz

How to use an image in a java project without an absolute path

I've been working in a little project to get a bit of practice programming. It's basically done, but I won't be satisfied until I can use images properly, a bit of help would be appreciated.
So, currently I'm using the the getImage method from the ImageIcon class, like so:
Image body = new ImageIcon("C:/Users/Centollo/Documents/NetBeansProjects/Chess/build/classes/chess/img/WhiteBishop.png").getImage();
I've been trying to figure out how to do the same thing without using an absolute path, but I don't know how to make the images a part of the jar so that it works fine in any other machine.
All I need to know is where to put the images and how would the code to access them look like.
Try to explain it like I'm stupid, please. I've read answers to similar questions but I can't make heads or tails of them.
I'm working in NetBeans with a "chess" package with all the .java and a "chess.img" package with all the .png.
If your class extends from JFrame, you can do this:
Image image = new ImageIcon(this.getClass().getResource("/images/MyImage.jpg")).getImage();
If your class extends Applet, you can go this way:
private URL base = null;
private Image myImage = null;
try
{
base = getDocumentBase();
} catch (Exception e)
{
e.printStackTrace();
}
myImage = getImage(base, "images/MyImage.jpg");
A very quick google search yields this:
URL resource = getClass().getClassLoader().getResource( "img/WhiteBishop.png" );
Image body = new ImageIcon( resource );
There are a couple ways to do this but here's the way I would suggest:
Make sure the chess.img folder is in your application's classpath. Then try referring to the path like chess.img/image (yes, you can use forward slash in windows.)
If that doesn't work use:
ChessClass.class.getClassLoader().loadResourceAsStream("/chess.img/image");
Note the forward slash at the beginning of the file reference. This points to the root of the classpath. It's a bit confusing as someone with unix/linux experience might think it refers to the root of the file system. This tends to work better than the other answer given for reasons I knew 10 years ago. This is an ugly bit of Java that was never quite cleaned up.

getResourceAsStream() returning null in jar but fine in eclipse

I have a program in Java using LWJGL. It runs fine in Eclipse, but when I try to compile it as a jar file it crashes, giving me a NullPointerException. It's been asked before, I know, but I don't seem to get an answer that works. Any help here? Thanks in advance!
The thing that seems to be having the problem is the class TextureHelper:
public class TextureHelper {
public static Texture LoadTexture(String texture)
{
try {
return TextureLoader.getTexture("PNG", ResPlaceholder.class.getResourceAsStream(texture));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Some notes:
I've also tried "/res/" + texture, as well as many other things like it.
ResPlaceholder is a class that sits in the res folder where I store all my images. It's a blank empty class.
This works perfectly in Eclipse.
My jar has these folders (just as an example):
foo.jar
----core
--------TextureLoader
----res
-------- Assorted Image Files
-------- ResPlaceholder
This is the same as the packages in eclipse.
Any help you have would be appreciated, I've been stuck on this for days with no progress.
EDIT:
META-INF/MANIFEST.MF
Main.class config/
config/Images.class
core/
core/LevelLoader.class
core/TextureHelper.class
core/TileSet.class
-Skipping some other stuff that has nothing to do with this-
res/
res/ResPlaceholder.class
res/BlankImg.png
res/test.txt
res/testImg.png
res/testTiles.png
If the path String is something like: /res/testImg.png it should work.
i.e.,
String resourcePath = "/res/testImg.png";
InputStream inStream = ResPlaceholder.class.getResourceAsStream(resourcePath );
BufferedImage img = ImageIO.read(inStream);
// use the img BufferedImage here
// for example:
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);
As an aside, it's always good to test problems and check new concepts in small test programs. In your case, I would create a small simple program with just a main method that attempts to extract a jar's image resource and display it in an option pane much like my small code above does. I'd try to avoid long lines and instead separate each step on its own line, again similar to my post above. This way if an exception is thrown, the code on the line will be much more informative as to what is causing the error.

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