RFR: 8360070: AccessibleText.getBeforeIndex returns null for last character [v3]

Abhishek Kumar abhiscxk at openjdk.org
Wed Aug 6 07:18:04 UTC 2025


On Wed, 23 Jul 2025 19:44:44 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:

> At the same time, I agree that all implementations of AccessibleText need updating to be consistent.

I checked with the implementation of `getAtIndex, getBeforeIndex and getAfterIndex` APIs and these UI components (JSpinner, JPasswordField, JTextComponent, JLabel, AbstractButton, ProgressMonitor classes) implement them. JSpinner, JPasswordField internally depends on the JTextComponent for the actual implementation as the Accessible Text object is an instance of Accessible JTextComponent. Similarly, ProgressMonitor depends on the JLabel class implementation.

JLabel and AbstractButton support these methods only if the  text is html and not plain text. 

I tried with html based JLabel and analyze the result returned by the getAtIndex API.
If a JLabel has the html text `<html>This is <b>Bold</b> text</html>`, then the result shown by getAtIndex for CHARACTER is:

      Index 0 : 

      Index 1 : T
      Index 2 : h
      Index 3 : i
      Index 4 : s
      Index 5 :  
      Index 6 : i
      Index 7 : s
      Index 8 :  
      Index 9 : B
      Index 10 : o
      Index 11 : l
      Index 12 : d
      Index 13 :  
      Index 14 : t
      Index 15 : e
      Index 16 : x
      Index 17 : t

This seems wrong to me, I expect the character at index **0** should be **T**. 

The array layout from the AbstractDocument class after html tags removal shows
<img width="1333" height="809" alt="image" src="https://github.com/user-attachments/assets/8dfeeec0-da78-4b9b-8b64-19f78ee90724" />


I don't understand **why there is a newline character at the start of the JLabel's text** after removing the html tags.

`getCharCount` method return the number of characters (valid indices) as 18, this includes the newline character at index 0 and trims the last newline character.

I think the new line character shouldn't be considered, otherwise, the results returned from get*Index APIs looks incorrect.

@aivanov-jdk As per my opinion, updating Accessible Text implementation for JLabel and AbstractButton can be taken up as a separate issue.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/25941#issuecomment-3157688225


More information about the client-libs-dev mailing list