import java.awt.*; /** * Utility to facilitate usage of GridBagLayout layout manager. *
* In particular, this utility makes it easier to handle the * GridBagConstraints object, to the point of completely hiding it if * so desired. *
* It will return the constructed GridBagConstraints object (actually * the GBC subclass) so it can be reused. *
* The GBC class has member setters that all return the GBC object, so * setting of several fields can be "chained" together, allowing more * compact code without physically placing multiple statements on the * same line. *
* Here is a basic example of common usage of GBLTool: *
* GBLTool.GBC gbc = * GBLTool.add(fieldsPane, _userIdLabel, * GBLTool.GBC().fill(GridBagConstraints.NONE). * anchor(GridBagConstraints.WEST). * insets(new Insets(0, 0, 10, 0))); * * GBLTool.add(fieldsPane, _userIdField, * gbc.fill(GridBagConstraints.HORIZONTAL). * weightx(1.0).insets(new Insets(0, 0, 10, 0))); * * GBLTool.add(fieldsPane, _passwordLabel, * gbc.weightx(0.0).gridx(0).gridy(1)); * * GBLTool.add(fieldsPane, _passwordField, * gbc.fill(GridBagConstraints.HORIZONTAL). * weightx(1.0).gridx(1)); ** * @see GridBagConstraints * @see java.awt.GridBagLayout */ public class GBLTool { /** * Adds a component to the specified container, using specified * constraints. It internally constructs the GBC (derived from * GridBagConstraints) and returns the GBC when done. * * @param container Container to add component to * @param component Component to add to container, with constraints * @param gridx X-coordinate of component * (see {@link GridBagConstraints#gridx}) * (see {@link HelpTool#init()}) * @param gridy Y-coordinate of component * (see {@link java.awt.GridBagConstraints#gridy GridBagConstraints.gridy}) * @param gridwidth number of cells in row used for component * (see {@link java.awt.GridBagConstraints#gridwidth * GridBagConstraints.gridwidth}) * @param gridheight number of cells in column used for component * (see {@link java.awt.GridBagConstraints#gridheight * GridBagConstraints.gridheight}) * @param weightx X direction component weight * (see {@link java.awt.GridBagConstraints#weightx * GridBagConstraints.weightx}) * @param weighty Y direction component weight * (see {@link java.awt.GridBagConstraints#weighty * GridBagConstraints.weighty}) * @param anchor where to place component when it is smaller than * its display area * (see {@link java.awt.GridBagConstraints#anchor * GridBagConstraints.anchor}) * @param fill how to resize component when component display area * is larger than component's requested size (see {@link * java.awt.GridBagConstraints#fill GridBagConstraints.fill}) * @param insets specifies external padding of component (see {@link * java.awt.GridBagConstraints#insets GridBagConstraints.insets}) * @param ipadx specifies internal width padding of component (see * {@link java.awt.GridBagConstraints#ipadx GridBagConstraints.ipadx}) * @param ipady specifies internal height padding of component (see * {@link java.awt.GridBagConstraints#ipady GridBagConstraints.ipady}) */ public static GBC add(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady) { GBC gbc = GBC(new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady)); GridBagLayout gbl = (GridBagLayout) container.getLayout(); gbl.setConstraints(component, gbc); container.add(component); return (gbc); } /** * Adds all of an array of components to the specified container, * using specified constraints. It internally constructs the GBC * (derived from {@link java.awt.GridBagConstraints}) and returns * the GBC when done. * * @param container Container to add component to * @param components Array of components to add to container, with * constraints * @param gridx X-coordinate of component * (see {@link java.awt.GridBagConstraints#gridx GridBagConstraints.gridx}) * @param gridy Y-coordinate of component * (see {@link java.awt.GridBagConstraints#gridy GridBagConstraints.gridy}) * @param gridwidth number of cells in row used for component * (see {@link java.awt.GridBagConstraints#gridwidth * GridBagConstraints.gridwidth}) * @param gridheight number of cells in column used for component * (see {@link java.awt.GridBagConstraints#gridheight * GridBagConstraints.gridheight}) * @param weightx X direction component weight * (see {@link java.awt.GridBagConstraints#weightx * GridBagConstraints.weightx}) * @param weighty Y direction component weight * (see {@link java.awt.GridBagConstraints#weighty * GridBagConstraints.weighty}) * @param anchor where to place component when it is smaller than * its display area * (see {@link java.awt.GridBagConstraints#anchor * GridBagConstraints.anchor}) * @param fill how to resize component when component display area * is larger than component's requested size (see {@link * java.awt.GridBagConstraints#fill GridBagConstraints.fill}) * @param insets specifies external padding of component (see {@link * java.awt.GridBagConstraints#insets GridBagConstraints.insets}) * @param ipadx specifies internal width padding of component (see * {@link java.awt.GridBagConstraints#ipadx GridBagConstraints.ipadx}) * @param ipady specifies internal height padding of component (see * {@link java.awt.GridBagConstraints#ipady GridBagConstraints.ipady}) */ public static GBC add(Container container, Component[] components, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady) { GBC gbc = GBC(new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady)); GridBagLayout gbl = (GridBagLayout) container.getLayout(); for (int ctr = 0; ctr < components.length; ++ ctr) { gbl.setConstraints(components[ctr], gbc); container.add(components[ctr]); } return (gbc); } /** * Adds a component to the specified container, using specified * {@link GBC} object. It internally constructs the {@link GBC} * and returns the {@link GBC} when done. * * @param container Container to add component to * @param component Component to add to container, with constraints * @param gbc {@link GBC} object to add component with */ public static GBC add(Container container, Component component, GBC gbc) { GridBagLayout gbl = (GridBagLayout) container.getLayout(); gbl.setConstraints(component, gbc); container.add(component); return (gbc); } /** * Adds all of an array of component to the specified container, * using specified {@link GBC} object. It internally constructs * the {@link GBC} and returns the {@link GBC} when done. * * @param container Container to add component to * @param components Array of components to add to container, with * constraints * @param gbc {@link GBC} object to add component with */ public static GBC add(Container container, Component[] components, GBC gbc) { GridBagLayout gbl = (GridBagLayout) container.getLayout(); for (int ctr = 0; ctr < components.length; ++ ctr) { gbl.setConstraints(components[ctr], gbc); container.add(components[ctr]); } return (gbc); } /** * Creates new GBC object, facilitating compact code. */ public static GBC GBC() { return (new GBC()); } /** * Creates new GBC object, templated off of {@link * GridBagConstraints} object. */ public static GBC GBC(GridBagConstraints gbc) { GBC newGbc = new GBC(); newGbc.gridx = gbc.gridx; newGbc.gridy = gbc.gridy; newGbc.gridwidth = gbc.gridwidth; newGbc.gridheight = gbc.gridheight; newGbc.weightx = gbc.weightx; newGbc.weighty = gbc.weighty; newGbc.anchor = gbc.anchor; newGbc.fill = gbc.fill; newGbc.insets = gbc.insets; newGbc.ipadx = gbc.ipadx; newGbc.ipady = gbc.ipady; return (newGbc); } /** * Subclass of GridBagConstraints which provides chained mutator * methods. */ public static class GBC extends GridBagConstraints { public GBC gridx(int newGridx) { gridx = newGridx; return (this); } public GBC gridy(int newGridy) { gridy = newGridy; return (this); } public GBC gridwidth(int newGridwidth) { gridwidth = newGridwidth; return (this); } public GBC gridheight(int newGridheight) { gridheight = newGridheight; return (this); } public GBC weightx(double newWeightx) { weightx = newWeightx; return (this); } public GBC weighty(double newWeighty) { weighty = newWeighty; return (this); } public GBC anchor(int newAnchor) { anchor = newAnchor; return (this); } public GBC fill(int newFill) { fill = newFill; return (this); } public GBC insets(Insets newInsets) { insets = newInsets; return (this); } public GBC ipadx(int newIpadx) { ipadx = newIpadx; return (this); } public GBC ipady(int newIpady) { ipady = newIpady; return (this); } } }