Java AWT LayoutManager2 extend
import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Insets; import java.awt.LayoutManager2; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; class MyGridLayout implements LayoutManager2 { private int vgap; private int hgap; private int rows = 2; private int cols = 2; private int minWidth; private int minHeight; private int preferredWidth; private int preferredHeight; private boolean sizeUnknown = true; private Component[][] components; private MyCellConstraint[][] constraints; public MyGridLayout(int hgap, int vgap, int cols, int rows) { this.hgap = hgap; this.vgap = vgap; this.rows = rows; this.cols = cols; components = new Component[rows][cols]; constraints = new MyCellConstraint[rows][cols]; }/*from ww w . j a v a 2 s . c o m*/ public void addLayoutComponent(String name, Component comp) { } public void removeLayoutComponent(Component comp) { } private void setSizes(Container parent) { System.out.println("-setSizes"); preferredWidth = 0; preferredHeight = 0; minWidth = 0; minHeight = 0; // calculate the largest width of all columns int maxColWidth[] = new int[cols]; int maxColHeight[] = new int[rows]; updateMaxColWidthAndHeight(maxColWidth, maxColHeight); // update preferred width for (int colIndx = 0; colIndx < maxColWidth.length; colIndx++) { preferredWidth += maxColWidth[colIndx]; preferredWidth += hgap; } preferredWidth += hgap; for (int rowIndx = 0; rowIndx < maxColHeight.length; rowIndx++) { preferredHeight += maxColHeight[rowIndx]; preferredHeight += vgap; } preferredHeight += vgap; } public Dimension preferredLayoutSize(Container parent) { Dimension dim = new Dimension(0, 0); setSizes(parent); // Add the container's insets Insets insets = parent.getInsets(); dim.width = preferredWidth + insets.left + insets.right; dim.height = preferredHeight + insets.top + insets.bottom; sizeUnknown = false; return dim; } public Dimension minimumLayoutSize(Container parent) { Dimension dim = new Dimension(0, 0); // Add the container's insets Insets insets = parent.getInsets(); dim.width = minWidth + insets.left + insets.right; dim.height = minHeight + insets.top + insets.bottom; sizeUnknown = false; return dim; } public void layoutContainer(Container parent) { Insets insets = parent.getInsets(); int x = 0, y = insets.top; if (sizeUnknown) { setSizes(parent); } // calculate the largest width of all columns int maxColWidth[] = new int[cols]; // calculate the largest height of all columns int maxColHeight[] = new int[rows]; updateMaxColWidthAndHeight(maxColWidth, maxColHeight); int previousWidth = 0, previousHeight = 0; for (int rowNum = 0; rowNum < components.length; rowNum++) { y += previousHeight + vgap; x = 0; previousWidth = 0; for (int colNum = 0; colNum < components[rowNum].length; colNum++) { Component curComp = components[rowNum][colNum]; Dimension cDim = null; if (curComp == null) { cDim = new Dimension(maxColWidth[colNum], maxColHeight[rowNum]); } else { cDim = curComp.getPreferredSize(); } x += previousWidth + hgap; MyCellConstraint cConstr = constraints[rowNum][colNum]; if (cConstr != null) { switch (cConstr.getAlign()) { case MyCellConstraint.RIGHT: x += maxColWidth[colNum] - cDim.width; break; case MyCellConstraint.CENTER: x += (maxColWidth[colNum] - cDim.width) / 2; break; } } if (curComp != null) { // Set the component's size and position. curComp.setBounds(x, y, cDim.width, cDim.height); } previousWidth = cDim.width; } previousHeight = maxColHeight[rowNum]; } previousWidth += hgap; previousHeight += vgap; } @Override public void addLayoutComponent(Component comp, Object constraint) { MyCellConstraint targetC = (MyCellConstraint) constraint; if (targetC != null) { components[targetC.getRowNum()][targetC.getColNum()] = comp; constraints[targetC.getRowNum()][targetC.getColNum()] = targetC; } } @Override public float getLayoutAlignmentX(Container target) { return 1f; // center } @Override public float getLayoutAlignmentY(Container target) { return 0f; // leading } @Override public void invalidateLayout(Container target) { } @Override public Dimension maximumLayoutSize(Container target) { return preferredLayoutSize(target); } private void updateMaxColWidthAndHeight(int[] maxColWidth, int[] maxColHeight) { for (int rowNum = 0; rowNum < components.length; rowNum++) { for (int colNum = 0; colNum < components[rowNum].length; colNum++) { Component curComp = components[rowNum][colNum]; if (curComp == null) { continue; } Dimension cDim = curComp.getPreferredSize(); maxColWidth[colNum] = Math.max(maxColWidth[colNum], cDim.width); maxColHeight[rowNum] = Math.max(maxColHeight[rowNum], cDim.height); } } } } class MyCellConstraint { private int rowNum = 0; private int colNum = 0; public final static int LEFT = -1; public final static int CENTER = 0; public final static int RIGHT = 1; private int align = LEFT; public int getAlign() { return align; } public MyCellConstraint setAlign(int align) { this.align = align; return this; } public int getColNum() { return colNum; } public MyCellConstraint setColNum(int colNum) { this.colNum = colNum; return this; } public int getRowNum() { return rowNum; } public MyCellConstraint setRowNum(int rowNum) { this.rowNum = rowNum; return this; } } class MyPanel extends JPanel { public MyPanel() { super(); JLabel fNameLbl = new JLabel("First Name"); JTextField fNameFld = new JTextField(15); JLabel lNameLbl = new JLabel("Last Name"); JTextField lNameFld = new JTextField(15); JButton saveButt = new JButton("Save"); // Create a 2x3 grid with 5 horizontal and vertical gaps // between components. MyGridLayout cglayout = new MyGridLayout(5, 5, 2, 3); setLayout(cglayout); // First name label addToPanel(fNameLbl, 0, 0, MyCellConstraint.RIGHT); // Last name label addToPanel(lNameLbl, 0, 1, MyCellConstraint.RIGHT); // First name field addToPanel(fNameFld, 1, 0, MyCellConstraint.LEFT); // Last name field addToPanel(lNameFld, 1, 1, MyCellConstraint.LEFT); // Save button addToPanel(saveButt, 1, 2, MyCellConstraint.RIGHT); } private void addToPanel(Component comp, int colNum, int rowNum, int align) { MyCellConstraint constr = new MyCellConstraint().setColNum(colNum).setRowNum(rowNum).setAlign(align); add(comp, constr); } } public class Main { public static void main(String[] args) { // create frame for Main JFrame frame = new JFrame("java2s.com"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MyPanel()); frame.setSize(300, 210); frame.setVisible(true); } }