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

StringIndexOutOfBoundsException is thrown on replacing text in TextArea while having listener added to selectedTextProperty

    XMLWordPrintable

Details

    • generic
    • generic

    Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]
      Windows 7 Professional

      A DESCRIPTION OF THE PROBLEM :
      StringIndexOutOfBoundsException is thrown on replacing text in TextArea while having invalidation or change listener added to textArea.selectedTextProperty().

      This is bad because client application has thread uncaught exception handler, that displays this exception in face of the client, when this is occurring.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I have added source code to test example that replicates this, but if you want to replicate from scratch do below:

      1. Add TextArea to component.
      2. Add listener to TextArea's selectedTextProperty().
      3. Run application, write string that looks like below:

      text text [start

      end]

      4. Start selection form "[start" and end it behind "end]".
      5. Press "d" or any other letter to replace selection.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After 5. "[start\n\nend]" is replaced by letter you have entered.
      ACTUAL -
      After 5. exception is thrown.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "JavaFX Application Thread" java.lang.StringIndexOutOfBoundsException: String index out of range: 27
      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 java.lang.String.valueOf(String.java:2994)
      at java.io.PrintStream.println(PrintStream.java:821)
      at sample.TextAreaTest.lambda$createScrollPane$0(TextAreaTest.java:43)
      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.TextArea$TextAreaContent.insert(TextArea.java:207)
      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 sample.TextAreaTest$1.replaceText(TextAreaTest.java:36)
      at com.sun.javafx.scene.control.behavior.TextAreaBehavior.replaceText(TextAreaBehavior.java:305)
      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.TextAreaBehavior.callAction(TextAreaBehavior.java:259)
      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:217)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(GlassViewEventHandler.java:248)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
      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.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
      at java.lang.Thread.run(Thread.java:748)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package sample;

      import javafx.application.Application;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.TextArea;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class TextAreaTest extends Application
      {
          public static void main( String[] args )
          {
              Application.launch( args );
          }

          @Override
          public void start( Stage stage )
          {
              System.err.println( System.getProperty( "javafx.runtime.version" ) );
              Parent content = createScrollPane();
              Scene scene = new Scene( content, 800, 600 );
              stage.setScene( scene );
              stage.show();
          }

          public Parent createScrollPane()
          {
              final BorderPane aBorderPane = new BorderPane();
              final TextArea aTextArea = new TextArea( "text text text [start\n\nend]" );
              aBorderPane.setCenter( aTextArea );

              aTextArea.selectedTextProperty().addListener( ( aObservable, aOldValue, aNewValue ) ->
              {
                  System.out.println( aNewValue );
              } );

              return aBorderPane;
          }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      final TextArea aTextArea = new TextArea( "text text text [start\n\nend]" )
      {
          @Override
          public void replaceText( final int start, final int end, final String text )
          {
              deselect();
              super.replaceText( start, end, text );
          }
      };

      Attachments

        Issue Links

          Activity

            People

              pmangal Priyanka Mangal (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: