Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Need help in getResource(My problem on eclipse and java in using import images)
Need help in getResource [message #1481831] Fri, 21 November 2014 08:58 Go to next message
neo sluf is currently offline neo slufFriend
Messages: 1
Registered: November 2014
Junior Member
Can someone tell me why this is not working?

This is from a tutorial. I read a lot on different threads about this problem and I found out that I need to create a new Source Folder. I name it to Images then add Hello.jpg to it by dragging the image into the folder and it works.

Now they said that in order to use the image you need to put "/nameofthefolder/file.filetype/"
So I put it in the correct path.

But my problem is, it is not working.. My code is very simple.. Add an image to an JLabel

I can't put an picture on because I need 10 rep.. but I create a Source Folder and add Image to that folder correctly I promise

I know this question sounds stupid but I am very new to programming... please have a kind consideration on the beginner..


package myGUI102;

import java.awt.*;
import javax.swing.*;

public class myGUIAddImage extends JFrame{
private ImageIcon image1;
private JLabel label1;


myGUIAddImage (){
setLayout(new FlowLayout());

image1 = new ImageIcon (getClass().getResource("/Images/Hello.jpg"));
label1 = new JLabel (image1);
add (label1);



}

public static void main(String[] args) {
myGUIAddImage mygui = new myGUIAddImage();
mygui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mygui.setVisible(true);
mygui.pack();
mygui.setTitle("My Image");


}

}
Re: Need help in getResource [message #1482009 is a reply to message #1481831] Fri, 21 November 2014 11:58 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29536
Registered: July 2009
Senior Member
Neo,

Asking about Swing applications is best done on some general forum such
as Stackoverflow. If you put the image in the same folder as the class,
you can just use "Hello.jpg. I think if you use /Images, it would need
to be in the root of the jar/bin...

On 21/11/2014 10:13 AM, neo sluf wrote:
> Can someone tell me why this is not working?
>
> This is from a tutorial. I read a lot on different threads about this
> problem and I found out that I need to create a new Source Folder. I
> name it to Images then add Hello.jpg to it by dragging the image into
> the folder and it works.
>
> Now they said that in order to use the image you need to put
> "/nameofthefolder/file.filetype/"
> So I put it in the correct path.
>
> But my problem is, it is not working.. My code is very simple.. Add an
> image to an JLabel
>
> I can't put an picture on because I need 10 rep.. but I create a
> Source Folder and add Image to that folder correctly I promise
>
> I know this question sounds stupid but I am very new to programming...
> please have a kind consideration on the beginner..
>
>
> package myGUI102;
> import java.awt.*;
> import javax.swing.*;
> public class myGUIAddImage extends JFrame{
> private ImageIcon image1;
> private JLabel label1;
>
>
> myGUIAddImage (){
> setLayout(new FlowLayout());
>
> image1 = new ImageIcon
> (getClass().getResource("/Images/Hello.jpg"));
> label1 = new JLabel (image1);
> add (label1);
>
>
>
> }
> public static void main(String[] args) {
> myGUIAddImage mygui = new myGUIAddImage();
> mygui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> mygui.setVisible(true);
> mygui.pack();
> mygui.setTitle("My Image");
>
> }
> }
>
Previous Topic:Why should I use Eclipse instead of Xcode?
Next Topic:Open CustomSearch Page Tab within Default Eclipse Search Page
Goto Forum:
  


Current Time: Wed Sep 19 09:13:02 GMT 2018

Powered by FUDForum. Page generated in 0.02849 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top