Java Utililty Methods JPanel Layout

List of utility methods to do JPanel Layout

Description

The list of methods to do JPanel Layout are organized into topic(s).

Method

voidconstrain(Component component, JPanel panel, GridBagLayout layout, GridBagConstraints constraints, int gridx, int gridwidth, int weightx, int gridy, int gridheight, int weighty, int fill, int anchor)
Adds the component and sets its grid bag constraints.
panel.add(component);
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.fill = fill;
constraints.anchor = anchor;
constraints.weightx = weightx;
...
JPaneldoLayout(JPanel parentContainer, Component[] components, int numberOfColumns, double[] weightsX, double[] weightsY)
This does a column oriented grid bag layout.
return doLayout(parentContainer, components, numberOfColumns, weightsX, weightsY, null, null, null);
voidformGridInColumn(JPanel panel, int rows, int cols)
Form panel with SpringLayout to grid.
if (panel == null || panel.getLayout().getClass() != SpringLayout.class) {
    throw new IllegalArgumentException("Invalid input for panel.");
SpringLayout layout = (SpringLayout) panel.getLayout();
Spring x = Spring.constant(3);
for (int col = 0; col < cols; col++) {
    Spring width = Spring.constant(0);
    for (int row = 0; row < rows; row++) {
...
JPanelgetJPanel(LayoutManager man)
get J Panel
JPanel panel = new JPanel(man);
panel.setBackground(BACKGROUND);
return panel;
voidlayoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel, JComponent tComp, JLabel rLabel, JComponent rComp)
This method lays out dual columns of labels and input fields if the number of rows displayed on the screen would exceed the vertical area available.
if (labels.length != components.length) {
    throw new IllegalArgumentException("Length of labels and components args not equal.");
int entries = labels.length;
int rows = entries / 2;
boolean thatsOdd = entries % 2 == 1;
if (thatsOdd) {
    rows++;
...
voidmakeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
Taken verabatim from java.sun.com online tutorial for using the SpringLayout to make a grid
Spring x = Spring.constant(initialX);
for (int c = 0; c < cols; c++) {
    Spring width = Spring.constant(0);
    for (int r = 0; r < rows; r++) {
        width = Spring.max(width,
                layout.getConstraints(jMainGridPanel.getComponent(r * cols + c)).getWidth());
    for (int r = 0; r < rows; r++) {
...
voidsetBoxXLayout(JPanel p)
set Box X Layout
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
GridBagLayoutsetGBLayout(JPanel panel, int[] columnWidths, int[] rowHeights, double[] columnWeights, double[] rowWeights)
set GB Layout
GridBagLayout gbl = new GridBagLayout();
gbl.columnWidths = columnWidths;
gbl.rowHeights = rowHeights;
gbl.columnWeights = columnWeights;
gbl.rowWeights = rowWeights;
panel.setLayout(gbl);
return gbl;
voidsetLayoutForEditingIcons(JPanel parentPanel, JLabel icon)
set Layout For Editing Icons
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
parentPanel.setLayout(gbl);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
...
voidshowCentered(JPanel panel)
show Centered
JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
center(frame);
frame.setVisible(true);