Question

How can i center image when i resize jpanel in swing ? my initial state of the Jpanel that its fits the size of the image ( with pack() ) but now when i resize the Jpanel with my mouse i will like to maintain the image in the center of the Jpanel relative to the Jpanel size.

Was it helpful?

Solution

Either:

  1. Easiest way is probably to use a border layout manager and add a Label with an image to the center. The layout manager will take care of changes.

  2. If you wish to draw the image manually (eg on paint()) you need to add a component listener and call repaint() on any component size change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top