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 6123905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:04:18+00:00 2026-05-23T16:04:18+00:00

Basically I need to get a JLabel component to align to the top of

  • 0

Basically I need to get a JLabel component to align to the top of it’s parent JPanel.

Here is my code:

public static JPanel createBoldLabelPanel(String labelText, Component c, Font font, Color c1){
    c.setFont(new Font(font.getName(), Font.PLAIN, font.getSize()));
    c.setBackground(c1);
    JPanel jp = new JPanel();
    if(!labelText.isEmpty()){
        jp.setLayout(new BoxLayout(jp, BoxLayout.LINE_AXIS));
        JLabel label = new JLabel(labelText);
        label.setFont(new Font(font.getFontName(), Font.BOLD, font.getSize()));
        label.setBackground(c1);
        label.setAlignmentY(Component.TOP_ALIGNMENT);
        jp.add(label);
    }
    jp.add(c);
    jp.setBackground(c1);
    return jp;
}

And here is my test code:

public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JTextArea jta = new JTextArea("Sed ut perspiciatis, unde " +
            "omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem " +
            "aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, " +
            "explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia " +
            "consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, " +
            "qui dolorem ipsum, quia dolor sit amet, consectetur, adipisci[ng] velit, sed quia non numquam [do] " +
            "eius modi tempora inci[di]dunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim " +
            "ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex " +
            "ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, " +
            "quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?");
    jta.setEditable(false);
    jta.setLineWrap(true);
    jta.setWrapStyleWord(true);
    frame.add(DisplayPanel.createBoldLabelPanel("Test:", jta, new Font(Font.SERIF, Font.PLAIN, 14), Color.GRAY));
    frame.setSize(400, 400);
    frame.setVisible(true);
}

If you run it, what I want should become clear, if it’s not already. Thanks!

As you can see, I need "Test:" moved up

  • 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-05-23T16:04:18+00:00Added an answer on May 23, 2026 at 4:04 pm

    Also set the aligment property of your text area:

    jta.setAlignmentY(Component.TOP_ALIGNMENT);
    

    Then both will be aligned to the top within the panel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression to basically get the first part of a string,
Basically I need to do String.IndexOf() and I need to get array of indexes
I have an html-like xml, basically it is html. I need to get the
I basically need to know how to import SQL code into Access. I've tried
I have a web application. On the client side I basically need to get
basically I need to get a constant for a class however I have no
I have a unicode string in Python and basically need to go through, character
this is a follow up to this post . Basically I need to get
I basically need to get current date and time separately, formatted as: 2009-04-26 11:06:54
Basically I need to get a list of CampaignTitles that have more than 2

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.