silentwinter
silentwinter

Reputation: 107

How to resize an image in a JLabel as the window resizes? How to maintain proper scale?

Say I've got an image in a JLabel. Here's how I'm currently adding it:

p=resizeImage(p,lbl.getWidth(),lbl.getHeight());
lbl.setIcon(p);

I'd like to resize this as the label is resized.

I could write a small function to get the image width to height ratio, get the label width and height, find the smaller of the two(proportionally) and calculate the new height and width manually. That would not be an issue, but I wonder if there's a cleaner way.

Upvotes: 0

Views: 1496

Answers (1)

Mot
Mot

Reputation: 29600

You could listen to resize-events of the label or, what I would prefer, paint the image directly (replacing the JLabel with JComponent).

Upvotes: 1

Related Questions