Simple ImageIcon issue - java

I can't get icons to appear on my button. I've copied this code from a book as I'm a beginner trying to grasp Java programming. Unfortunately I can't progress without getting this working, the other exercises are based on GUI images. I believe I've followed the exact steps, hopefully somebody can help!
Here is the simple code:
package Chapter13;
import javax.swing.*;
/** #author Chris */
public class TestButtonIcons extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new TestButtonIcons();
frame.setTitle("ButtonIcons");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public TestButtonIcons()
{
ImageIcon usIcon = new ImageIcon("image/usIcon.gif");
ImageIcon caIcon = new ImageIcon("image/caIcon.gif");
ImageIcon ukIcon = new ImageIcon("image/ukIcon.gif");
JButton jbt = new JButton("Click it", usIcon);
jbt.setPressedIcon(caIcon);
jbt.setRolloverIcon(ukIcon);
getContentPane().add(jbt);
}
}
File Hierachy:
As you can see I've copied the "image" file twice as an attempt to try and debug this. All the images are in the "image" folder despite the photos not showing them all.

you need to create the image folder outside of the source package when you run from the netbeans while when you run directly executing the jar then you need to put the executable jar file and image folder in same folder or same place

It seems a bit of a problem with BookExcercises; that is not a normal project.
A normal NetBeans project (you can see all in the Files tab to the right of the Projects tab): there is a src directory shown under Source Packages. Building a project fills a target directory. Now a build would put the .class files there and also copy your images. Run (project) would run with as class path the target directory. Run File however might not find the images in the target directory.
A solution depends on the BookExcercises project. You might create a new project for every excercise. Depends.

If you are using a command Prompt to run your program, then simply paste this image folder alongside your chapter13 package folder(which contains your TestButtonIcons.class).
Or if you using some IDE, check where your compiled stuff goes. Again paste your image folder alongside your chapter13 package folder (which contains your TestButtonIcons.class). That will do the needful to run the program as you are expecting.
Regards

Related

Java - File writing/reading works on Windows but not Mac

I'm currently developing an application that I wish to work on both Windows and other operating systems.
I have run into a problem where the files that are being called from folders in my program are not appearing on macOS, but ARE appearing on Windows.
All of my files are being written and read the same way as this example GUI code:
public class GUI extends JFrame {
private JLabel imageLabel;
public GUI() {
this.setTitle("test");
this.setSize(500,349);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
imageLabel = new JLabel(new ImageIcon("assets/test.png"));
this.getContentPane().setBackground(new Color(20,50,250));
this.getContentPane().add(imageLabel);
this.setSize(500, 350);
}
}
On Windows, the GUI program looks like this:
On MacOS, the GUI program looks like this:
I would like to add that these are being run on the operating systems via a Runnable Jar file exported from Eclipse IDE. The version that is being used is JavaSE-1.8.
The problem line lies here:
imageLabel = new JLabel(new ImageIcon("assets/test.png"));
My Solution:
imageLabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("test.png")));
Explaination:
My original intention was to somehow incorporate the resources of the program itself to work together with the JAR executable. In my testing (I'm new with making executables), I found that nothing would work unless I had a separate folder "assets" that the user would also download with the jar file. While this solution did work for a little bit, some reading and writing seemed to be different on some machines.
In order for this to work, you must add the "assets" folder or whatever folder has your images inside of them to your Java Build Path.
From here, it works on all tested machines; Windows and MacOS.

Trying to include an image in a .jar exported file

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

Loading an Image in Java/Gdx

I have been developing an android game for a while now and i have this problem. Let's take this example code
class game {
private Texture image;
public game()
{
image = new Texture(Gdx.files.internal("image.png");
}
}
If i write my code like this everything is fine, the image loads properly and the game runs smoothly.
The problems start when i try to create a folder inside my assets folder and load the image from there.
For example
class game {
private Texture image;
public game()
{
image = new Texture(Gdx.files.internal("newfolder/image.png");
}
}
When i do this and compile it i get no erros inside Eclipse and everything is fine. But when i try to export the project into a runnable deskop jar or as an android application, the game start for a second, shows a black screen then it stops. On android it doesn't even start. I believe it is not able to locate and load the image and throws an exception.
Any idea why this is happening and what am I doing wrong?
When you export your executable in Eclipse you need to package the required assets into the jar.
Often times when you export a JAR file it doesn't include the added library folders, and it searches for an external folder wherever the JAR is located.
Try checking the option for packing required assets into the JAR. This will actually put the assets inside the JAR file and make the necessary changes to the manifest.
I would also double check the LibGDX wiki and make sure that your projects build paths are properly configured to use assets folder from the android project.

Displaying images within a jar file

I know this question has been asked before, but I have gone over all the solutions I could find and cannot make any of them work for me. I have a program in Eclipse that I am trying to export to an executable jar file. It is the first time I have done this. When I execute the jar, my program images do not show up. So I did some research and found I need to load them as resources, but I cannot seem to make it work.
Here is the code I was using to load the images without the jar:
private void initComponents()
{
// create an enterprise icon and make it invisible
enterpriseIcon = new JLabel(new ImageIcon("res/enterprise1.png"));
enterpriseIcon.setVisible(false);
}
I have all my images in a folder named res in my root project directory, and I told Eclipse to put this folder in the build path.
When the images wouldn't show up when running the jar file, I tried the following:
enterpriseIcon = new JLabel(new ImageIcon(getClass().getResource("/res/enterprise1.png")));
and
enterpriseIcon = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/enterprise.png"))));
However, when either of these is run from within Eclipse, I get a null pointer exception. (I tried a few other things as well, but the above were the only solutions I thought I understood.)
Any help getting this to work would be appreciated.
Use Class#getResource to get the URL of your image inside the jar file
enterpriseIcon = new JLabel(new ImageIcon(getClass().getResource("/res/enterprise1.png")));
Try this. This works well for me :)
private void initComponents() {
try {
BufferedImage icon = ImageIO.read(getClass().getResource("/enterprise1.png"));
frame.setIconImage(icon);
} catch (IOException e) {
e.printStackTrace();
}
}
I was also facing the same issue till yesterday. I have got a fix for this. Let me share it with you.
In your Eclipse, go to your Project name.
Right your project name.
New -> Source file/folder
Name this Source file/folder as images
Now on your local machine go to the Eclipse workspace where your project
is physically present.
Copy ALL your images into the newly created
'images' folder right there only.
Now go back to Eclipse.
Right click on your Project -> Refresh
Now, wherever you are using images in your .java files , go to those lines and prefix the image location with images/
Example : XXX xxx = XXXX (example.jpg)
Change it with .... XXX xxx = XXXX (images/example.jpg)
BOOM !!
The images will be shown when you run the .jar file.
NOTE : Do not run the .jar file from cmd using java -jar AppName.jar
Instead, just double click the .jar file and you will be seeing the images.
If it works for you, kindly upvote. :)

Image not loading in jar file

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.

Categories