I have a JPanel with some JLabel added with the add() method of JPanel. I want to align the JLabel to the right like the image below but I don’t know how to do that. Any Idea? Thanks!

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This can be done in two ways.
JLabel Horizontal Alignment
You can use the
JLabelconstructor:To align to the right:
JLabelalso hassetHorizontalAlignment:This assumes the component takes up the whole width in the container.
Using Layout
A different approach is to use the layout to actually align the component to the right, whilst ensuring they do not take the whole width. Here is an example with
BoxLayout: