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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:16:00+00:00 2026-05-24T17:16:00+00:00

Font font = Font(Arial, Font.BOLD, 35); JLabel label = new JLabel(57); JPanel panel =

  • 0
Font font = Font("Arial", Font.BOLD, 35);

JLabel label = new JLabel("57");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(label);

This creates a JLabel with an extra space above and below it. I tried setVerticalAlignment(SwingConstants.TOP) but it not working. Again, I don’t want to align JLabel to top but the text inside JLabel should be aligned to top.

here is how my label looks like
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-05-24T17:16:01+00:00Added an answer on May 24, 2026 at 5:16 pm

    The text in your label is actually aligned to the top already. Even if you set all three of:

    label.setVerticalAlignment(JLabel.TOP);
    label.setVerticalTextPosition(JLabel.TOP);
    panel.setAlignmentY(TOP_ALIGNMENT);
    

    you still would find that gap.

    The problem is related to font-metrics. The font leaves space for diacritics, and while English numbers and even letters do not contain diacritics on capital letters, Arial definitely contains a full-breadth of international characters including ones taller than a capital letter, for example, German umlauts (ÄÖÜ) or characters containing Portuguese diacritics (ÁÂÃ).

    If you want a quick, easy solution that is a hack, that may not scale well across fonts and platforms, you can use a negative value on a border to compensate for the font metrics.

    label.setBorder(BorderFactory.createEmptyBorder( -3 /*top*/, 0, 0, 0 ));
    

    If you want to fix it “right” you should look into learning about the FontMetrics package, as it has many functions that could be useful to calculating the actual height and location of the text being displayed, such that you can move the whole string by the appropriate amount of pixels.

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

Sidebar

Related Questions

I need to do something like this in iText: Font fuente=new Font(Font.getFamily(ARIAL),30,Font.BOLD); But instead
I found this css body { font: 13px/1.231 arial,helvetica,clean,sans-serif; *font-size: small; *font: x-small; }
Say I have this HTML fragment <td width=25% style=text-align:right; > <span style=font-family: Arial; font-size:
I am trying to embed arial bold font but getting run time error font
I have the following CSS: html, body { background-color:black; font-family:arial bold; overflow:auto; text-align: center;
I am creating an installer that also needs to delete the Arial font (I
I'm programmatically sending HTML-formatted email, and setting the font as Arial throughout ( font-family:
In a nutshell, there's a global stylesheet: a { font-family: Arial; } I want
My code: body { font-family:Verdana,Arial,Helvetica,sans-serif; } .myfont { font-family:Verdana,Arial,Helvetica,sans-serif; } <body> Hello <select> <option>
One of my friend told me Use font family: helvetica, arial, sans-serif in your

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.