Java JPanel Layout formGridInColumn(JPanel panel, int rows, int cols)

Here you can find the source of formGridInColumn(JPanel panel, int rows, int cols)

Description

Form panel with SpringLayout to grid.

License

Open Source License

Parameter

Parameter Description
sourcesPanel panel to reform
rows number of rows in the panel
cols number of columns in the panel

Declaration

public static void formGridInColumn(JPanel panel, int rows, int cols) 

Method Source Code


//package com.java2s;
//License from project: MIT License 

import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;

public class Main {
    /**/*from   www.  j  a  va 2  s.c  o m*/
     * Form panel with SpringLayout to grid.
     * 
     * @param sourcesPanel panel to reform
     * @param rows number of rows in the panel
     * @param cols number of columns in the panel
     */
    public static void formGridInColumn(JPanel panel, int rows, int cols) {
        if (panel == null || panel.getLayout().getClass() != SpringLayout.class) {
            throw new IllegalArgumentException("Invalid input for panel.");
        }

        SpringLayout layout = (SpringLayout) panel.getLayout();

        // Align all cells in each column and make them the same width.
        Spring x = Spring.constant(3);
        for (int col = 0; col < cols; col++) {
            Spring width = Spring.constant(0);
            for (int row = 0; row < rows; row++) {
                width = Spring.max(width, getConstraintsForCell(row, col, cols, panel).getWidth());
            }
            for (int row = 0; row < rows; row++) {
                SpringLayout.Constraints constraints = getConstraintsForCell(row, col, cols, panel);
                constraints.setX(x);
                constraints.setWidth(width);
            }
            x = Spring.sum(x, Spring.sum(width, Spring.constant(3)));
        }

        // Align all cells in each row and make them the same height.
        Spring y = Spring.constant(3);
        for (int row = 0; row < rows; row++) {
            Spring height = Spring.constant(0);
            for (int col = 0; col < cols; col++) {
                height = Spring.max(height, getConstraintsForCell(row, col, cols, panel).getHeight());
            }
            for (int col = 0; col < cols; col++) {
                SpringLayout.Constraints constraints = getConstraintsForCell(row, col, cols, panel);
                constraints.setY(y);
                constraints.setHeight(height);
            }
            y = Spring.sum(y, Spring.sum(height, Spring.constant(3)));
        }

        // Set panel's size
        SpringLayout.Constraints pCons = layout.getConstraints(panel);
        pCons.setConstraint(SpringLayout.SOUTH, y);
        pCons.setConstraint(SpringLayout.EAST, x);
    }

    private static SpringLayout.Constraints getConstraintsForCell(int row, int col, int totalCols, JPanel panel) {
        SpringLayout layout = (SpringLayout) panel.getLayout();
        Component c = panel.getComponent(row * totalCols + col);
        return layout.getConstraints(c);
    }
}

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. getJPanel(LayoutManager man)
  4. layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel, JComponent tComp, JLabel rLabel, JComponent rComp)
  5. makeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
  6. setBoxXLayout(JPanel p)