I have a little problem with Swing (or maybe netBeans), I was making a testing JFrame, I changed its background by an image with this line of code:
this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src/testdesign/BG.png")))));
And I added a JLabel with an Icon to the frame using netBeans.
The result when compiling the project (In the IDE) was:
All is fine.. But when I generate a JAR file, the result is:
I'm not a Java pro, so I think that the way I changed the background isn't the proper way, or maybe it's another thing.
You use incorrect way to load background image. In your code image location depends on project directory src. You should load image from classpath but file system. You Class.getResource method to fix it e.g.:
this.setContentPane(new JLabel(new ImageIcon(getClass().getResource(pathToImage))));
Related
I'm creating a Java program with several windows (JFrame). One of them contains an image as a JLabel.
I have tried a lot to include the image in the .jar file exported from Eclipse , to allow the program to read them not only on my pc: however it doesn't work!
Basically, as soon as I press the button that should open the frame containing the image, nothing happens (this on the exported file, while on Eclipse it works perfectly).
I'm going to post the code, I state that I am just starting out as a programmer and I am trying to learn Java, but I am not too familiar with the subject yet.
Thanks to Anyone who will spend His time to make my request.
Richard
URL url = ClassLoader.getSystemResource("images/books.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel iconl = new JLabel(imageIcon); // the images has been saved in folder images, that is located in the bin folder
I have a problem with a program i wrote. The Problem is, that when i compile and execute it from my programming environment, everything works and shows up fine, but after i generate a .jar out of it, the frame itself shows up, but the jpanel with icons inside the JTabbedPane doesn't. I used Images in some icons in the jpanel, in other jpanels where i didnt use icons, it works after creating a .jar. i think the problem is getting the icons from the folder where the .jar is in but i dont know how to do this better.
Already tried:
ImageIcon icon1 = new ImageIcon(getClass().getResource("refresh.png"));
JLabel label1 = new JLabel(icon1);
and
URL url = home.class.getResource("refresh.png");
ImageIcon icon1 = new ImageIcon(url);
JLabel label1 = new JLabel(icon1);
but nothing seems to work after the .jar is cretated
In programming environment:
As generated .jar:
I searched everywhere but i didn't find an answer to this. If you need some more code let me know.
Try reading the file from the resources folder like this:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("refresh.png").getFile());
ImageIcon icon = new ImageIcon(file.toURI().toURL());
This will load the file from your classpath. For this to work, you will need to copy the file into the standard "resources" directory as shown in this example. And yes, you really do want to use this standard maven convention for storing files your application needs. It simplifies moving your project from development to other environments like test or production.
http://tinypic.com/r/25rcxa1/8
Hello everyone. I' am having trouble having my picture being viewable within the JButton. Why isn't this compiling? I have placed image in src folder as you can see from the picture of my dual monitor setup,which shows folder hierarchy and eclipse IDE.
Actually the code compiles, and you have a runtime exception. That's because you pass "../geek.jpg" to the ImageIcon constructor, and that's not how resource resolution works.
You should use "/geek.jpg" instead, provided the image will also be packaged along with your app (check your Eclipse deployment/packaging configuration)
I am currently using the following code segment to get the image for the interface.
ImageIcon img = new ImageIcon("./lib/logo.gif");
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(img);
lblNewLabel.setBounds(28, 65, 140, 100);
However, when i execute clean and build, the image does not appear when i execute the jar file. What seems to be the problem and where should I put the image file? Thanks in advance.
"However, when i execute clean and build, the image does not appear when i execute the jar file. What seems to be the problem and where should I put the image file?"
This is the reason you don't load from a file. You want to load it as an embedded resources from your class path with a URL
Your image should be in a package somewhere in the src during development. When you build, the IDE with copy that image for you into the class path, and add it to the jar
Then to load from the class path in your code.
Class.getResource(..) return a URL, so you want to do this
ImageIcon icon = new ImageIcon(MyClass.class.getResource("/images/logo.gif"));
In the code snippet you provided, the path to the image file is considered to be on the file system (the constructor of class ImageIcon takes a file name as an argument). If you want to load a file (or an image) from the jar itself, you need to use ClassLoader.getResourceAsStream(). Check this question.
While writing my application (used Eclipse as my IDE), I didn't realize the problem that my images would not be eventually bundled in the jar. Now after completing the project and creating the executable jar, I bumped into this problem. I have around 50 java classes, and I have used images/icons extensively in most of these classes (e.g. in menus, as JLabel icon, in buttons, as tab icons, etc). I have used the ImageIcon API in most of the cases.
One example:
JLabel myHeaderImage = new JLabel(new ImageIcon("images/myHeader.jpg"));
What's the best way to pull in the images in the jar. Do I need to change the way I have used the images in my project?
Use getResource():
JLabel myHeaderImage = new JLabel(new ImageIcon(getClass().getResource("/images/myHeader.jpg")));
Just put a full path to ur images, such as in my case:
ImageIcon icon = new ImageIcon("X:/ProjektyNetBeans/GUITest/src/data/world.png");
Then Clean and Build. File .jar will work fine. Good luck.