An example of a nested layout, one using BorderLayout, FlowLayout (JPanel by default) and GridBagLayout:
import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.util.HashMap; import java.util.Map; import javax.swing.*; public class LayoutFoo { private static final String TITLE = "Configure Main Foobar Parameters"; private static final String[] LABEL_TEXTS = { "Number of Spams", "Number of Frapzats", "Number of Zignuts" }; private static final int TEXTFIELD_SIZE = 10; private static final Insets WEST_INSETS = new Insets(5, 5, 5, 10); private static final Insets EAST_INSETS = new Insets(5, 10, 5, 5); private static final int EB_GAP = 5; private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>(); public JPanel getConfigFooPanel() { JPanel textFieldPanel = new JPanel(new GridBagLayout()); for (int i = 0; i < LABEL_TEXTS.length; i++) { addTextAndField(textFieldPanel, LABEL_TEXTS[i], i); } int blVertGap = 20; JPanel borderLayoutPanel = new JPanel(new BorderLayout(0, blVertGap)); borderLayoutPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP)); JLabel titleLabel = new JLabel(TITLE, JLabel.CENTER); borderLayoutPanel.add(titleLabel, BorderLayout.PAGE_START); borderLayoutPanel.add(textFieldPanel, BorderLayout.CENTER); JPanel outerWrapperFlowPanel = new JPanel(); outerWrapperFlowPanel.add(borderLayoutPanel); return outerWrapperFlowPanel; } public String getFieldText(String labelText) { JTextField field = textFieldMap.get(labelText); if (field == null) { return "";
source share