How to align JLabel-JTextField pairs vertically
Solution 1
Is there any way to align the JLabels vertically to their right, so that the starts of JTextFields that follow would be aligned?
1.6+, GroupLayout
. E.G. from the JavaDocs:
Use the label alignment that pushes the text to the RHS.
See also this answer for an MCVE.
Solution 2
You didn't specify which layout do you use, so a good layout to implement that would be GridBagLayout. The demo in oracle site is great to start with.
And a short example:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
panel.add(new JLabel("Label 1:"), c);
c.gridx = 1;
c.gridy = 0;
panel.add(new JTextField("TextField 1"), c);
c.gridx = 0;
c.gridy = 1;
panel.add(new JLabel("Label 2:"), c);
c.gridx = 1;
c.gridy = 1;
panel.add(new JTextField("TextField 2"), c);
Solution 3
or
there is possible align just text inside JTextComponents with
JLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
Solution 4
This is a perfect use case for DesignGridLayout:
DesignGridLayout layout = new DesignGridLayout(contentPane);
layout.labelAlignment(LabelAlignment.RIGHT);
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
...
Solution 5
I would suggest the GridLayout layout manager. It presents the easiest solution to show pair-wise visualization of label and textbox controls. Thereby you simply define the number of rows and columns at time of instantiation and the added controls will be handled by the manager.
skyork
Updated on June 06, 2020Comments
-
skyork about 4 years
What I mean by a JLabel-JTextField pair is a JLabel component followed by a JTextField one, for example, "Parameter 1: -----" where "-----" denotes a blank JTextField.
The problem is, the width of JLabels varies due to the varying lengths of parameter names, so that the starts of JTextFields are not aligned vertically.
Is there any way to align the JLabels vertically to their right, so that the starts of JTextFields that follow would be aligned? Thanks.
-
trashgod about 13 years+1 See also this example.
-
Hovercraft Full Of Eels about 13 yearsBut the label and text fields are forced to be the same size, usually resulting in a very ugly GUI. I advise against using this.
-
Hovercraft Full Of Eels about 13 years+1, but they'll soon be complaining about the components bunching up in the middle when the GUI is resized. Don't forget the weightx and weighty. :)
-
trashgod about 13 years@Hovercraft is correct, although this can work if the labels and fields are of comparable size and when used with @mKorbel's answer.
-
mKorbel about 13 yearshmmm that was good and same as how to create EmptyBorder at 10pixels by using BorderLayout ... (place JComponents around CENTER locations without any sizing)
-
trashgod about 13 yearsThis approach nests nicely as one
LabelTextPanel
(having aGridBagLayout
) for each row in aGridLayout
. -
camickr about 13 years-1, XYLayout is not a standard layout and layout managers based on absolute positioning should not be encouraged since they are not really layout managers.
-
Murmel over 7 yearsIt is not part of the standard library
-
jfpoilpret over 7 years@user1885518 OP did not mention it had to be part of standard library.