views:

194

answers:

1

Hi all,

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  A: 

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.

aasdasda
Thank you for your commentsWhat I ment to say was could it be because width of some of the characters in the font are different than othersactually the sentence goes over an image. Image has three lines and I want to display the sentence on those three lines. So I feel I'll have to go through the same trouble to set the text properly even with some other jtextcomponent.I read in few articles jlabels are not suitale for this kind of work (But I opted to use jlabels b'cos of the above reason). Is there any particular reason for that beside extra functionalities jtextarea provide?Thank you
Niroshan