49

I would like to have all elements in my JPanel to be aligned to the left. I try to do it in the following way:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setAlignmentX(Component.LEFT_ALIGNMENT);

As a result Java use left side of all elements as a position of the element and then put all elements in the center (not left part) of the JPanel.

Roman
  • 124,451
  • 167
  • 349
  • 456

3 Answers3

116

The easiest way I've found to place objects on the left is using FlowLayout.

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

adding a component normally to this panel will place it on the left

Daniel G
  • 67,224
  • 7
  • 42
  • 42
Chris
  • 1,432
  • 1
  • 10
  • 8
  • 1
    +1 For me a cast was necessary: `JPanel panel = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEFT));` – caw May 17 '12 at 00:40
  • 1
    @theUg In case you've been working on this for the past 6 years, Eclipse won't recognize this constructor until you import `FlowLayout`. I was being told the `JPanel(LayoutManager)` as well, but after importing the `FlowLayout` class that line worked. – Lord Farquaad Sep 13 '18 at 18:01
14

You should use setAlignmentX(..) on components you want to align, not on the container that has them..

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(c1);
panel.add(c2);

c1.setAlignmentX(Component.LEFT_ALIGNMENT);
c2.setAlignmentX(Component.LEFT_ALIGNMENT);
Jack
  • 131,802
  • 30
  • 241
  • 343
  • 5
    I do not think so. `setAlignementX(Component.LEFT_ALIGNEMENT)` means that left side of the component will be used to align elements. And elements will still be aligned to the center of JPanel. – Roman Apr 26 '10 at 15:31
  • 1
    Are you sure? Take a look here http://www.java2s.com/Code/Java/Swing-JFC/ComponentAlignment.htm the method setAlignmentX is used on buttons added to the panel, not on the panel itself – Jack Apr 26 '10 at 16:09
  • 2
    Jack, you are right that this method is used on buttons but it does not have the effect you described. It does not define the position of the button. It defines the part of the button which will be used for the alignment. For example "LEFT" side of the button will be used to put button in the center (left side of the button will be put into the center). – Roman Apr 27 '10 at 12:04
  • This does not help (me) to align whole components within their parent. – mvreijn Dec 02 '15 at 14:30
1

My favorite method to use would be the BorderLayout method. Here are the five examples with each position the component could go in. The example is for if the component were a button. We will add it to a JPanel, p. The button will be called b.

//To align it to the left
p.add(b, BorderLayout.WEST);

//To align it to the right
p.add(b, BorderLayout.EAST);

//To align it at the top
p.add(b, BorderLayout.NORTH);

//To align it to the bottom
p.add(b, BorderLayout.SOUTH);

//To align it to the center
p.add(b, BorderLayout.CENTER);

Don't forget to import it as well by typing:

import java.awt.BorderLayout;

There are also other methods in the BorderLayout class involving things like orientation, but you can do your own research on that if you curious about that. I hope this helped!