• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding image to JPanel to display on JLable

 
nate carpenter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has been asked many times before and I've tried just as many answers but none have worked so far. What I'm trying to do is very simple. I want to add an image to a JLable and have it change based on the user input. I can do everything except actually add the image. It seems like it should be really simple but so far it hasn't. Here is the code. Thanks for any help!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The easiest way to add/change images to a label is to use the setIcon(Icon icon) method.

nate carpenter wrote: Here is the code

Looks like you forgot to add your code
 
nate carpenter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've tried using setIcon() but am not really sure how it is done.
 
nate carpenter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! looks like I put then code in wrong sorry about that.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. I corrected your code tags.

The code you posted does not use JLabel. On a side note, does it even compile for you?

I am a bit confused on what you want to do here.
If you want to use an image as the panel background, then check out https://www.coderanch.com/how-to/java/BackgroundImageOnJPanel
To change the image you will need to provide a setter in your panel. Inside the setter, set the image reference to the new image reference you get and call a repaint.

If you want to use the JLabel
1) Create JPanel instance
2) Set the layout to BorderLayout* (if you want the label to occupy the while panel)
3) Create JLabel instance
4) Add the #3 to #1
5) The invoke the setIcon on #3. No need to explicitly invoke repaint or anything.

*More on layout managers here http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
 
nate carpenter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. Here is what I came up with I am using an image in the same folder as my program.


It still doesn't compile. here are the errors.

ImagePanel.java:14: error: cannot find symbol
setTitle("ImagePanel");
^
symbol: method setTitle(String)
location: class ImagePanel
ImagePanel.java:16: error: cannot find symbol
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
symbol: method setDefaultCloseOperation(int)
location: class ImagePanel
ImagePanel.java:20: error: cannot find symbol
JLabel pic = new Jlabel();
^
symbol: class Jlabel
location: class ImagePanel
ImagePanel.java:22: error: method setIcon in class JLabel cannot be applied to given types;
pic.setIcon("test.png");
^
required: Icon
found: String
reason: argument mismatch; String cannot be converted to Icon
4 errors
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error 1 and 2: those methods exist on class JFrame. You're calling these inside a JPanel sub class though.

Error 3: the error marker seems to be misplaced - the error is in Jlabel instead of JLabel. Remember, Java is case sensitive.

Error 4: the error message says it all. setIcon takes an Icon, you provide a String. You need to create an Icon and pass that to the method. Take a look at class ImageIcon.
 
nate carpenter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This program compiles and runs but I don't actually get a window that pops up or anything.
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you actually tell the frame to show itself?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!