10

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
skyork
  • 7,113
  • 18
  • 63
  • 103

7 Answers7

7

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:

enter image description here

Use the label alignment that pushes the text to the RHS.


See also this answer for an MCVE.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
6

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MByD
  • 135,866
  • 28
  • 264
  • 277
  • 2
    +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. :) – Hovercraft Full Of Eels May 14 '11 at 23:13
  • This approach nests nicely as one `LabelTextPanel` (having a `GridBagLayout`) for each row in a `GridLayout`. – trashgod May 14 '11 at 23:52
3

or

there is possible align just text inside JTextComponents with

JLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1 See also this [example](http://stackoverflow.com/questions/3174765/variable-layout-in-swing/3175280#3175280). – trashgod May 14 '11 at 23:10
  • hmmm that was good and same as how to create EmptyBorder at 10pixels by using BorderLayout ... (place JComponents around CENTER locations without any sizing) – mKorbel May 14 '11 at 23:25
2

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);
...
jfpoilpret
  • 10,449
  • 2
  • 28
  • 32
1

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.

Osiris76
  • 1,234
  • 9
  • 5
  • 1
    But 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 May 14 '11 at 23:11
  • 1
    @Hovercraft is correct, although this can work if the labels and fields are of comparable size and when used with @mKorbel's [answer](http://stackoverflow.com/questions/6005258/how-to-align-jlabel-jtextfield-pairs-vertically/6005330#6005330). – trashgod May 14 '11 at 23:14
1

Good solutions for this that I've seen include use of the GridBagLayout (as noted above) or the MiGLayout, though since the latter isn't part of standard Java, it must be downloaded and placed on the classpath prior to use. MiGLayout is not as difficult to use.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

The LayoutManager of the parent component has the responsability of positioning the elements contained. Maybe you need to set an XYLayout.

See the setLayoutManager() for your parent class.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • 1
    -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. – camickr May 15 '11 at 00:35