Here you can find the source of addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container)
public static void addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container)
//package com.java2s; /*//from w w w . j a va 2 s . c o m Copyright (c) 2006 Harri Kaimio This file is part of Photovault. Photovault is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Photovault is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Photovault; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ import javax.swing.*; import java.awt.*; public class Main { public static void addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container) { GridBagConstraints c = new GridBagConstraints(); c.anchor = GridBagConstraints.EAST; c.insets = new Insets(2, 2, 2, 2); int numLabels = labels.length; for (int i = 0; i < numLabels; i++) { c.anchor = GridBagConstraints.EAST; c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last c.fill = GridBagConstraints.NONE; //reset to default c.weightx = 0.0; //reset to default gridbag.setConstraints(labels[i], c); container.add(labels[i]); c.anchor = GridBagConstraints.WEST; c.gridwidth = GridBagConstraints.REMAINDER; //end row c.weightx = 1.0; gridbag.setConstraints(textFields[i], c); container.add(textFields[i]); } } }