On Mon, 25 May 2026 17:42:02 GMT, Jeremy Wood <[email protected]> wrote:

>> Calling `getAccessibleContext()` may in some cases replace the field 
>> `accessibleContext`. So we should use `getAccessibleContext()` here to make 
>> sure sure we have the most up-to-date AccessibleContext / DocumentListener.
>> 
>> 
>> ### Other Related Consideration
>> 
>> The `setDocument()` method includes analogous logic for 
>> `inputMethodRequestsHandler`. However `inputMethodRequestsHandler` is never 
>> reassigned outside of the JTextComponent class, so that shouldn't require a 
>> similar fix/change anytime soon.
>> 
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Jeremy Wood has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   8385367: make sure new DocumentListener is only added once
>   
>   In getAccessibleContext() when we create a new AccessibleContext: we 
> immediately add it as a DocumentListener to our Document.
>   
>   So in that case: setDocument(..) doesn't need to add a DocumentListener.

src/java.desktop/share/classes/javax/swing/text/JTextComponent.java line 460:

> 458:             if (accessibleContext == getAccessibleContext()) {
> 459:                 model.addDocumentListener(
> 460:                         
> ((AccessibleJTextComponent)getAccessibleContext()));

I guess this will fix it, but consider that JEditorPane is an odd one, and 
perhaps if it doesn't always have a AccessibleJEditorPane as its accessible 
context, that maybe it should be over-riding setDocument to do the unusual 
thing. 
Not asking you to jump on that, as this is the simpler fix but to think about 
it.

Also this new code can effectively remove the listener. If we start with 
HTMLEditor and set a styled doc so get a different AC returned, this skips 
installing the listener. But by calling getAccessibleContext() you've ensured 
accessibleContext is not null .. and in such a case, we'd install the listener.
Remember we are only in this block because someone must have previously 
initialized accessibleContext

But I don't understand the thinking behind why the existing code only installs 
the listener if no one has called getAccessibleContext() so maybe I'm missing 
something.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/31271#discussion_r3358850956

Reply via email to