JDK-4116436 : StringIndexOutOfBoundsException with getSelectedText()
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 2.0,unknown,1.1,1.1.4
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_2.5.1,solaris_2.6
  • CPU: generic,sparc
  • Submitted: 1998-03-02
  • Updated: 1999-07-20
  • Resolved: 1999-07-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other Other
1.1.8 1.1.8Fixed 1.2.2Fixed
Related Reports
Duplicate :  
Description

Name: mf23781			Date: 03/02/98



A simplified testcase down to the following

import java.awt.*;
import java.awt.event.*;

public class BpsTextArea extends TextArea
{
  public BpsTextArea( int rows, int columns )
  {
    super( rows, columns );
    addKeyListener( new KeyAdapter()
    {
      public void keyPressed( KeyEvent e )
      {
        if ( e.getKeyCode() != KeyEvent.VK_BACK_SPACE )
        {
          System.out.println ("getSelectedText().length() = "
                             + getSelectedText().length());
        }
      } 
    } );
  }

  public static final void main( String[] args )
  {
    java.awt.Frame frame = new java.awt.Frame();
    BpsTextArea textArea = new BpsTextArea( 10, 12 );
    textArea.setText( "mm\nmm" );
    frame.add( "Center", textArea );
    frame.pack();
    frame.show();
  }
}

To reproduce the problem select all the text by dragging the
cursor from the start to the end and then press the backspace
key to delete all the text. Press any key and you will get a
java.lang.StringIndexOutOfBoundsException. Dragging the mouse 
button anywhere over the text area will stop the exception
(This effectively resets the selection to 0) 

The problem appears to stem from the fact that getSelectedText()
calls getSelectionStart() and if there is no selected text then
this returns the cursor position. (I assume this is done in
sun_awt_motif_MTextAreaPeer_getSelectionStart in awt_TextArea.c
with the call XmTextGetCursorPosition(tdata->txt);

If the text is selected by dragging the mouse from the start 
to the end of the text then the Cursor position will be set to
the end of the string. For some reason if the selection is 
deleted using the backspace key then the Cursor position does
not get reset so that getSelectionStart returns the old 
cursor position rather then 0 as expected.



======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.1.8 FIXED IN: 1.1.8 1.2.2 INTEGRATED IN: 1.1.8 1.2.2 VERIFIED IN: 1.1.8
14-06-2004

WORK AROUND Name: mf23781 Date: 03/02/98 Catch the StringIndexOutOfBoundsException and discard it. ======================================================================
11-06-2004

EVALUATION eric.hawkes@eng 1998-05-20 This bug occurs on 1.2beta4 as well. Backspace isn't the only problem - DELETE and CUT cause the same exceptions to be thrown (for the same reasons, I'm sure). No exception is thrown under winNT. This bug is verified as fixed in jdk118f via regression testing. john.s.lee@Eng 1999-01-22
22-01-1999

SUGGESTED FIX After selecting text from left to right, and then deleting it, XmTextGetSelectionPosition returns a zero length selection, but at a position of the end of the previous selection. In this case we should use XmTextGetCursorPosition instead. I will apply the same fix to awt_TextArea.c and awt_TextField.c. The fix is also required on 1.2. Diffs: (diff src/solaris/sun/awt_TextArea.c 1.40 1.41) 2c2 < * @(#)awt_TextArea.c 1.40 98/07/15 --- > * @(#)awt_TextArea.c 1.41 98/09/30 280c280,281 < if (XmTextGetSelectionPosition(tdata->txt, &start, &end)) { --- > if (XmTextGetSelectionPosition(tdata->txt, &start, &end) > && (start!=end)) { 302c303,304 < if (XmTextGetSelectionPosition(tdata->txt, &start, &end)) { --- > if (XmTextGetSelectionPosition(tdata->txt, &start, &end) > && (start!=end)) { (diff src/solaris/sun/awt_TextField.c 1.50 1.51) 2c2 < * @(#)awt_TextField.c 1.50 98/07/15 --- > * @(#)awt_TextField.c 1.51 98/09/30 551c551,552 < if (XmTextGetSelectionPosition(tdata->widget, &start, &end)) { --- > if (XmTextGetSelectionPosition(tdata->widget, &start, &end) > && (start!=end)) { 573c574,575 < if (XmTextGetSelectionPosition(tdata->widget, &start, &end)) { --- > if (XmTextGetSelectionPosition(tdata->widget, &start, &end) > && (start!=end)) { Testcase: /* * Licensed Materials - Property of IBM * * GetSelectedTextApplet.java * * (C) Copyright IBM Corporation 1998 All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ /* @test %I% %E% @bug 4116436 @summary StringOutOfBoundsException with getSelectedText() @run applet/manual=done GetSelectedText.html @author Stuart Lawrence */ /*To look for the problem select all the text by dragging the cursor from the start to the end and then press the backspace key to delete all the text. Press any key and you will get a java.lang.StringIndexOutOfBoundsException. Dragging the mouse button anywhere over the text area will stop the exception (This effectively resets the selection to 0) . */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class GetSelectedTextApplet extends Applet implements KeyListener { TextArea ta; boolean exceptionRaised = false; public void init() { setLayout(new BorderLayout()); ta = new TextArea("Select all of this text"); add("Center", ta); ta.addKeyListener(this); } public void keyPressed(KeyEvent ke) { if (ke.getKeyCode() != KeyEvent.VK_BACK_SPACE) { try { System.out.println("getSelectedText().length() = " + (ta.getSelectedText()).length()); } catch(StringIndexOutOfBoundsException se) { System.out.println(se); exceptionRaised = true; } } } public void keyTyped(KeyEvent ke) {} public void keyReleased(KeyEvent ke) {} public void destroy(){ if (exceptionRaised) throw new RuntimeException("Test Failed: caught StringIndexOutOfBoundsException"); } } <HTML> <BODY> Select all of the text in the text area, from left to right. Then delete it using the delete or backspace key. Press any key to add new text in to the text area. If an exception is not thrown the test has passed. <APPLET code="GetSelectedTextApplet.class" width=200 height=200> </APPLET> </BODY> </HTML> matthew.chapman@eng 1998-09-30
30-09-1998