Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get width of content pane?

160 views
Skip to first unread message

Ronald Fischer

unread,
Mar 11, 2002, 8:29:52 AM3/11/02
to
I have a JFrame, w, and a JPanel, p. I would like to add p to w and
give p a size as follows:

- p's width should be exactly the width of the content pane of w
- p's height should be a specific number, h.

I tried this:

Dimension dim = w.getContentPane().getSize();
dim.height = h;
p.setSize(dim);
w.add(p);

This does not work, though: Surprisingly (to me, at least), getSize()
always returns (0,0). Could someone explain to me, why this is so,
and how to solve my problem?

Ronald
--
To reduce spam in my inbox, the address given in the Reply-To: header is
not guaranteed to live longer than 1 month after the article was
posted. My permanent address is (after deleting the XXX):
Ronald Otto Valentin Fischer <rov...@operamail.com>

Jason Teagle

unread,
Mar 11, 2002, 10:17:38 AM3/11/02
to

"Ronald Fischer" <ro...@feriasonline.pt.invalid> wrote in message
news:m2hennd...@karfiol.glp.de...

> I have a JFrame, w, and a JPanel, p. I would like to add p to w and
> give p a size as follows:
>

> Dimension dim = w.getContentPane().getSize();
> dim.height = h;
> p.setSize(dim);
> w.add(p);
>
> This does not work, though: Surprisingly (to me, at least), getSize()
> always returns (0,0). Could someone explain to me, why this is so,
> and how to solve my problem?

I don't know if things have changed since I was into this, but I hit this
problem and it is very annoying. I believe you get (0, 0) because you call
getSize() while the frame is not visible. If you make it visible before
calling it, I believe it might then work.

Perhaps you could add the panel with an arbitrary size to begin with, and in
your frame's resize() (sp?) method (which hopefully will get called when the
frame is first shown as well as if the user resizes it), you can then adjust
the panel accordingly, since the frame must now be visible.


--
--------------------------------------------
_ _
o o Jason Teagle
< ja...@teagster.co.uk
v
--------------------------------------------


Babu Kalakrishnan

unread,
Mar 11, 2002, 9:53:53 AM3/11/02
to
On 11 Mar 2002 14:29:52 +0100, Ronald Fischer <ro...@feriasonline.pt.invalid> wrote:
>I have a JFrame, w, and a JPanel, p. I would like to add p to w and
>give p a size as follows:
>
>- p's width should be exactly the width of the content pane of w
>- p's height should be a specific number, h.
>
>I tried this:
>
>Dimension dim = w.getContentPane().getSize();
>dim.height = h;
>p.setSize(dim);
>w.add(p);
>
>This does not work, though: Surprisingly (to me, at least), getSize()
>always returns (0,0). Could someone explain to me, why this is so,
>and how to solve my problem?

Several problems :

1) You shouldn't add anything to the JFrame directly - add stuff to its
contentPane.

2) The size of any component is determined by the layoutManager that is
used by the parent container. Unless this layoutManager is null, there
is absolutely no point trying to call setSize on a component. The size
you gave it will last only till the time the parent container performs
a layout of its children.

3) getSize() returns reliable results only for components that have been
realized on screen. That's the reason you get (0,0) when you call
getSize from the constructor.

To the solution for your problem - assuming that you want only "p" to be
visible on the frame and the remainder of the frame below p to remain
blank.

Container contentPane = getContentPane();
p.setPreferredSize(0,h);
contentPane.add(p,BorderLayout.NORTH);
Component filler = box.createGlue();
contentPane.add(filler,BorderLayout.CENTER);


BK

0 new messages