Java Swing: ImageIcon not appearing in eclipse? - java

I am designing a GUI. I have created a Menubar and added Menu items to Menu and Have setMenubar.
My problem is i am not able to add an icon (icon is not appearing in eclipse) to the Menuitem.
This is how I did:
I have my .png file in D:/something/src/resources/new.png
JMenuBar menuBar = new JMenuBar();
JMenu File = new JMenu("File");
menuBar.add(File);
java.net.URL imageURL = this.getClass().getResource("/resources/new.png");
System.out.println(imageURL); //imageURL is printing correctly in console
ImageIcon im = new ImageIcon(imageURL);
JMenuItem newItem = new JMenuItem("New", im);
File.add(newItem);
setJMenuBar(menuBar);
I am facing the similar problem adding icon to a button in Toolbar too. Guess, it is same cause. Could anyone please tell me what I am doing wrong.
Note: I have tried with .jpg, .jpeg and .ico files too. But nothing is appearing in eclipse!. I am using Windows & MS Access database.

A file in resources would typically end up at the root of a Jar. Try:
java.net.URL imageURL = this.getClass().getResource("/new.png");
If that fails for you, expand the Jar and check the image is located where you think it is.

Hopefully the resources folder, is the Source Folder that you had created by right clicking your Project. So you can use this also to retrieve images.
URL url = ClassLoader.getSystemResource(path);
where path = "new.png"
Or as quoted by you :
"This is how I did: I have my .png file in D:/something/src/resources/new.png"
Try to change the location of the resources folder inside classes/bin folder instead of src. So it must look like this D:/something/classes/resources/new.png
Now if you will use
URL url = ClassLoader.getSystemResource(path);
then path = "resources/new.png";
Then i guess your way of doing will run too.
Hopefully this might help you in some way.
Regards.

Related

Maven path to resources

I am ending my project and I want to add some photos as Icons.
This project is in Maven.
I just dont know how to get path correctly. I tried all possible paths.
JLabel fLabel = new JLabel("Text");
fLabel.setBounds(375,5,50,50);
ImageIcon icon = new ImageIcon(".//resources/flag.png");
fLabel.setIcon(icon);
this.add(fLabel);
the content of the src/main/resources/ directory from Maven is directly added to the class path of your compiled jar.
So it should work with directly addressing the file without a path.
ImageIcon icon = new ImageIcon("flag.png");
Your images are in the correct directory.
In your code, instead of passing the path, pass the URL instead as following:
var flagIconUrl = this.getClass().getClassLoader().getResource("flag.png");
var icon = new ImageIcon(flagIconUrl);

How do I add an image to JPanel using imageicon (Java GUI)?

I looked all over the place but I am still stuck on how directory works to find the image to put onto the JPanel. Where is the image supposed to be? I clicked on properties for my image and it shows Location: C:\Users\Joseph\Pictures\Background and the picture's name is random.jpg.
I am trying to add an image to a tab using tabbedPane. Here is what I have so far, and I am not able to do it.
JPanel flPanel = new JPanel();
flPanel.setLayout(new FlowLayout());
ImageIcon image = new ImageIcon(getClass().getResource(""));
// Tried /Users/Joseph/Pictures/Background/random.jpg and doesn't work
JLabel j1 = new JLabel(image);
flPanel.add(j1);
tabbedPane.add("Tab 2", flPanel);
Is the picture supposed to be in the same package file as the project? Or is it supposed to be in the source file to be able to just do "random.jpg"?
If you want the image to be available to your application at runtime, then you should consider making sure that the image is included within your Jar when you application is built.
From the sounds of it, you are using Netbeans, you should copy the image to a directory within your src directory of your project.
You should then be able to use...
BufferedImage bi = ImageIO.read(getClass().getResource("/full/path/to/image/random.jpg"));
ImageIcon image = new ImageIcon(bi);
The path to the image should be the full path (from the context of the src directory) within your project.
That is, if you placed the image in the resources directory within the src directory, then you would use /resources/random.jpg as the path/file name
Take a look at Reading/Loading an Image for more details
getClass().getResource(...) will only get resources inside the classpath.
You can use ImageIO.read(File) like this:
BufferedImage bi = ImageIO.read(new File("C:\\Users\\Joseph\\Pictures\\Background\random.jpg"))
ImageIcon image = new ImageIcon(bi);

Not sure about image on JButton

I have already tried the following =
ImageIcon clear = new ImageIcon ("icons\delete1.png");
JButton clearBT = new JButton(clear);
And it works perfectly, I just have a question to make. The directory of the image is into my hard disk, but I want to deliver my project to my professor, so I am not sure if the image will still show up when in his computer. The thing is that I am not sure if the directory that I've put in my code (which exactly is "C:\\Users\\George\\Desktop\Giorgos\\icons\\delete1.png") will have something to show on my professor's pc.
Thank you in advantage for your answers, and if I am not clear enough, I am willing to rephrase.
If icons folder is in project directory folder and in code you are calling the image with :
ImageIcon clear = new ImageIcon ("icons\delete1.png");
And It's working perfectly ,then It will work anywhere.
Just Make sure you are not calling the image location address as its complete address that is referring to your computer hard disk.
If Your Professor's PC has internet access then you can use something like
(Please upload your file somewhere on internet like imgur and pass that url of your image new URL(here))
URL lenna =
new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png");
ImageIcon clear = new ImageIcon (URL);
JButton clearBT = new JButton(clear);
Put the picture in you projects and you will have to change your code to
JButton clearBT = new JButton(new ImageIcon(getClass().getResource(image_path)));
Another option would be to ship the image bundled with your source code. For instance, create a "package" / directory inside your project hierarchy and store images there.
Then, you can load them this way:
ImageIcon image = new ImageIcon(getClass().getResource("/path/to/the/image");
For instance, if the image is under org/example/program then the above will be:
ImageIcon image = new ImageIcon(getClass().getResource("/org/example/program/image.png");

Locating resource in Jar

I am having difficulty creating the image icon. I want to package a jar and have the images available to be displayed in the gui. Both of the following throw null pointer exceptions. The path is a path directly to a package in my Eclipse project that contains the necessary images.
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource(
"/TicTacToe/src/edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png")
.getPath());
and
ImageIcon icon = new ImageIcon(getClass().getResource(
"/TicTacToe/src/edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png"));
I can't see to be able to access the appropriate package. Any suggestions?
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader()
.getResourceAsStream("edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png");
Try like this
JButton myButton =new JButton(“press me”);
myButton.setIcon(new ImageIcon(image));
EDIT:
Refer to this discussion to know loading the image from executable Jar files

Java swing app can't find image

I'm making a torpedo game for school in java with swing gui, please see the zipped source HERE.
I use custom button icons and mouse cursors of images stored in the /bin/resource/graphics/default folder's subfolders, where the root folder is the program's root folder (it will be the root in the final .jar as well I suppose) which apart from "bin" contains a "main" folder with all the classes. The relative path of the resources is stored in MapStruct.java's shipPath and mapPath variables.
Now Battlefield.java's PutPanel class finds them all right and sets up its buttons' icons fine, but every other class fail to get their icons, e.g. Table.java's setCursor, which should set the mouse cursor for all its elements for the selected ship's image or Field.java's this.button.setIcon(icon); in the constructor, which should set the icon for the buttons of the "water".
I watched with debug what happens, and the images stay null after loading, though the paths seem to be correct. I've also tried to write a test file in the image folder but the method returns a filenotfound exception. I've tried to get the path of the class to see if it runs from the supposed place and it seems it does, so I really can't find the problem now.
Could anyone please help me?
Thank you.
You need to load icons like this:
ClassLoader cl= this.getClass().getClassLoader();
URL imageURL = cl.getResource("resources/...");
ImageIcon icon = new ImageIcon(imageURL);
And you need to add your resource folder to the classpath in Eclipse. Note that the path is not a file path, this way it will work if you decide to bundle your app in a jar file.
btnRegistration.setIcon(createImageIcon("reg.png"));
protected ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Master.class.getClassLoader().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.out.println("Couldn't find file: " + path);
return null;
}
}
here btnRegistration is my JButton
Master is my Class
and reg.png is my image that is belong in my project

Categories

Resources