Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:34:15+00:00 2026-06-11T22:34:15+00:00

I have a JPanel with some JLabel added with the add() method of JPanel.

  • 0

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!

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:34:17+00:00Added an answer on June 11, 2026 at 10:34 pm

    This can be done in two ways.

    JLabel Horizontal Alignment

    You can use the JLabel constructor:

    JLabel(String text, int horizontalAlignment) 
    

    To align to the right:

    JLabel label = new JLabel("Telephone", SwingConstants.RIGHT);
    

    JLabel also has setHorizontalAlignment:

    label.setHorizontalAlignment(SwingConstants.RIGHT);
    

    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:

        Box box = Box.createVerticalBox();
        JLabel label1 = new JLabel("test1, the beginning");
        label1.setAlignmentX(Component.RIGHT_ALIGNMENT);
        box.add(label1);
    
        JLabel label2 = new JLabel("test2, some more");
        label2.setAlignmentX(Component.RIGHT_ALIGNMENT);
        box.add(label2);
    
        JLabel label3 = new JLabel("test3");
        label3.setAlignmentX(Component.RIGHT_ALIGNMENT);
        box.add(label3);
    
    
        add(box);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPanel and I want do draw to this JPanel some very
I have a JPanel for which I set some image as the background. I
I have a JPanel in which I need to add bunch of JLabel s
Right, I have a JTabbedPane that has a JPanel that contains a JLabel and
I have a JPanel which contains some fields. The height of the JPanel is
I have an JPanel populated with several opaque custom components. Now I would like
I have a JPanel, which I would like to detect the following events (1)
I have found similar questions here on Stackoverflow, but for some reason when I
I want to overlap two image. For that, I use JLabel and set an
I have a new error with the code below. Originally I had some classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.