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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:46:30+00:00 2026-05-14T23:46:30+00:00

I am trying to fit a sentence that changes often, in to a few

  • 0

I am trying to fit a sentence that changes often, in to a few jlabels. Widths of my 3 jlabels stay unchanged all the time. What I am doing is changing the font size so all the characters can fit with out non being out of the display range of the labels. What I do is call below code snippet when ever sentence is changed.

Here is my code

    String sentence = "Some long sentence";
    int SentenceLength = sentence.length();
    int FontSize = 0;
    // sum of widths of the three labels
    int TotalLblLength=lbl_0ValueInWords.getWidth()+lbl_1ValueInWords.getWidth()+lbl_1ValueInWords.getWidth();

    /*decide the font size so that all the characters can be displayed 
     with out exceeding the display renge(horizontal) of the 3 labels 
     Inconsolata -> monopace font
     font size == width of the font*2 (something I observed, not sure 
     if this is true always)  */
    FontSize=(TotalLblLength/SentenceLength)*2;          
    // max font size is 20 - based on label height
    FontSize=(FontSize>20)?20:FontSize; 

    lbl_0ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));
    lbl_1ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));
    lbl_2ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));

    int CharCount_lbl0 = width_lbl0 / (FontSize / 2);
    int CharCount_lbl1 = width_lbl1 / (FontSize / 2);
    int CharsCount_lbl2 = width_lbl2 / (FontSize / 2);

    /*Set texts of each label
     if sentence has more than the number of characters that can fit in the
     1st label, excessive characters are moved to the 2nd label. same goes 
     for the 2nd and 3rd labels*/
    if (SentenceLength > CharCount_lbl0) {
        lbl_0ValueInWords.setText(sentence.substring(0, CharCount_lbl0));
        if (SentenceLength > CharCount_lbl0 + CharCount_lbl1) {
            lbl_1ValueInWords.setText(sentence.substring(CharCount_lbl0, CharCount_lbl0 + CharCount_lbl1));
            lbl_2ValueInWords.setText(sentence.substring(CharCount_lbl0 + CharCount_lbl1, SentenceLength));
        } else {
            lbl_1ValueInWords.setText(sentence.substring(CharCount_lbl0, SentenceLength));
        }
    } else {

        lbl_0ValueInWords.setText(sentence);
    }

But even after resetting font size sometimes the last character goes out of the display range. I have removed margines from the jlabels that may cause this. This happens for random length sentences. I can solve the problem for the application by reducing label width used for the calculations(hopefully)

Can anyone explain me the reason? Could be because of some defect in the fonts symmetry?

  • 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-14T23:46:31+00:00Added an answer on May 14, 2026 at 11:46 pm

    There is no such thing as font symmetry?

    There are 2 types of fonts for what you are dealing with. Monospace fonts, and non-monospace fonts. Monospace fonts have the same exact width for every single character you can type. The others do not.

    On top of that, fonts are rendered differently across different OS’s. Something on windows will be around 10-20% longer on Mac because they space out the fonts differently.

    Whatever it is you are trying to do with JLabels, stop. You should not be using 3 JLabels to show 3 lines of text because they dont fit. Scrap them and use a JTextArea. It has text wrap, you can set the font, and remove the margin/border/padding and make it non-editable. You can customize it very easily so it is indistinguishable from a JLabel, but it will save you a ton of work.

    Pick the right tool for the right job.

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

Sidebar

Related Questions

I'm trying to fit an image into a <td> that is smaller than the
So I'm trying to look at an input, and count words that fit a
I'm trying to fit in a google plus widget inside a div that has
I'm trying to fit a 20x20 table into a view using a programmatically built
I find myself always trying to fit everything into the OOP methodology, when I'm
I have installed rapache and I am trying to fit a linear model inside
Based on earlier thread with same argument i am trying to fit a stable
I'm trying to calculate the number of lines of best fit for an ellipse;
I'm trying to use scipy.leastsq() as a method of finding a best fit point
Trying to add the ability to delete a Folder using FTP and all subfolders

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.