This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reuse of GridBagConstraints

 
Ganni Kal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • 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.)

 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cai Horstmann has a class which helps a lot with GridBag.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic