JDK-7045593 : Possible Regression : JTextfield cursor placement behavior algorithm has changed.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-05-17
  • Updated: 2011-07-18
  • Resolved: 2011-07-18
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 7
7 b145Fixed
Related Reports
Relates :  
Relates :  
Description
SYNOPSIS
--------
Possible Regression : JTextfield cursor placement algorithm is broken
Behavior changed with b134 and then changed again at b140.


OPERATING SYSTEM
----------------
ALL


FULL JDK VERSION
----------------
JDK 7 b134 onwards.  Reproducible with latest, b142.
Not reproducible with b133 and earlier.
Not reproducible JDK 6u25.


DESCRIPTION from LICENSEE
-------------------------
The behaviour of JTextField's cursor positioning algorithm has changed or has broken from JDK 7 b134 onwards. (Note that other components may also be affected - I've only tested with JTextField.) The exact symptoms change slightly in recent builds. Here are the details:

Behaviour in JDK 7b134-b139

If the mouse is clicked when the pointer is touching any part of a character, the cursor will be placed in between that character and the one preceding it - even if the pointer was only one pixel away from the gap between that character and the one after it. For example, consider the String "abcdefg". If you try to place the cursor between 'c' and 'd', the pointer is placed between 'b' and 'c' instead.

Behaviour in JDK 7 b140 onwards.

If the mouse is clicked when the pointer is touching any part of a character, the cursor will be placed in between that character and the one preceding it - even if the pointer was only one pixel away from the gap between that character and the one after it. For example, consider the String "abcdefg". If you try to place the cursor between 'c' and 'd', but the pointer is in the space occupied by the 'c', even its right-most extremity, the pointer will be placed between 'b' and 'c' instead. This is better than the situation in b133-b139, but still unintuitive and incorrect.

REPRODUCTION INSTRUCTIONS
-------------------------
1. Compile and run the attached testcase
2. Try to place the cursor in between any two of the characters.

Observed behaviour (b134-b139):
The cursor is not positioned in the position to left of the expected position.

Observed behaviour (b140 onwards):
The cursor is not positioned in the expected place if the mouse pointer is positioned slightly to the left of the intended position.

Expected behaviour:
The cursor should be placed in the correct place i.e. the nearest possible position to the mouse pointer's location. This is the behaviour in b133 and earlier, and in Java 6.

TESTCASE
--------
import javax.swing.*;
import java.awt.event.*;

class TextFieldTest extends JFrame {

    TextFieldTest() {
        this.add(new JTextField("abcdefg"));
        setSize(150, 55);
        addWindowListener(new WindowAdapter() {
                              public void windowClosing(WindowEvent e) { System.exit(0);}
                          });
        setVisible(true);
    }

    public static void main(String[] args) {
        new TextFieldTest();
    }
}

Comments
SUGGESTED FIX In case round parameter of the Utilities#getTabbedTextOffset method is true we should find the nearest position to the X value in the provided Segment.
24-05-2011

EVALUATION The Utilities#getTabbedTextOffset method doesn't take into account the case when the round param is true. In such case it should find the nearest position to the X value in the provided Segment, but it doesn't.
19-05-2011