How do I move/align the Yellow colored JLabel
in the JPanel
to right AND other JLabel
to the left?
Asked
Active
Viewed 782 times
-5

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
1Welcome to Stack Overflow! Please take the [tour] and visit our [help] to learn what kinds of questions are on topic for this site. If you can [edit] your question to make it on topic, please do so. – Joe C Jan 01 '19 at 18:31
-
1`JPanel panel = new JPanel(new BorderLayout()); panel.add(yellowLabel, BorderLayout.LINE_END); panel.add(blueLabel, BorderLayout.LINE_START);` – Andrew Thompson Jan 01 '19 at 18:35
-
1This question was originally closed as a duplicate of: https://stackoverflow.com/questions/17511518/how-to-align-jlabel-to-the-left-of-the-jpanel. It does not answer the question asked by the OP who want one label on the left and the other on the right. – camickr Jan 01 '19 at 20:19
-
Don't forget to "accept" answers when you get help by clicking on the checkbox so people know the problem has been solved. – camickr Jan 03 '19 at 21:41
1 Answers
2
You use an appropriate layout manager.
One option is to use a panel with a horizontal BoxLayout
:
Box panel = Box.createHorizontalBox();
panel.add( leftLabel );
panel.add( Box.createHorizontalGlue() );
panel.add( yellowLabel );
Another option is to use a panel with a BorderLayout
:
JPanel panel = new JPanel( new BorderLayout() );
panel.add(leftLabel, BorderLayout.LINE_START);
panel.add(yellowLabel, BorderLayout.LINE_END);
Read the section from the Swing tutorial on Layout Mangers for more information and working examples.

camickr
- 321,443
- 19
- 166
- 288