0
votes

so I have my JFrame set to fullsize

temp.setExtendedState(JFrame.MAXIMIZED_BOTH); 

I add 3 panels (borderlayout), in panel1 I have JLabel to display an image using:

img.getScaledInstance(600, 400,
        Image.SCALE_SMOOTH);

panel2 the same , panel3 just some buttons. (I will not post the whole code here), but this code runs great. Now what I want to do is: Jframe is fullsized, and 45% is taken by panel1, 45% by panel2, 10% panel3. If my picture to dsiplay is too big to fit 45% it gets scaled to fit this 45% of JFrame, if it's smaller than it's just displayed. Any ideas ?

1
You mean you want the image to resize when the size of the container changes? - MadProgrammer
Perhaps this and this might help - MadProgrammer
Hi, I will check it out, but just to explain: First I want to divide JFrame which is in fullsize into: 45% , 45%, 10% so u see 1 picture, under it there is another picure and on the botton u have some buttons (10%). If the picture is too big to fit 45% it gets scaled if its OK then its just displayed - Pirate
Or i can say it like this: the size of container Jpanel1 is always 45% of Jframe, Jpanel 2 also. Jframe is always in fullsize. Now i wana make the picture to fit those 45%. It's ;ike this: I want to display 2 pictures at the same time as big as possible with some controls under it (buttons) and thats it - Pirate

1 Answers

2
votes

You can use Darryl's Shrink Icon. This is an Icon you can add to a JLabel and then add the JLabel to your JPanel.

The Shrink Icon works such that:

  1. if there is not enough space to display the image then it will shrink the image
  2. if there is enough space, then the image is centered in the space available.

We don't hardcode space allocation for components. We let each component take the space it needs. So for your main layout you would use the BorderLayout of the frame.

The you would use:

JPanel controls = new JPanel();
controls.add(...);
frame.add(controls, BorderLayout.PAGE_END);

Then you would create a second panel for your images:

JPanel images = new JPanel( new GridLayout(0, 1) );
images.add( new JLabel(...) );
images.add( new JLabel(...) );
frame.add(images, BorderLayout.CENTER)

Now the image panel will grow/shrink as the size of the frame changes and the labels will also grow/shrink depending on the space available.