The title pretty much says it all. What I don’t mean is how to get the size of the JTextArea
. I need to get the size of the actual text. I am completely open to suggestions though. If you have a wonky workaround that involves using something other than JTextArea
but still has a similar effect, I’d love to hear it. Thanks!
Advertisement
Answer
If you want to get the size of the Font
then you can use:
JTextArea area = new JTextArea(); Font font = area.getFont(); int fontSize = font.getSize();
Alternatively if you set the font of the text area yourself you’ll know before hand what the font size is, for example:
Font font = new Font("Dialog", Font.PLAIN, 12); area.setFont(font);
And if that wasn’t actually the question and instead you want to know how big certain parts of the font are in pixels you can use a FontMetrics
object for the font.
FontMetrics fontMetrics = area.getFontMetrics(font);
Using the FontMetrics
object you should be able to find out everything related to the size and distances in a font. You can find more information on how you can use the FontMetrics
object here: Javanotes, Measuring Text.
I hope this helps 🙂
Note: This applies to Swing, if you’re using a different kind of TextArea the answer will be different.