0

I am creating a simple program in which I want to display an Image and a button.So I've created a JFrame.

This JFrame contains tow JPanel and one of the panels contains JButton and the other panel contains my image. But image is not displaying in the panel. When I add image to the JFrame, it is displaying normally. Please help with the following code!

main() method:

public class NewClass2 {
public static void main(String args[]){

    EventQueue.invokeLater(new Runnable(){
        public void run(){
            JFrame frm = new JFrm();
            frm.setVisible(true);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setSize(500,500);
        }
    });

}
}

JFrm class which initialize JFrame:

class JFrm extends JFrame{

JButton button;
JPanel panel0,panel1;

JFrm(){

    panel0 = new JPanel();
    panel1 = new JPanel();
    button = new JButton("Start");
    button.setPreferredSize(new Dimension(100, 30));
    panel0.add(button);

  panel1.add(new ImageComponent());   //adding image component to panel1 is not working
  //add(new ImageComponent());      //adding image component to the frame is working .. 


    add(panel1,BorderLayout.PAGE_START);
    add(panel0,BorderLayout.PAGE_END);

}

}

ImageComponent class which adds the image:

class ImageComponent extends JComponent{
Image img;
ImageComponent(){
    img = new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png").getImage();

}


@Override
public void paint(Graphics g){

    g.drawImage(img, 100,100 , null);
}

}

How can I add image to JPanel?

  • You `ImageComponent` should override the `getPreferredSize` method of `JComponent` and return a suitable size, probably the size of the image. This way, the `FlowLayout` of the `JPanel` will know how much space it needs in order to display the component. The reason it probably works when you add it to the `JFrame` is because of the fact it's using a `BorderLayout` and your specify the size of the frame instead of using `pack` – MadProgrammer Jun 01 '16 at 09:21
  • As a general preference, you should use `paintComponent` of `paint`, have a look at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details – MadProgrammer Jun 01 '16 at 09:21
  • Thank you @MadProgrammer –  Jun 01 '16 at 12:43
  • `new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png").getImage()` Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jun 01 '16 at 13:12

2 Answers2

0

Consider wrapping the image in a JLabel before adding it to the JPanel. See Display a jpg image on a JPanel

class JFrm extends JFrame{

JButton button;
JPanel panel0, panel1;

JFrm(){

    panel0 = new JPanel();
    panel1 = new JPanel();
    button = new JButton("Start");
    button.setPreferredSize(new Dimension(100, 30));
    panel0.add(button);

    ImageIcon image = new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png");
    panel1.add(new JLabel(image));

    add(panel1, BorderLayout.PAGE_START);
    add(panel0, BorderLayout.PAGE_END);

}
}
Community
  • 1
  • 1
  • This does not answer the question. The OP is asking for a way to display a custom `JComponent`, not a `JLabel`. – Abdul Fatir Jun 01 '16 at 08:40
0

You did not set a size for your custom JComponent so it'll be squished by the LayoutManager to zero size. Use setPreferredSize() to set whatever size seems suitable.

ImageComponent(){
    ImageIcon temp = new ImageIcon("test.png");
    img = temp.getImage();
    setPreferredSize(new Dimension(temp.getIconWidth(),temp.getIconHeight()));
}


@Override
public void paint(Graphics g){
    g.drawImage(img, 0,0 , null);
}  
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jun 01 '16 at 09:19
  • 1
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Jun 01 '16 at 10:32
  • `g.drawImage(img, 0,0 , null);` should use the image observer implemented for any `JComponent` i.e. `g.drawImage(img, 0,0 , this);` – Andrew Thompson Jun 01 '16 at 13:11