Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Example of a nested layout, one using BorderLayout, FlowLayout (JPanel's default), and GridBagLayout:</p> <pre><code>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&lt;String, JTextField&gt; textFieldMap = new HashMap&lt;String, JTextField&gt;(); public JPanel getConfigFooPanel() { JPanel textFieldPanel = new JPanel(new GridBagLayout()); for (int i = 0; i &lt; 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 ""; // ?? throw exception } else { return field.getText(); } } private void addTextAndField(JPanel panel, String text, int i) { JLabel label = new JLabel(text, JLabel.LEFT); JTextField textField = new JTextField(TEXTFIELD_SIZE); textFieldMap.put(text, textField); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = i; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; gbc.insets = WEST_INSETS; panel.add(label, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.EAST; gbc.insets = EAST_INSETS; panel.add(textField, gbc); } private static void createAndShowGui() { JFrame frame = new JFrame("LayoutFoo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new LayoutFoo().getConfigFooPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload