Java tutorial
//package com.java2s; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JTextField; public class Main { /** * Adds a <code>JTextField</code> with the specified text as a label * appearing before the text field to the given <code>JComponent</code>. * Assuming the <code>JComponent</code> has the MigLayout set, the label * and text field are both left aligned within the <code>JComponent</code>. * If the wrap parameter is set to true, MigLayout's "wrap" attribute will be * applied to the <code>JTextField</code>, meaning the next component added * will appear on the next line. * (Exception: Will not work if MigLayout's flowy layout constraint is applied, * but it is rarely used MigLayout feature and thus not a common concern; however * if you have set this layout constraint on your <code>JComponent</code> do not * attempt to use the wrap option of this method.) * @param label - the text to appear in front of the text field * @param length - the length in columns of the text field * @param wrap - indicates is this component should be the last on the current line * @return the <code>JTextField</code> added to the component */ public static JTextField addLabeledTextField(JComponent c, String label, int length, boolean wrap) { JLabel l = new JLabel(label); c.add(l, "align left"); JTextField text = new JTextField(length); if (wrap) { c.add(text, "align left, wrap"); } else { c.add(text, "align left"); } return text; } }