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.
======================================================================