Java examples for Swing:GridBagLayout
Add one line components to panel use GridBagLayout
//package com.java2s; import java.awt.Component; import java.awt.GridBagConstraints; import java.util.List; import javax.swing.JPanel; public class Main { public static void addLineComponents(JPanel panel, List<Component[]> lineList, GridBagConstraints gbc, int leftInset, int midInset, int rightInset) { for (Component[] line : lineList) addLineComponents(panel, line, gbc, leftInset, midInset, rightInset);/*ww w. ja v a 2 s .c o m*/ } /** * Add one line components to panel use GridBagLayout * @param panel * @param line * @param gbc * @param leftInset * @param midInset * @param rightInset */ public static void addLineComponents(JPanel panel, Component[] line, GridBagConstraints gbc, int leftInset, int midInset, int rightInset) { Component leftComp = line[0]; Component rightComp = line[line.length - 1]; // add only one component if (line.length == 1) { gbc.weightx = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets.left = leftInset; gbc.insets.right = rightInset; panel.add(leftComp, gbc); } else { gbc.weightx = 1.0; gbc.gridwidth = 1; gbc.insets.left = leftInset; gbc.insets.right = midInset; panel.add(leftComp, gbc); // add middle components for (int i = 1; i < line.length - 1; i++) { gbc.weightx = 1.0; gbc.gridwidth = 1; gbc.insets.left = 0; gbc.insets.right = midInset; Component midComp = line[i]; panel.add(midComp, gbc); } // add right component gbc.weightx = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets.left = 0; gbc.insets.right = rightInset; panel.add(rightComp, gbc); } } }