• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reuse of GridBagConstraints

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun recommands not to reuse the GridBagConstraints object as this can very easily lead to introduce subtle bugs if we forget to reset the fields for each new instance.

To overcome the problem of missing to reset any of the fields of GridBagConstraints, I have written a method as below

private GridBagConstraints gbc =null;

private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth,
int gridheight, double weightx, double weighty, int anchor,
int fill, Insets insets, int ipadx, int ipady) {
if (gbc == null) {
gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
} else {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.anchor = anchor;
gbc.fill = fill;
gbc.insets = insets;
gbc.ipadx = ipadx;
gbc.ipady = ipady;
}
return gbc;
}

Whether this code is good for reuse the GridBagConstraints objects and to overcome the problem of missing to reset any of the fields.
I plan to use this code as part of Framework API in my project.
Please provide me the drawback of the above code if any.

Regards
Ganni
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganni Kal wrote:Sun recommands not to reuse the GridBagConstraints object as this can very easily lead to introduce subtle bugs if we forget to reset the fields for each new instance.

To overcome the problem of missing to reset any of the fields of GridBagConstraints, I have written a method as below

private GridBagConstraints gbc =null;

private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth,
int gridheight, double weightx, double weighty, int anchor,
int fill, Insets insets, int ipadx, int ipady) {
if (gbc == null) {
gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
} else {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.anchor = anchor;
gbc.fill = fill;
gbc.insets = insets;
gbc.ipadx = ipadx;
gbc.ipady = ipady;
}
return gbc;
}

Whether this code is good for reuse the GridBagConstraints objects and to overcome the problem of missing to reset any of the fields.
I plan to use this code as part of Framework API in my project.
Please provide me the drawback of the above code if any.



Any advantage of using this code over just invoking the 11-arg constructor is pretty slim. Yes it instantiates fewer objects, but they would get reclaimed quickly by the garbage collector anyway. (GridBayLayout keeps references to clones of the constraint objects, not to the originals.)

 
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cai Horstmann has a class which helps a lot with GridBag.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic