JDK-8098258 : [Text] Hitting an empty Text returns insertion index out of text bounds
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: 8u20
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-07-03
  • Updated: 2015-06-12
  • Resolved: 2014-07-03
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 8
8u40Fixed
Related Reports
Relates :  
Description
Hitting an empty Text node, i.e. a Text whose test is an empty string, returns insertion index 1, which is out of text bounds. The only valid position within an empty string is 0. The test case is below.

I realize that impl_* methods are not public API, but I'm using them in RichTextFX [1] anyway because there's no public API for this functionality. If you are planning/willing to expose more public API on Text/TextFlow, I will be happy to summarize what private API is currently used in RichTextFX.

The workaround is to check for empty text and treat it specially (i.e. don't call impl_hitTestChar on an empty text at all).


import javafx.geometry.Point2D;
import javafx.scene.text.Text;

import com.sun.javafx.scene.text.HitInfo;

public class HitEmptyText {

    public static void main(String[] args) {
        Text text = new Text("");
        HitInfo hit = text.impl_hitTestChar(new Point2D(1, 1));
        int insertionIndex = hit.getInsertionIndex();
        System.out.println(hit);
        System.out.println("insertion index: " + insertionIndex); // prints 1
        assert insertionIndex == 0; // because there's no other valid position
    }

}


[1] https://github.com/TomasMikula/RichTextFX
Comments
http://hg.openjdk.java.net/openjfx/8u-dev/rt/rev/d205ff5593c2
03-07-2014

changeset: 7420:d205ff5593c2 tag: tip user: Felipe Heidrich <felipe.heidrich@oracle.com> date: Thu Jul 03 16:45:57 2014 -0700 summary: RT-37801: [Text] Hitting an empty Text returns insertion index out of text bounds
03-07-2014

Felipe to evaluate. Perhaps for 9?
03-07-2014