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.
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'm stuck again on a problem concerning SWT using Java RCP.
What I want :
I have a Composite in which I wanna load an Image in it.
How it's currently done :
For this, I use a SWT Label, and call the method setImage(image).
The image (img.jpg) is found in a directory called /img/ next to the /src/ one. The directory is added in the Build Path. Here's a screen to illustrate :
directories
I call this image as follows :
InputStream is = getClass().getClassLoader().getResourceAsStream("/img/img.jpg");
ImageData imageData = new ImageData(is);
Label label = new Label(picRoom, SWT.NONE);
label.setImage(new Image(display, imageData));
What's the problem ? :
Once again, when I launch the project, everything goes right. The image loads and displays perfectly. But when I export it, the image doesn't anymore. The problem comes from the path actually. When I make an image as new Image(display, absolutePathToTheImage), it loads even if exported. But I want the image in the /img/ directory, because if I launch the project from the .exe from another computer, I want it to have the picture, wherever it is (so in /img/).
I've tried many solutions across the internet (using URL in particular), but no one worked. And as I said, I need the image to be in the /img/ directory and to take the relative path. And, of course, to work after exporting the project as a .exe .
Thanks in advance.
Kosnyru.
First make sure your img directory is included in the build.properties includes so that it is included in the exported plugin.
Then use the FileLocator class to find the image using a relative path:
// Your plugin's bundle - there are several ways to get this
Bundle bundle = FrameworkUtil.getBundle(getClass());
// Relative path in the plugin
String path = "/img/img.jpg";
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
Image image = imageDesc.createImage();
Assuming your file is in resources directory, you can do something like:
JLabel label = new JLabel(new ImageIcon(this.getClass().getResource("/logo.png")));
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))));
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.
I am trying to fix this problem. Trying different solutions but nothing works. I am using NetBeans IDE. I created a project with this structure and files:
E:\java\project\ecadpb\src\ecadpb
The image files are in
E:\java\project\ecadpb\src\
I have specified working folder for my project in Netbeans as E:\java\project\ecadpb
I have created my image icons like this
new ImageIcon("device21.png");
In my source file, it works perfectly while running the project in Netbeans but images are not showing up when I build and run my JAR file separately. But the image files are inside the JAR.
I have also tried the answers for the same question asked previously.
URL imageUrl=getClass().getResource("device21.png");
new ImageIcon(imageUrl);
But it doesn't work in my case. I am building a JAR file for the first time. Can anyone help me with this!!
A simple way of doing this will be to add the image in your classpath or a directory in your classpath say img as shown below:
E:\java\project\ecadpb\src\main\java\img\device21.png
And then load your image from this location like this:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("img/device21.png");
ImageIcon icon = new ImageIcon(resource);
Create a source folder called Images (ie if you're using eclipse, right-click your project -> new ->sourceFolder). Call it whatever you want, i called my Images. Put some images in it.
Now i had JLabels where i gave them ImageIcons. Look at the following code.
ImageIcon BPawn;
ImageIcon WPawn;
JLabel Label = new JLabel[8][8] //2D array of labels that gives each spot a position.
public void setPieces(){
//set up pawns in their respective positions.
BPawn = new ImageIcon("Images/BPawn.png", "BPawn");
WPawn = new ImageIcon("Images/WPawn.png", "WPawn");
for(int i=0;i<Label[0].length;i++){
Label[1][i].setIcon(BPawn);
Label[6][i].setIcon(WPawn);
}//end for
}//end setPieces.
There is a lot more in setPieces() method, but this glimpse is how you would reference the images in your source folder when you create an executable jar and want the images to show up.
I think answer could be one these suggestions here
I have used a similar approach,
a) Specify the package path to the image file name.
b) Make sure that the image file is not ommited by your build scripts and that it is present in your jar file.