3 Replies Latest reply: Jan 9, 2013 12:29 AM by 980759 RSS

    Put an Image into a JPanel

      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(){
           public static void main(String[] args){
                mainClass window = new mainClass();
                window.setTitle("Mi título");

        • 1. Re: Put an Image into a JPanel
          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
            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
              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
                        JLabel picLabel = new JLabel(new ImageIcon( picture ));
                        setBounds(100, 100, 640, 480);