0

I have an interesting task. Consider a String contentwith unknown length. I'd like to display content with a Font font with Justified text. I need to know what height this would take up if I specify the width. I want to display this text in the middle of the screen but it must have a width of width.

Hence, I would like to do something such as the following.

//create a JTextPane or JTextArea
pane.setText(content);
pane.setFont(font);
//set the width of the pane here
//get the height of the full text block if its justified
pane.setLocation(screenWidth/2 - width/2, screenHeight/2, height/2);

How can I do this?

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • Justified: Left? Right? Center? Fill? See also this [Q&A](http://stackoverflow.com/q/5979795/230513) on `TextLayout`. – trashgod Dec 31 '12 at 19:27
  • See http://en.wikipedia.org/wiki/Justification_(typesetting) to learn what "Justified" text is. – CodeGuy Dec 31 '12 at 19:28
  • Are you specifying the width? It looks like you need to compute the width based on the font size and String to display. – Thorn Dec 31 '12 at 19:32
  • I'm specifying the width, and need to determine the height. – CodeGuy Dec 31 '12 at 19:38
  • http://en.wikipedia.org/wiki/Justification_(typesetting) – CodeGuy Dec 31 '12 at 19:38
  • by default `JLabel.setText("

    + your text here + ")`, same / similair with `JTextPane/Editor`, no idea how to determine `numbers of rows` for `JLabel`, `JTextPane/Editor` returns that by default, no idea where is your issue

    – mKorbel Dec 31 '12 at 19:49
  • can you fill in the code I'm missing then? I'm not following completely what you're saying. keep in mind I'm trying to use a specific font. – CodeGuy Dec 31 '12 at 20:04
  • 3
    I' sure that must be solved by ([@StanislavL](http://stackoverflow.com/users/301607/stanislavl)) or ([@Andrew Thompson)](http://stackoverflow.com/users/418556/andrew-thompson)) a few times here – mKorbel Dec 31 '12 at 20:15
  • @mKorbel Thanks for the accolades, but why did you delete *your* answer? Seems to me your suggestion of `style='text-align:justify'` achieves the stated goal perfectly. – Andrew Thompson Jan 01 '13 at 02:31

1 Answers1

1

Normally, justification is done the other way around.

The text has a fixed height, and you adjust the width of the text in one of the following ways:

  • Add spaces between the words. Easier, but not visually appealing.
  • Add space between the letters. This is called kerning.

If you adjust the height so that the width is correct, every line in your text will have a different height.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111