Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rows in JTextArea

Is there a reliable way to calculate how is a string divided into rows in JTextArea?

I have a JTextArea with fixed width and when it gets filled, a new row is added and it expands vertically.

Now, I need to know exactly which characters are in which row. I could add up single character widths using font metrics, but I don't know if that is reliable, or if there may be a better method.

Is the font metrics "trick" the only way?

like image 495
Karlovsky120 Avatar asked Nov 21 '25 01:11

Karlovsky120


1 Answers

All JTextComponents have modelToView(...) and viewToModel(...) methods that can help, but perhaps even better are the methods in the javax.swing.text.Utilities class including getRowStart(...) and getRowEnd(...)

like image 183
Hovercraft Full Of Eels Avatar answered Nov 23 '25 15:11

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!