<Swing Dev> RFR JDK-8232243:Wrong caret position in JTextPane on Windows with a screen resolution > 100%

Prasanta Sadhukhan prasanta.sadhukhan at oracle.com
Thu Apr 16 10:16:26 UTC 2020


Hi All,

Please review a fix for an issue where it is seen that, with a screen 
resolution higher than 100% and then clicking in a JTextPane, the caret 
(insertion point) is not aligned with the cursor.

The issue seems to stem from the fact that caret position calculation in 
DefaultCaret class utilises API that uses integer calculation than 
floating point calculations.

This issue is in continuation to the fix done for JDK-8199441 where the 
issue was fixed for JTextArea. 
[http://hg.openjdk.java.net/jdk/jdk/rev/480c2ae4d031]

The code flow for JTextArea was
DefaultCaret#positionCaret=>BasicTextUI#viewToModel=>BoxView.viewToModel=>CompositeView#viewToModel=>WrappedPlainView#viewToModel=>Utilities.getTabbedOffset 


whereas for JTextPane it it is 
DefaultCaret#positionCaret=>BasicTextUI#viewToModel=>BoxView.viewToModel=>CompositeView#viewToModel=>GlyphPainter1#viewToModel=>Utilities.getTabbedOffset

Now, getTabbedOffset utilises FontMetrics.charsWidth() which uses 
integer arithmetic to get the caret position.
The same getTabbedOffset uses Font.getStringBounds() which uses floating 
point arithmetic via Rectangle2D.Float.

Proposed fix is to make sure proper floating point getTabbedOffset API 
is used so that floating point calculations is done for char width.

Bug:https://bugs.openjdk.java.net/browse/JDK-8232243

webrev: http://cr.openjdk.java.net/~psadhukhan/8232243/webrev.0/

Regards
Prasanta


More information about the swing-dev mailing list