Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8176270

Adding ChangeListener to TextField.selectedTextProperty causes StringOutOfBoundsException

XMLWordPrintable

    • generic
    • generic

        FULL PRODUCT VERSION :
        java version "1.8.0_102"
        Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
        Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Linux engel-cc300-virtualbox 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        Accessing the selectedTextProperty of the TextField control during a ChangeListener or InvalidationListener may cause a StringOutOfBoundsException.
        Please see the attached Test code for details. (Note: the tests depend on TestFX 4.0.4-alpha)




        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        java.lang.StringIndexOutOfBoundsException: String index out of range: 9
        at java.lang.String.substring(String.java:1963)
        at javafx.scene.control.TextInputControl$2.computeValue(TextInputControl.java:164)
        at javafx.beans.binding.StringBinding.get(StringBinding.java:152)
        at javafx.beans.binding.StringBinding.get(StringBinding.java:61)
        at javafx.beans.binding.StringExpression.getValue(StringExpression.java:51)
        at javafx.beans.binding.StringExpression.getValue(StringExpression.java:47)
        at javafx.beans.property.StringPropertyBase.get(StringPropertyBase.java:130)
        at javafx.beans.property.ReadOnlyStringWrapper$ReadOnlyPropertyImpl.get(ReadOnlyStringWrapper.java:111)
        at javafx.beans.property.ReadOnlyStringWrapper$ReadOnlyPropertyImpl.get(ReadOnlyStringWrapper.java:107)
        at javafx.beans.property.ReadOnlyStringProperty.toString(ReadOnlyStringProperty.java:64)
        at test.TextFieldIssueTest$1.invalidated(TextFieldIssueTest.java:81)
        at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
        at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
        at javafx.beans.property.ReadOnlyStringPropertyBase.fireValueChangedEvent(ReadOnlyStringPropertyBase.java:72)
        at javafx.beans.property.ReadOnlyStringWrapper.fireValueChangedEvent(ReadOnlyStringWrapper.java:103)
        at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
        at javafx.beans.property.StringPropertyBase.access$000(StringPropertyBase.java:49)
        at javafx.beans.property.StringPropertyBase$Listener.invalidated(StringPropertyBase.java:230)
        at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
        at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
        at javafx.beans.binding.StringBinding.invalidate(StringBinding.java:171)
        at com.sun.javafx.binding.BindingHelperObserver.invalidated(BindingHelperObserver.java:51)
        at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
        at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
        at javafx.scene.control.TextInputControl$TextProperty.fireValueChangedEvent(TextInputControl.java:1389)
        at javafx.scene.control.TextInputControl$TextProperty.markInvalid(TextInputControl.java:1393)
        at javafx.scene.control.TextInputControl$TextProperty.controlContentHasChanged(TextInputControl.java:1332)
        at javafx.scene.control.TextInputControl$TextProperty.access$1600(TextInputControl.java:1300)
        at javafx.scene.control.TextInputControl.lambda$new$162(TextInputControl.java:139)
        at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
        at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
        at javafx.scene.control.TextField$TextFieldContent.insert(TextField.java:87)
        at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:1204)
        at javafx.scene.control.TextInputControl.updateContent(TextInputControl.java:556)
        at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:548)
        at com.sun.javafx.scene.control.skin.TextFieldSkin.replaceText(TextFieldSkin.java:576)
        at com.sun.javafx.scene.control.behavior.TextFieldBehavior.replaceText(TextFieldBehavior.java:202)
        at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.defaultKeyTyped(TextInputControlBehavior.java:238)
        at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(TextInputControlBehavior.java:139)
        at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)
        at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(TextInputControlBehavior.java:127)
        at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$74(BehaviorBase.java:135)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
        at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
        at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
        at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:216)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:148)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(GlassViewEventHandler.java:247)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:246)
        at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
        at com.sun.glass.ui.View.notifyKey(View.java:966)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
        at java.lang.Thread.run(Thread.java:745)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package test;

        import java.util.logging.Level;
        import java.util.logging.Logger;

        import org.junit.Test;
        import org.testfx.framework.junit.ApplicationTest;
        import org.testfx.util.WaitForAsyncUtils;

        import javafx.application.Platform;
        import javafx.beans.InvalidationListener;
        import javafx.beans.Observable;
        import javafx.beans.value.ObservableValue;
        import javafx.scene.Scene;
        import javafx.scene.control.TextField;
        import javafx.scene.control.TextInputControl;
        import javafx.scene.input.KeyCode;
        import javafx.scene.layout.VBox;
        import javafx.stage.Stage;

        public class TextFieldIssueTest extends ApplicationTest
        {

          private final static Logger LOGGER = Logger.getLogger(TextFieldIssueTest.class.getName());
          static
          {
            Thread.setDefaultUncaughtExceptionHandler(TextFieldIssueTest::handleError);
          }

          private static Throwable unchaughtException;
          private TextInputControl textField;

          private static void handleError(Thread t, Throwable e)
          {
            unchaughtException = e;
            LOGGER.log(Level.SEVERE, "Unchaught exception observed!", e);
          }

          @Override
          public void start(Stage stage) throws Exception
          {
            // reset exception
            unchaughtException = null;

            VBox vBox = new VBox();
            textField = new TextField();
            textField.setText("1234 5678");
            vBox.getChildren()
                .add(textField);

            Scene scene = new Scene(vBox);
            stage.setScene(scene);
            stage.show();
          }

          @Test
          public void test_fail_case01()
          {
            LOGGER.info("Running test 'fail_case01'!");
            textField.selectedTextProperty()
                .addListener(this::handleSelectionChanged);

            doSelectAndReplace();
          }

          @Test
          public void test_fail_case02()
          {
            LOGGER.info("Running test 'fail_case02'!");
            textField.selectedTextProperty()
                .addListener(new InvalidationListener()
                {
                  @Override
                  public void invalidated(Observable observable)
                  {
                    // accessing the selectedTextProperty causes a
                    // StringOutOfBoundsException
                    observable.toString();
                  }
                });
            doSelectAndReplace();
          }

          @Test
          public void test_fail_workaround()
          {
            LOGGER.info("Running test 'fail_case02'!");
            textField.selectedTextProperty()
                .addListener(new InvalidationListener()
                {
                  @Override
                  public void invalidated(Observable observable)
                  {
                    // workaround: ensure that state of selected text property is
                    // accessed later
                    Platform.runLater(() -> observable.toString());
                  }
                });

            doSelectAndReplace();
          }

          private void handleSelectionChanged(ObservableValue<? extends String> observable, String oldValue, String newValue)
          {
            // internally the selectedTextProperty is accessed to get the current value
            // from which
            // causes the StringOutOfBoundsException
          }

          private void doSelectAndReplace()
          {
            textField.positionCaret(5);
            WaitForAsyncUtils.waitForFxEvents();

            // select 2nd word
            textField.selectNextWord();
            WaitForAsyncUtils.waitForFxEvents();

            // replace selection
            type(KeyCode.DIGIT0);
            WaitForAsyncUtils.waitForFxEvents();

            assert (unchaughtException == null);
          }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        use InvalidationListener instead of ChangeListener and encapsulate access to selectedTextProperty in Platform.runLater (see provided test cases)

              rlichten Robert Lichtenberger
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: