JDK-8232243 : Wrong caret position in JTextPane on Windows with a screen resolution > 100%
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 9,10,11,12,13
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2019-10-14
  • Updated: 2021-10-11
  • Resolved: 2020-05-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 15
11.0.13-oracleFixed 15 b23Fixed
Related Reports
Relates :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Windows 10, JDK 13

A DESCRIPTION OF THE PROBLEM :
JTextPane still has the issue with caret position described here: https://bugs.openjdk.java.net/browse/JDK-8199441
JTextArea works well in JDK > 11.0.1, whereas JTextPane does not.


REGRESSION : Last worked in version 8u221

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Run Windows
2) Set the screen resolution to 150%
3) Start the provided Java program
4) It shows a JTextPane pre-filled with text
5) Click at random places in the text and the caret is inserted find at the cursor position
6) Now check "Line Wrap" and repeat the previous step and you will see that the caret is not positioned at the cursor position

Further, the demo program shows a combobox with all available fonts. Almost all of them gives the same result, except for example the Monospaced font. It works fine with it even with Line Wrap checked. 

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The caret should always be inserted where the mouse is clicked. 
ACTUAL -
The caret is not in the same position as where the cursor is clicked. 

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

/**
 * Test to show that the caret when left-click doesn't align with
 * the mouse pointer position on Windows with Java 13 and screen resolution > 100%.
 */
public class TestCaretJava13 {

    private TestCaretJava13() {
        JFrame f = new JFrame("Test Cursor/Caret with Java 9");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JTextPane textPane = new JTextPane();

        JComboBox<Font> fontCombo = new JComboBox<>();
        fontCombo.setMaximumRowCount(20);
        fontCombo.addItemListener(e -> {
            Font font = (Font) e.getItem();
            textPane.setFont(font);
        });
        fontCombo.addItem(textPane.getFont());
        fontCombo.addItem(new Font("Monospaced", Font.PLAIN, 12));
        List<Font> fonts = getFonts();
        for (Font font : fonts) {
            fontCombo.addItem(font);
        }

        fillTextPane(textPane);

        JPanel toolbar = new JPanel();
        toolbar.add(fontCombo);

        f.add(toolbar, BorderLayout.NORTH);
        f.add(new JScrollPane(textPane), BorderLayout.CENTER);

        f.pack();
        f.setVisible(true);
    }

    private void fillTextPane(JTextPane textPane) {
        StringBuilder buf = new StringBuilder();
        addSystemProperties(buf);

        for (int i = 0; i < 30; i++) {
            StringBuilder row = new StringBuilder();
            for (int j = 0; j < 50; j++) {
                row.append(j);
                if (j % 5 == 0) {
                    row.append(" ");
                }
            }
            buf.append(row).append(System.lineSeparator());
        }
        textPane.setText(buf.toString());
        textPane.setCaretPosition(0);
    }

    private void addSystemProperties(StringBuilder buf) {
        buf.append("os.name: ").append(System.getProperty("os.name")).append(System.lineSeparator());
        buf.append("os.version: ").append(System.getProperty("os.version")).append(System.lineSeparator());
        buf.append("os.arch: ").append(System.getProperty("os.arch")).append(System.lineSeparator());
        buf.append("java.version: ").append(System.getProperty("java.version")).append(System.lineSeparator());
        buf.append("java.vm.name: ").append(System.getProperty("java.vm.name")).append(System.lineSeparator());
        buf.append("java.vm.vendor: ").append(System.getProperty("java.vm.vendor")).append(System.lineSeparator());
        buf.append("java.home: ").append(System.getProperty("java.home")).append(System.lineSeparator());
        buf.append("Monitors: ").append(System.lineSeparator()).append(getScreenInfo()).append(System.lineSeparator());
        buf.append(System.lineSeparator());
    }

    private static String getScreenInfo() {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = env.getScreenDevices();

        StringBuilder returnString = new StringBuilder();
        int screenNo = 1;
        for (GraphicsDevice device : devices) {
            DisplayMode displayMode = device.getDisplayMode();
            if (returnString.length() > 0) {
                returnString.append(System.lineSeparator());
            }
            returnString.append("Screen ").append(screenNo++);
            returnString.append(": size: ").append(displayMode.getWidth()).append(" x ").append(displayMode.getHeight());
            returnString.append(", refresh rate: ").append(displayMode.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN ? displayMode.getRefreshRate() : "unknown");
            returnString.append(", bit depth: ").append(displayMode.getBitDepth());
            if (isHiDPI(device)) {
                returnString.append(", HiDPI display: true");
            }
        }
        return returnString.toString();
    }

    private static boolean isHiDPI(GraphicsDevice device) {
        try {
            Field field = device.getClass().getDeclaredField("scale");

            if (field != null) {
                field.setAccessible(true);
                Object scale = field.get(device);

                if (scale instanceof Integer && (Integer) scale == 2) {
                    return true;
                }
            }
        } catch (Throwable ignore) {
        }

        return device.getDefaultConfiguration().getDefaultTransform().getScaleX() == 2;
    }

    private List<Font> getFonts() {
        List<Font> fonts = new ArrayList<>();

        String[] fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

        for (String fontFamilyName : fontFamilyNames) {
            Font font = new Font(fontFamilyName, Font.PLAIN, 12);
            fonts.add(font);
        }
        return fonts;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestCaretJava13::new);
    }
} 
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Either set resolution in Windows to 100% or use the Monospaced font in Java 

FREQUENCY : always



Comments
Fix Request (11u): Should get backported for parity with 11.0.13-oracle. Applies cleanly.
28-07-2021

URL: https://hg.openjdk.java.net/jdk/jdk/rev/248314145b78 User: psadhukhan Date: 2020-05-09 04:20:48 +0000
09-05-2020

URL: https://hg.openjdk.java.net/jdk/client/rev/248314145b78 User: psadhukhan Date: 2020-05-06 08:28:32 +0000
06-05-2020

Pending review
16-04-2020

The issue is reproducible from jdk9 onwards
13-11-2019

Checked this for reported version JDk-13, but couldn't reproduce as explained in description. Writing back to the submitter requesting additional information.
15-10-2019