This discussion is archived
3 Replies Latest reply: Jan 8, 2013 10:29 PM by 980759 RSS

Put an Image into a JPanel

983618 Newbie
Currently Being Moderated
I don't know if I am doing it well, this is my code:

import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class mainClass extends JFrame implements Runnable {
     
     private static final long serialVersionUID = 1L;
     
     Container content = new Container();
     JPanel background = new JPanel();
     String path = "imagen.png";
     ImageIcon Imagen = new ImageIcon(path);
     JLabel Img = new JLabel();
     int arreglo[] = new int[10];

     public mainClass(){
          this.setBounds(100,100,640,480);
          this.setVisible(true);
          this.add(content);
          
          content.add(background);
          content.setVisible(true);
          background.add(Img);
          background.setVisible(true);
          Img.setIcon(Imagen);
          Img.setVisible(true);
     }
     
     public static void main(String[] args){
          mainClass window = new mainClass();
          window.setTitle("Mi título");
     }

}
  • 1. Re: Put an Image into a JPanel
    gimbal2 Guru
    Currently Being Moderated
    980615 wrote:
    I don't know if I am doing it well, this is my code:
    That's not a question nor a problem description. Did you try it? What happened? What was wrong about it?

    Note that you generally only have to call setVisible() on the frame, not on the components in it.

    And PS: please use \
     tags when posting code.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 2. Re: Put an Image into a JPanel
    983618 Newbie
    Currently Being Moderated
    My question is in the title. The unique things that is not happening is: the image is not appearing in the window. And I don't know if I am doing something bad.
    If I only use: this.add(Img); -> The image appear, but no in the JPanel. I want to insert the image in the JPanel and show it.

    Ok, I delete the other setVisible, and I only let the "this.setVisible".
    The problem is still the same.
  • 3. Re: Put an Image into a JPanel
    980759 Newbie
    Currently Being Moderated
    try the below and remove Runnable interface and check your image path
    BufferedImage picture = null;
              try {
                   picture = ImageIO.read(new File("/untitled.png"));
              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              }
              JLabel picLabel = new JLabel(new ImageIcon( picture ));
              add(picLabel);
              setBounds(100, 100, 640, 480);
              setVisible(true);

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points