Reputation: 175
Java Swing:
I've created a method to return a GridBagConstraints so that I don't need to constantly call new GridBagConstraints(); and set a bunch of variables. It works like such:
displayPanel.add(labelPanel, createGBC(0, 0, 2);
displayPanel.add(nosePanel, createGBC(1, 0, 3);
displayPanel.add(mainPanel, createGBC(2, 0, 3);
etc..
And the code for my createGBC:
private GridBagConstraints createGBC(int x, int y, int z) {
gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.EAST : GridBagConstraints.WEST;
if (z == 0) gbc.insets = new Insets(0, 0, 0, 0);
else if (z == 1) gbc.insets = new Insets(8, 0, 0, 0);
else if (z == 2) gbc.insets = new Insets(4, 4, 0, 4);
else if (z == 3) gbc.insets = new Insets(0, 2, 0, 2);
else if (z == 4) gbc.insets = new Insets(0, 0, 16, 0);
else if (z == 5) gbc.insets = new Insets(6, 0, 16, 0);
return gbc;
}
My question is: Is there a better way to deal with many different insets than simply doing a ton of else if statements? A couple issues arise with my current method.
I'm starting to lose track of which insets are assigned to which value of z. (I'm trying to refactor to make it more readable/reusable).
I actually may have to add even more inset presets which will compound issue 1.
Upvotes: 2
Views: 2089
Reputation: 44414
As MadProgrammer mentioned, you don't need a new GridBagConstraints object each time, because GridBagLayout clones the constraints for each added component.
Normally, I would suggest that you replace your int value (z
) with enum constants, and store your Insets objects as values in an EnumMap. But in your case, there's an easier solution:
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(4, 4, 0, 4);
displayPanel.add(labelPanel, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 2, 0, 2);
displayPanel.add(nosePanel, gbc);
displayPanel.add(mainPanel, gbc);
Notice that I didn't set gridx, gridy, gridwidth or gridheight at all. gridwidth and gridheight are already 1 by default. gridx and gridy default to GridBagConstraints.RELATIVE, which happens to do exactly what you want: it automatically adds each new component on the same row, unless gridwidth or gridheight is changed to REMAINDER, in which case a new row or column (respectively) will be started for the next relatively placed component.
Upvotes: 1
Reputation: 347314
When you add a component to a GridBagLayout
, GridBagLayout
will make a copy of the constraints, this means that you don't need to create a new instance EVERY time you want to make a small change, for example...
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(0, 0, 0, 0);
add(labelPanel, gbc);
gbc.gridx = 1;
gbc.insets = new Insets(0, 2, 0, 2);
add(nosePane, gbc);
gbc.gridx = 2;
add(mainPanel, gbc);
This means that you only need to change those attributes you need to and can continue to reuse the basic constraints you setup previously.
When the amount of attributes you need to change/reset becomes to large (or you can't remember or the attributes you need to change), then you could create a new instance of the constraints.
I tend to use a single instance this way for "groups" of components.
If, in your case, you want to re-use the Insets
, then maybe you could create a series of constants and use those, which would make your code easier to read and maintain
Upvotes: 2