Home > Mobile >  How to get the height when knowing the width of the label in java swing?
How to get the height when knowing the width of the label in java swing?

Time:11-17

Hi everyone i am using java swing now. I have a problem like this: I have a pretty long piece of text and put it in a label with the <html> tag.

<html> My text </html>

If the text is too long, it will break the line according to the width of the label. How to calculate the number of line breaks? or height needed to display text after line breaks

This is my code

public Test() {
        initComponents();
        setPreferredSize(new Dimension(200, 200));
        add(new JLabel("<html> "
                  "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."
                  "</html>"));
    }

enter image description here

How to know the exact height needed to display the text?

  • Related