GridBagLayout: window resizing problem

S

Stéphanie Vanhove

Hello,

I have a JPanel on which I have a square drawing. Now I added this panel to
a gridbaglayout, along with some buttons and a list.
Now the problem is, when I test the program and I drag the window to resize
it, the panel completely disappears as soon as there 's not enough space for
it's preferred size. When I make the window larger again, the panel
reappears.
As it is a project for school, i have to be able to make the window smaller
and still show the panel.

Now, somebody told me to set gbc.fill = GridBagConstraints.BOTH;
And indeed, this worked!
But if I use this, when I enlarge the window, the drawing is not centered
anymore. Even if I use gbc.anchor = GridBagConstraints.CENTER;
This is quite obvious, as the panel is filling the whole area, while the
drawing begins at (0,0) so it seems as if the drawing is always in the upper
left corner.
So I don't really like this solution because of this.

I want to be able to resize the window, without the panel disappearing, but
I also want the drawing in the panel to appear in the center at all times
....
Can anyone please help, because I really don't know anymore!
Thanks already !
 
A

Andrew Thompson

I have a JPanel

A better group..
..on which I have a square drawing. Now I added this panel to
a gridbaglayout,

Oh-oh! Are you sure you need the layout that has driven
so many people stark raving mad?
..along with some buttons and a list.
Now the problem is, when I test the program and I drag the window to resize
it, the panel completely disappears as soon as there 's not enough space for
it's preferred size. When I make the window larger again, the panel
reappears.

I don't see it, not in the description you provided, nor the
code you didn't post. I suggest when you go over to c.l.j.gui
that you let the well commented code do the talking.

Read the following article carefully for tips on an example.
As it is a project for school, i have to be able to make the window smaller
and still show the panel.

It seems you would have to do that for your end users as
a courtesy, in any case. ;-)
...
Can anyone please help, because I really don't know anymore!

If you have not worked through the Sun layout tutorial yet,
I'd recommend it. Using a nested layout, you might find a
much better solution* than using a GBL. (* I am still not
entirely clear what you want. )

An useful resource you should check before doing anything though.
<http://www.cs.uu.nl/wais/html/na-dir/computer-lang/java/gui/faq.html>
Particurly section 9.1, for the links.

HTH
 
A

A. Bolmarcich

Hello,

I have a JPanel on which I have a square drawing. Now I added this panel to
a gridbaglayout, along with some buttons and a list.
Now the problem is, when I test the program and I drag the window to resize
it, the panel completely disappears as soon as there 's not enough space for
it's preferred size. When I make the window larger again, the panel
reappears.

Set the minimum size of the panel to an acceptable value. When the size
of a container using GridBagLayout is not larger enough to set each
component to its preferred size, GridBagLayout uses the minimum size of
the components instead of the preferred size.
As it is a project for school, i have to be able to make the window smaller
and still show the panel.

Now, somebody told me to set gbc.fill = GridBagConstraints.BOTH;
And indeed, this worked!
But if I use this, when I enlarge the window, the drawing is not centered
anymore. Even if I use gbc.anchor = GridBagConstraints.CENTER;
This is quite obvious, as the panel is filling the whole area, while the
drawing begins at (0,0) so it seems as if the drawing is always in the upper
left corner.
So I don't really like this solution because of this.

I want to be able to resize the window, without the panel disappearing, but
I also want the drawing in the panel to appear in the center at all times
...
Can anyone please help, because I really don't know anymore!
Thanks already !

With GridBagLayout the position of a component depends on the preferred
size, minimum size, and layout constraints of each component. You can
center a component in the container if the other components cooperate.
Without knowing where you want all of the components to be in the
container, it is not possible to give you detailed help.
 
S

Stéphanie Vanhove

Thank you very much! My problem is solved!!!
All I needed to do was set the minimum size to the same value as the
preferred size, instead of zero. It works now :)
 
F

Filip Larsen

Stéphanie Vanhove wrote
I have a JPanel on which I have a square drawing. Now I added this panel to
a gridbaglayout, along with some buttons and a list.
[...]
I want to be able to resize the window, without the panel disappearing, but
I also want the drawing in the panel to appear in the center at all
times

You don't specify how your JPanel draws that square, but if you do it
from your own code, you can change that code to support drawing the
square centered. If the square has a fixed size independent of the
JPanel size, you should offset the drawing
Math.max(0,(panel.width-square.width)/2) in x and similar in y.


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top