Skip to content

8389970: StringIndexOutOfBoundsException when a text containing a mnemonic is removed - #2253

Open
eduardsdv wants to merge 6 commits into
openjdk:masterfrom
eduardsdv:bugfix/JDK-8389970-mnemonic-StringIndexOutOfBoundsException
Open

8389970: StringIndexOutOfBoundsException when a text containing a mnemonic is removed#2253
eduardsdv wants to merge 6 commits into
openjdk:masterfrom
eduardsdv:bugfix/JDK-8389970-mnemonic-StringIndexOutOfBoundsException

Conversation

@eduardsdv

@eduardsdv eduardsdv commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

This PR fixes the StringIndexOutOfBoundsException, that occurs when an empty text is set to a Labeled, that previously contained a mnemonic.

The reason for the error is that the updateDisplayedText(double, double), which also updates containsMnemonic flag, was not invoked if the text was empty. The value of this flag was still true but the index of the mnemonic character inside MnemonicInfo had already been updated to -1. This led to the StringIndexOutOfBoundsException in the line 611.

I fixed it by moving the call to updateDisplayedText(double, double) outside the if-clause, so that it is always called when the text is being laid out. This should not affect performance because the method already checks whether recalculation is required. If not, it exits quickly.



Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8389970: StringIndexOutOfBoundsException when a text containing a mnemonic is removed (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jfx.git pull/2253/head:pull/2253
$ git checkout pull/2253

Update a local copy of the PR:
$ git checkout pull/2253
$ git pull https://git.openjdk.org/jfx.git pull/2253/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 2253

View PR using the GUI difftool:
$ git pr show -t 2253

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jfx/pull/2253.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Aug 7, 2026

Copy link
Copy Markdown

👋 Welcome back eduardsdv! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk

openjdk Bot commented Aug 7, 2026

Copy link
Copy Markdown

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@eduardsdv eduardsdv changed the title Fix StringIndexOutOfBoundsException 8389970: Fix StringIndexOutOfBoundsException Aug 7, 2026
@openjdk openjdk Bot added the rfr Ready for review label Aug 7, 2026
@eduardsdv eduardsdv changed the title 8389970: Fix StringIndexOutOfBoundsException 8389970: StringIndexOutOfBoundsException when a text containing a mnemonic is removed Aug 7, 2026
@openjdk

openjdk Bot commented Aug 7, 2026

Copy link
Copy Markdown

The total number of required reviews for this PR has been set to 2 based on the presence of this label: rfr. This can be overridden with the /reviewers command.

@mlbridge

mlbridge Bot commented Aug 7, 2026

Copy link
Copy Markdown

Webrevs

@andy-goryachev-oracle

Copy link
Copy Markdown
Contributor

Thank you for logging the bug and offering to fix it!
The reproducer works fine with macOS; I'll test it on windows soon.

@andy-goryachev-oracle andy-goryachev-oracle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good solution.

@andy-goryachev-oracle

Copy link
Copy Markdown
Contributor

as I commented in the ticket, the mnemonic decorations are not visible on macOS so this issue does not occur there. It still needs to be tested on linux.

@arapte could you be the second reviewer please?

@eduardsdv

Copy link
Copy Markdown
Contributor Author

It would be nice to have this reviewed. Could you be the second reviewer, @arapte?

Comment on lines 551 to 560
// Have to do this just in case it needs to be recomputed
// This is especially important because it also recomputes the 'containsMnemonic' value,
// which is used by this method later on.
updateDisplayedText(w, h);

if (ignoreText) {
textWidth = textHeight = 0;
text.setText("");
} else {
updateDisplayedText(w, h); // Have to do this just in case it needs to be recomputed
textWidth = snapSizeX(Math.min(text.getLayoutBounds().getWidth(), wrapWidth));

@arapte arapte Aug 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignoreText would be true in two cases, either

With this change, updateDisplayedText() gets invoked unconditionally even for the scenario when the text is non-empty, but needs to be ignored because ContentDisplay.GRAPHIC_ONLY is true.

Please check if this can cause any issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right.
This is my approach to fix the issue by calling the updateDisplayedText() regardless of the value of ignoreText. Otherwise, the containsMnemonic value may be stale.

I added additional tests to check combinations ContentDisplay.GRAPHIC_ONLY and ContentDisplay.TEXT_ONLY with empty and null text.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When ContentDisplay.GRAPHIC_ONLY is true, and text is non-empty: Calling updateDisplayedText() may result in invoking addMnemonic(); and getChildren().add(mnemonic_underscore);. [ refer the method updateDisplayedText() , unable to share links due to Github outage ]
This can result in addition of an un-required node to scenegraph.

The proposed change fixes the exception, but it seems to be introducing a regression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps another option would be to reset the containsMnemonic flag in the if (ignoreText) block rather than calling updateDisplayedText?

@eduardsdv eduardsdv Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tested a new condition (!ignoreText) in the layoutLabelInArea() method in the following if-clause.
The test are running green and also no additional children are added in case of ContentDisplay.GRAPHIC_ONLY.

Would this be a preferable solution?

Point2D mnemonicPos = null;
double mnemonicWidth = 0.0;
double mnemonicHeight = 0.0;
if (!ignoreText && containsMnemonic) {
    final Font font = text.getFont();
    String preSt = mnemonicInfo.getText();
    boolean isRTL = (labeledNode.getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT);
    mnemonicPos = Utils.computeMnemonicPosition(font, preSt, mnemonicInfo.getMnemonicIndex(), this.wrapWidth, labeled.getLineSpacing(), isRTL);
    mnemonicWidth = Utils.computeTextWidth(font, preSt.substring(mnemonicInfo.getMnemonicIndex(), mnemonicInfo.getMnemonicIndex() + 1), 0);
    mnemonicHeight = Utils.computeTextHeight(font, "_", 0, text.getBoundsType());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be OK, but I'll defer to @arapte and @andy-goryachev-oracle

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, to keep the changes to a minimum, we only need to clear containsMnemonic in updateDisplayedText() in case of ContentDisplay.GRAPHIC_ONLY ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to keep the changes to a minimum

This is the key point.

The StringIndexOutOfBoundsException occurs because the mnemonicIndex is -1.

Since the mnemonicIndex is checked for being >= 0 in other places in javafx.scene.control.skin.LabeledSkinBase and com.sun.javafx.scene.control.skin.Util classes, I decided to move the updateDisplayedText() back behind the ignoreText condition and to add a new mnemonicIndex >=0 condition to the if-clause instead.

This avoids the error and minimizes the risk of regressions.

Point2D mnemonicPos = null;
double mnemonicWidth = 0.0;
double mnemonicHeight = 0.0;
int mnemonicIndex = mnemonicInfo != null ? mnemonicInfo.getMnemonicIndex() : -1;
if (containsMnemonic && mnemonicIndex >= 0) {
    final Font font = text.getFont();
    String preSt = mnemonicInfo.getText();
    boolean isRTL = (labeledNode.getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT);
    mnemonicPos = Utils.computeMnemonicPosition(font, preSt, mnemonicIndex, this.wrapWidth, labeled.getLineSpacing(), isRTL);
    mnemonicWidth = Utils.computeTextWidth(font, preSt.substring(mnemonicIndex, mnemonicIndex + 1), 0);
    mnemonicHeight = Utils.computeTextHeight(font, "_", 0, text.getBoundsType());
}

@eduardsdv
eduardsdv requested a review from arapte August 17, 2026 14:30
assertTrue(containsMnemonicNode.get());
}

label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we tested mnemonic behavior when content display was set to GRAPHIC_ONLY.

would it make sense to set the content display to TEXT_ONLY and re-check as a part of this test?

tk.firePulse();
assertFalse(containsMnemonicNode.get());

sl.dispose();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: to make sure the test cleans the environment even when it fails, you could create a private StageLoader stageLoader field instead of a local variable, and dispose of it if it's not null in @AfterEach.

alternatively, you'll need to dispose of it in a finally block.

import com.sun.javafx.tk.Toolkit;
import test.com.sun.javafx.scene.control.infrastructure.StageLoader;


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary blank line, please remove.


// mnemonic -> empty
label.setText("");
tk.firePulse();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we also check the mnemonic registration?
the registration should exist before L2171 and be removed after L2172

StageLoader sl = new StageLoader(label);
tk.firePulse();

label.setText("foo_bar");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting SOOB exception if I do

label.setText("test_(t)");

here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rfr Ready for review

Development

Successfully merging this pull request may close these issues.

4 participants