thechrishaddad
thechrishaddad

Reputation: 6735

responsive image in swing

Just wondering if anyone knows how to make an image in a java application using swing adjust based on your screen size, at the moment when i maximize my application the image stays at the same size, it does not re-adjust, any ideas?

JLabel logo = new JLabel();
ImageIcon imgLogo = new ImageIcon("path-to-image/logo.png");
logo.setIcon(imgLogo);
add(logo);

This is the code i have so far, it is positioned at the top of my application, i only want it to adjust horizontally when i re-size the window.

Like in web design you can usually just set the image to a percentage value, but i am unsure about how it is done in Swing or if it is even possible.

Upvotes: 1

Views: 2159

Answers (1)

Aniket Inge
Aniket Inge

Reputation: 25705

You may have to redraw the PNG file each time the width changes.

add a ComponentListener to the JFrame, and override the componentResized method. Then you will have to use the Image.getScaledInstance method to resize the image depending upon the size of the JFrame

Upvotes: 1

Related Questions