Java JPanel Layout layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel, JComponent tComp, JLabel rLabel, JComponent rComp)

Here you can find the source of layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel, JComponent tComp, JLabel rLabel, JComponent rComp)

Description

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.

License

Open Source License

Parameter

Parameter Description
bean the panel to add components to
labels an array of text labels
components an array of components
JLabel register label
JComponent register field

Declaration

public static void layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel,
        JComponent tComp, JLabel rLabel, JComponent rComp) 

Method Source Code

//package com.java2s;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JComponent;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {
    /**//from  w w  w  .j  a v a 2  s .  c o m
     * 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.
     * 
     * @param bean the panel to add components to
     * @param labels an array of text labels
     * @param components an array of components
     * @param JLabel total label
     * @param JComponent total field
     * @param JLabel register label
     * @param JComponent register field
     */
    public static void layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel,
            JComponent tComp, JLabel rLabel, JComponent rComp) {
        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++;
        }

        JLabel[] labels1 = new JLabel[rows];
        JComponent[] components1 = new JComponent[rows];
        JLabel[] labels2 = new JLabel[rows];
        JComponent[] components2 = new JComponent[rows];

        System.arraycopy(labels, 0, labels1, 0, rows);
        System.arraycopy(components, 0, components1, 0, rows);

        System.arraycopy(labels, rows, labels2, 0, entries - rows);
        System.arraycopy(components, rows, components2, 0, entries - rows);

        bean.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(5, 0, 5, 0);
        constraints.gridheight = 1;

        //add the first column of labels + components
        for (int i = 0; i < labels1.length; i++) {
            if (labels1[i] != null) {
                constraints.gridx = 0;
                constraints.gridy = i;
                constraints.fill = GridBagConstraints.HORIZONTAL;
                bean.add(labels1[i], constraints);
            }
            if (components1[i] != null) {
                constraints.gridx = 1;
                constraints.gridy = i;
                bean.add(components1[i], constraints);
            }
        }

        int j;
        //add the second column of labels + components
        for (j = 0; j < labels2.length; j++) {
            if (labels2[j] != null) {
                constraints.gridx = 2;
                constraints.gridy = j;
                constraints.fill = GridBagConstraints.HORIZONTAL;
                bean.add(labels2[j], constraints);
            }
            if (components2[j] != null) {
                constraints.gridx = 3;
                constraints.gridy = j;
                constraints.fill = GridBagConstraints.NONE;
                bean.add(components2[j], constraints);
            }
        }

        constraints.insets = new Insets(10, 5, 10, 5);
        if (tLabel != null && rLabel == null) {
            constraints.gridx = 2;
            constraints.gridy = j;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            bean.add(tLabel, constraints);
            constraints.gridx = 3;
            constraints.fill = GridBagConstraints.NONE;
            bean.add(tComp, constraints);
        } else {
            constraints.gridx = 0;
            constraints.gridy = j++;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            bean.add(tLabel, constraints);
            constraints.gridx = 1;
            constraints.fill = GridBagConstraints.NONE;
            bean.add(tComp, constraints);

            constraints.gridx = 2;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            bean.add(rLabel, constraints);
            constraints.gridx = 3;
            constraints.fill = GridBagConstraints.NONE;
            bean.add(rComp, constraints);
        }
    }
}

Related

  1. constrain(Component component, JPanel panel, GridBagLayout layout, GridBagConstraints constraints, int gridx, int gridwidth, int weightx, int gridy, int gridheight, int weighty, int fill, int anchor)
  2. doLayout(JPanel parentContainer, Component[] components, int numberOfColumns, double[] weightsX, double[] weightsY)
  3. formGridInColumn(JPanel panel, int rows, int cols)
  4. getJPanel(LayoutManager man)
  5. makeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
  6. setBoxXLayout(JPanel p)
  7. setGBLayout(JPanel panel, int[] columnWidths, int[] rowHeights, double[] columnWeights, double[] rowWeights)
  8. setLayoutForEditingIcons(JPanel parentPanel, JLabel icon)