Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2007

Tip: Looking for answers? Try searching our database.

GridBagLayout and changing constraints objects.

Thread view: 
Daniel Pitts - 25 Mar 2007 23:07 GMT
Say I have the following code:

JPanel myPanel = new JPanel(new GridBagLayout());
GridBagConstraints constaints = getConstraints();
myPanel.add(new JLabel("Hello world"), constaints);

And then later (as a result of an Event), I do something like
constraints.gridx = 3;
myPanel.revalidate();

Will this have the expected effect, or do I need to remove the old
label, and re-add it with the new constraints?
Chris Smith - 25 Mar 2007 23:32 GMT
> Say I have the following code:
>
[quoted text clipped - 8 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

The latter.  The GridBagLayout class makes a copy of the layout
constraints when the device is added.  In fact, it's fairly common to
create just one GridBagConstraints object, and use it to add a large
number of components by just changing the few properties that differ
each time.

Signature

Chris Smith

Knute Johnson - 25 Mar 2007 23:39 GMT
> Say I have the following code:
>
> JPanel myPanel = new JPanel(new GridBagLayout());
> GridBagConstraints constaints = getConstraints();

What class does the method getConstraints() belong to?

> myPanel.add(new JLabel("Hello world"), constaints);
>
[quoted text clipped - 4 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

No.  You will have to re-add it.

Signature

Knute Johnson
email s/nospam/knute/

Tom Hawtin - 25 Mar 2007 23:57 GMT
> Say I have the following code:
>
[quoted text clipped - 8 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

It wont have any affect. The constraints object is cloned. Indeed it is
normal to use one constraints object to add many components.

To change the constraints for a component, just call
LayoutManager[2].addLayoutComponent.

Tom Hawtin
Daniel Pitts - 26 Mar 2007 01:00 GMT
> > Say I have the following code:
>
[quoted text clipped - 16 lines]
>
> Tom Hawtin

Thanks to everyone who replied.
I should have RTFM. Actually, its RTFA, isn't it?

GridBagLayout.setConstraints is what I need to use. :-)

Cheers,
Daniel.
SadRed - 26 Mar 2007 01:14 GMT
On Mar 26, 7:07 am, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> Say I have the following code:
>
[quoted text clipped - 8 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

You could try it yourself.
Daniel Pitts - 26 Mar 2007 02:46 GMT
> On Mar 26, 7:07 am, "Daniel Pitts" <googlegrou...@coloraura.com>
> wrote:
[quoted text clipped - 13 lines]
>
> You could try it yourself.

I could, but that might have only proved that sometimes it works. I
was asking if it would always work...  As I discovered, it won't ever
work, but there is a different way to do it that will always work.
SadRed - 26 Mar 2007 06:19 GMT
On Mar 26, 7:07 am, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> Say I have the following code:
>
[quoted text clipped - 8 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

Why don't you try it yourself?
Brandon McCombs - 26 Mar 2007 06:28 GMT
> Say I have the following code:
>
[quoted text clipped - 8 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

You will have to remove it and re-add it. How is Java to know which
Object is supposed to have the new constraint if you don't re-add the
Object with that constraint? It could assume to apply that new
constraint property to the only Object in a JPanel if there is only one
in it but that wouldn't work for more than 1 Object in container and it
isn't a good way to do it anyway. It is bad design to need to
relocate/resize an Object after an event occurs. You should rethink how
you are presenting the interface to the user because the user should not
normally see new things appear when they do something; all the Objects
in the GUI should be visible, their sizes unchanging, and the unused
ones disabled until they are needed (with the enabling trigger being
your event).

As it is now, the line:
myPanel.add(new JLabel("Hello world"), constaints);

isn't going to do much because you aren't defining what your constraints
are although some of the properties do have default values. The default
for gridx is 'relative' which may not be what you want initially.
Daniel Pitts - 26 Mar 2007 07:41 GMT
> > Say I have the following code:
>
[quoted text clipped - 21 lines]
> ones disabled until they are needed (with the enabling trigger being
> your event).
Actually, I have four objects which would only trade places, and they
are similar enough that the change shouldn't bother users.  Have you
ever played a game on Yahoo games?  notice when you sit at a table,
your side becomes the southern most side.   I'm basically emulating
that effect.

It is easier to move the components around than to change which model
each component is looking at.  Either way, the effect would be the
same.

> As it is now, the line:
> myPanel.add(new JLabel("Hello world"), constaints);
>
> isn't going to do much because you aren't defining what your constraints
> are although some of the properties do have default values. The default
> for gridx is 'relative' which may not be what you want initially.

Hmm, I don't see how you think that my constraints object is unset. I
simply didn't copy the code that implemented getConstraints();
Ian Wilson - 26 Mar 2007 10:46 GMT
> Say I have the following code:

OK, I shall ...

> JPanel myPanel = new JPanel(new GridBagLayout());
> GridBagConstraints constaints = getConstraints();
[quoted text clipped - 6 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

Others have given the answer you were seeking. However I can't help
noticing that the code you quoted can never have the expected effect
since constaints != constraints :-)
Daniel Pitts - 26 Mar 2007 17:53 GMT
> > Say I have the following code:
>
[quoted text clipped - 14 lines]
> noticing that the code you quoted can never have the expected effect
> since constaints != constraints :-)

Did I surround that code with an sscce tag? I don't think so. :-)
Larry Barowski - 26 Mar 2007 15:20 GMT
> Say I have the following code:
>
[quoted text clipped - 8 lines]
> Will this have the expected effect, or do I need to remove the old
> label, and re-add it with the new constraints?

It will have no effect. Normally you change the (single) constraints
object between adding components. There is no need to create
multiple constraints objects. To change after creation, I assume you
can use GridBagLayout.setConstraints(). You could use
GridBagLayout.getConstraints(), make the change, then use
setConstraints() to avoid storing the constraints object.
Larry Barowski - 26 Mar 2007 15:32 GMT
Oops, the OP was to cljp and cljg with followups to cljg, so
I didn't see the answers already here.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.