How to add picture to JLabel? [duplicate] - java

This question already has answers here:
Java: how to add image to Jlabel?
(5 answers)
Closed 7 years ago.
How to add a picture to JLabelin Java?

You can use ImageIcon
JLabel l = new JLabel(new ImageIcon("path-to-file"));

Try this code:
ImageIcon imageIcon = new ImageIcon("yourFilepth");
JLabel label = new JLabel(imageIcon);
For more Info

jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\admin\\Desktop\\Picture 34029.jpg"));
Here i posted the code that got generated automatically in netbeans.Hope my code helps in this regards keep coding goodluck.

You have to supply to the JLabel an Icon implementation (i.e ImageIcon). You can do it trough the setIcon method, as in your question, or through the JLabel constructor:
Image image=GenerateImage.toImage(true); //this generates an image file
ImageIcon icon = new ImageIcon(image);
JLabel thumb = new JLabel();
thumb.setIcon(icon);
I recommend you to read the Javadoc for JLabel, Icon, and ImageIcon. Also, you can check the How to Use Labels Tutorial, for more information.
Also have a look at this tutorial: Handling Images in a Java GUI Application

Related

Adding few images in Java JFrame

I'm making 2D game. I have background and character images. Both of them are .gif files. What layout or something do I need to use to set background behind the character? I tried some ways but either they're in a row or nothing happens.
I am adding pictures like that:
URL url = Main.class.getResource("images/main.gif");
ImageIcon imageIcon = new ImageIcon(url);
JLabel background = new JLabel(imageIcon);
Code for Background :
first of all copy your background image and paste in src of code
than set layout to borderlayout like this:
setLayout(new BorderLayout());
now add this code:
setContentPane(new JLabel new ImageIcon(getClass().getResource("image.jpg"))));
Note: add your image name here "image.jpg"
now set layout to flow layout like this
setLayout(new FlowLayout());
and write your code here

Inserting an image as a label in java

label_007= new JLabel("My Label");
In lieu of the above Label, My Label I want to insert an image in the same position. How can I do that? I am just a novice in Java. Please help
ImageIcon icon = new ImageIcon('image.png');
JLabel image = new JLabel();
image.setIcon(icon);
The above code should work.
Make an image icon with an image file
Make a JLabel
Then set your JLabel's Icon
You can use ImageIcon for images:
ImageIcon ICON = new ImageIcon(URLClassLoader.class.getResource(IMAGE_PATH));
JLabel imageLabel = new JLabel();
imageLabel.setIcon(ICON);
URLClassLoader.class.getResource is using for packaging the application in runnable-jar file.
The IMAGE_PATH is relative to your project folders/packages. for example if the image is in com.app.images package, the path will be /com/app/images/image.png.
If you're novice in Java and particularly in Swing, I encourage you to check my swing projects- Minesweeper and Pacman. They are well-documented, I built them when I learned Swing.
Good luck :)

Using JLabel for a background image

When I run the code it just opens an empty window
I also important whatever is necessary
relevant parts of the code:
public class Game extends JFrame implements ActionListener,KeyListener{
private JLabel background;
....
public Game(){
background=new JLabel(new ImageIcon("/graphics/board.gif"));
...
this.add(background);
this.setSize(800,600);
this.setVisible(true);...
I tried adding the JLabel to a JPanel and then add it to the frame but it still shows nothing in the window
Originally the code was:
JLabel background = new JLabel("/graphics/board.gif");
This would not set the image at the path described, Suggest that the following method is used (this could be simplified to just use a different JLabel constructor but steps shown for clarity)
Create and load the image and then set the icon for the Label As follows
ImageIcon icon = new ImageIcon("/graphics/board.gif");
JLabel background = new JLabel();
background.setIcon(icon);
Link to ImageIcon Java Doc
It is important to set in the layout the order in which the elements are displayed , maybe you have something that is displayed over the label..
I'm guessing you have a directory structure something like:
-c:\java
- source (for source and class files)
- graphic (for your images)
background=new JLabel(new ImageIcon("/graphics/board.gif"));
Don't specify the leading "/" in the file name. That tells Java to look at the root of the C drive, not at the directory where your class is executing from.
Also, don't use:
this.setSize(800,600);
The image does not stretch to fill the size of the frame. Intead you should be using:
this.pack();
so the frame will be the size of the image.

Changing the icon of my java application [duplicate]

This question already has answers here:
How do I change the default application icon in Java?
(10 answers)
Closed 9 years ago.
for a simple Java Desktop Application I added a JFrame Form with the assistant of the NetBeans IDE. For this frame I want to change the icon in the title bar.
I tried to do so with the following code at the very end of the constructor in the generated View class:
ImageIcon ii = new ImageIcon(iconUrl);
this.getFrame().setIconImage(ii.getImage());
The String iconUrl is definitely correct, the object ii seems to be alright as far as I can judge from the variables overview in the debugger perspective.
However, the icon in the title bar does not change, it's still the default java icon.
Why?
You can try this:
Image i = ImageIO.read(getClass().getResource("/path/to/image"));
setIconImage(i);
Note that here / would represent your src directory
Try this code
Image i = new ImageIcon(ClassLoader.getSystemResource("signal/icm/gui/images/oconp.png")).getImage();
setIconImage(i);
Hope following solution works for you:
ImageIcon icon = new ImageIcon(IconURL);
myImg = ImageIcon.getImage();
JFrame.setIconImage(myImg);

error adding image object

The following code produces error:
ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(10,10,Image.SCALE_DEFAULT);
mainPanel.add(scaleImage);
The error is cannot find method add(Image).
Why it is giving this error?
You can't do it like that. JPanel does not accept Image as parameter (that's what the error tells you).
You have two options:
draw the image in the panel instead. The solution uses the paintComponent(..) method.
use a JLabel and an ImageIcon
The answers to this question will show you how to do it either way.
ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(70,70,Image.SCALE_DEFAULT);
ImageIcon ii=new ImageIcon(scaleImage);
JLabel pic=new JLabel(ii);
mainP.add(pic); // now you can add

Categories