8 Replies - 286 Views - Last Post: 28 August 2012 - 06:21 PM Rate Topic: -----

#1 oha055  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 229
  • Joined: 02-February 09

Resizing JPanel on runtime

Posted 28 August 2012 - 01:13 PM

So, I set preferredSize() of the content pane (jpanel) when I first start my application. But, at runtime the user is able to open and view an image in the content pane (painted onto the content pane using drawImage()), how should I go about resizing the JPanel, so that the application resize to fit the current image? setPreferredSize

tried this, but nothing happened:
setPreferredSize(new Dimension(img.getWidth, img.getHeight())); // when image is added to the jpanel



Tried calling repaint() too, nothing happened.

Thanks guys :)

This post has been edited by oha055: 28 August 2012 - 01:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Resizing JPanel on runtime

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 01:29 PM

We'll need more details
In which type of layout is your JPanel ? And in any case the size of the JPanel could be larger than the JFrame that contains it, unless it is in a JScrollPane
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,204
  • Joined: 05-April 11

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 01:30 PM

It depends what you want your frame to look like.
Do you want it to pack around the JPanel, then simply call pack() on your frame
Was This Post Helpful? 1
  • +
  • -

#4 oha055  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 229
  • Joined: 02-February 09

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 01:38 PM

View PostCasiOo, on 28 August 2012 - 10:30 PM, said:

It depends what you want your frame to look like.
Do you want it to pack around the JPanel, then simply call pack() on your frame


pack() fixed it. Thanks mate! :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 01:40 PM

View PostCasiOo, on 28 August 2012 - 04:30 PM, said:

then simply call pack() on your frame

NMot a good idea if you overload the paintComponent() method to do your own drawing of an Image as it is probably the case here.

pack() is good when your JPanel contains JComponents. It can evalauate the size of each one and act accordingly. If you do your own drawing it cannot calculate the size of what you will be drawing.
Was This Post Helpful? 0
  • +
  • -

#6 oha055  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 229
  • Joined: 02-February 09

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 01:48 PM

View Postpbl, on 28 August 2012 - 10:40 PM, said:

View PostCasiOo, on 28 August 2012 - 04:30 PM, said:

then simply call pack() on your frame

NMot a good idea if you overload the paintComponent() method to do your own drawing of an Image as it is probably the case here.

pack() is good when your JPanel contains JComponents. It can evalauate the size of each one and act accordingly. If you do your own drawing it cannot calculate the size of what you will be drawing.


Thanks for the heads up! :) So, what would be an alternate solution to this? I'd really like to do this the right way from the very start.

This is the panel I'm drawing the image in. It is also the class im using as the content pane
package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ImagePanel extends JPanel implements Runnable {

	private static final Dimension FRAME_DIM = new Dimension(250, 200);

	private BufferedImage image;
	private boolean isUpdating;

	public ImagePanel() {
		image = null;
		isUpdating = true;
		setPreferredSize(FRAME_DIM);
		new Thread(this).start();
	}

	@Override
	public void run() {
		while(isUpdating) {
			repaint();
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(image, 0, 0, null);
	}

	public void setImage(BufferedImage bi) {
		image = bi;
		setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
	}
}


This post has been edited by oha055: 28 August 2012 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,204
  • Joined: 05-April 11

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 01:59 PM

View Postpbl, on 28 August 2012 - 08:40 PM, said:

View PostCasiOo, on 28 August 2012 - 04:30 PM, said:

then simply call pack() on your frame

NMot a good idea if you overload the paintComponent() method to do your own drawing of an Image as it is probably the case here.

pack() is good when your JPanel contains JComponents. It can evalauate the size of each one and act accordingly. If you do your own drawing it cannot calculate the size of what you will be drawing.


He is changing the size of the JPanel to the size of the image, so the frame will pack nicely around the image.

And you could set the icon of a JLabel to your image if all you want to do is simply show an image
Was This Post Helpful? 1
  • +
  • -

#8 oha055  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 229
  • Joined: 02-February 09

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 02:03 PM

Yes, I know about the JLabel-way. However I will eventually start drawing lines etc. onto the bufferedimage :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: Resizing JPanel on runtime

Posted 28 August 2012 - 06:21 PM

View PostCasiOo, on 28 August 2012 - 04:59 PM, said:

He is changing the size of the JPanel to the size of the image, so the frame will pack nicely around the image.

Yes but you will have to call back pack() again every time the image is changed and the setPreferredSize() is called
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1