| Author |
Reuse of GridBagConstraints
|
Ganni Kal
Greenhorn
Joined: Jan 17, 2008
Posts: 16
|
|
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
Joined: Sep 20, 2005
Posts: 852
|
|
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.)
|
bitguru blog
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
Cai Horstmann has a class which helps a lot with GridBag.
|
 |
 |
|
|
subject: Reuse of GridBagConstraints
|
|
|