-
Bug
-
Resolution: Unresolved
-
P4
-
24
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
The AccessibleText.getBeforeIndex(int part, int index) method, when used with AccessibleText.CHARACTER as the part and the text's total length (i.e., the index immediately after the last character) as the index, is expected to return the character immediately preceding the given index. However, for a JTextField, this method incorrectly returns null instead of the last character.
Upon examining the internal implementation of getBeforeIndex and its delegate method getAtIndex, the root cause becomes clear. The getBeforeIndex method calls getAtIndex(part, index, -1). Inside getAtIndex, there is an initial boundary check:
if (index < 0 || index >= model.getLength()) {
return null;
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
return last character
ACTUAL -
return null
---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.accessibility.*;
public class AccessBug {
public static void main(String[] args) throws Exception {
JTextField textField = new JTextField("Test1 Test2 Test3. Test4 Test5. Test6");
AccessibleContext accessibleContext = textField.getAccessibleContext();
AccessibleText accessibleText = accessibleContext.getAccessibleText();
if (accessibleText != null) {
String text = textField.getText();
int textLength = text.length();
String result = accessibleText.getBeforeIndex(AccessibleText.CHARACTER, textLength);
System.out.println("Text: \"" + text + "\"");
System.out.println("Text Length: " + textLength);
System.out.println("Call: getBeforeIndex(CHARACTER, " + textLength + ")");
System.out.println("Expected result: '6'");
System.out.println("Actual result: '" + result + "'");
if (!"6".equals(result)) {
throw new RuntimeException("The actual result does not match the expected result.");
} else {
System.out.println("\nTest Passed.");
}
}
}
}
---------- END SOURCE ----------
The AccessibleText.getBeforeIndex(int part, int index) method, when used with AccessibleText.CHARACTER as the part and the text's total length (i.e., the index immediately after the last character) as the index, is expected to return the character immediately preceding the given index. However, for a JTextField, this method incorrectly returns null instead of the last character.
Upon examining the internal implementation of getBeforeIndex and its delegate method getAtIndex, the root cause becomes clear. The getBeforeIndex method calls getAtIndex(part, index, -1). Inside getAtIndex, there is an initial boundary check:
if (index < 0 || index >= model.getLength()) {
return null;
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
return last character
ACTUAL -
return null
---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.accessibility.*;
public class AccessBug {
public static void main(String[] args) throws Exception {
JTextField textField = new JTextField("Test1 Test2 Test3. Test4 Test5. Test6");
AccessibleContext accessibleContext = textField.getAccessibleContext();
AccessibleText accessibleText = accessibleContext.getAccessibleText();
if (accessibleText != null) {
String text = textField.getText();
int textLength = text.length();
String result = accessibleText.getBeforeIndex(AccessibleText.CHARACTER, textLength);
System.out.println("Text: \"" + text + "\"");
System.out.println("Text Length: " + textLength);
System.out.println("Call: getBeforeIndex(CHARACTER, " + textLength + ")");
System.out.println("Expected result: '6'");
System.out.println("Actual result: '" + result + "'");
if (!"6".equals(result)) {
throw new RuntimeException("The actual result does not match the expected result.");
} else {
System.out.println("\nTest Passed.");
}
}
}
}
---------- END SOURCE ----------
- links to
-
Review(master) openjdk/jdk/25941