JPanel doesn't update until resize Jframe

java, paintcomponent, swing, user-interface

Solution

Verify that you invoke `setVisible()` after adding components and calling `pack()`, as discussed in this related example. You may also need to adopt an appropriate layout. Invoking `repaint()`, as suggested here, may fix the symptom but not the underlying cause.

Problem

I subclass JPanel to overwrite paintComponent(Graphics), I want to draw an image onto jpanel in a jframe. But my image hasn't shown up until I make a change to jframe's size. This is my code: ``` public class ImagePanel extends JPanel{ public void setImage(BufferedImage bi) { image = bi; revalidate(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if(image != null) { g.drawImage(image, 0, 0, this); } } } ```

Original source

Related problems