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.