Re: GridBagLayout and changing constraints objects.



Daniel Pitts <googlegroupie@xxxxxxxxxxxxx> wrote:

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?

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.

--
Chris Smith
.



Relevant Pages

  • Re: GridBagLayout question
    ... > It is best to get the information direct from Sun ... GridBagConstraints constraints = new GridBagConstraints; ... > For the lightweight entry point to the Sun Java 1.5 JavaDocs ...
    (comp.lang.java.help)
  • Re: GridBagLayout and changing constraints objects.
    ... JPanel myPanel = new JPanel); ... GridBagConstraints constaints = getConstraints; ... and re-add it with the new constraints? ...
    (comp.lang.java.gui)
  • Re: GridBagLayout and changing constraints objects.
    ... JPanel myPanel = new JPanel); ... GridBagConstraints constaints = getConstraints; ... and re-add it with the new constraints? ...
    (comp.lang.java.gui)