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

TextArea.getSelectedText() throws StringIndexOutOfBoundsException

XMLWordPrintable

    • x86
    • windows_nt



      Name: agR10216 Date: 06/06/2002

      ###@###.###
      Run following test, press <Shift+End> to select whole line
      and press the button to retrieve selected text.
      StringIndexOutOfBoundsException will be thrown.

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


      public class Test extends Frame {
          public TextArea ta;

          public Test() {
              ta = new TextArea();
              ta.setText("ABCDEFGH");
              add(ta, BorderLayout.CENTER);

              Button but = new Button("Print selection");
              but.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      System.err.println(ta.getSelectedText());
                  }
              });
              add(but, BorderLayout.SOUTH);

              addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent we) {
                      System.exit(0);
                  }
              });
              setBounds(0, 0, 200, 200);
          }

          public static void main(String[] args) {
              new Test().setVisible(true);
          }
      }

      -----------------------------------------------------------

      Here is the output.
      -----------------------------------------------------------
      java.lang.StringIndexOutOfBoundsException: String index out of range: 9
              at java.lang.String.substring(String.java:1477)
              at java.awt.TextComponent.getSelectedText(TextComponent.java:224)
              at Test$1.actionPerformed(Test.java:16)
              at java.awt.Button.processActionEvent(Button.java:381)
              at java.awt.Button.processEvent(Button.java:350)
              at java.awt.Component.dispatchEventImpl(Component.java:3598)
              at java.awt.Component.dispatchEvent(Component.java:3439)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
              at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
      read.java:197)
              at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
      ad.java:150)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)

              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

              at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
      -----------------------------------------------------------

      ###@###.### 2002-06-06
      ======================================================================

            kdmsunw Kdm Kdm (Inactive)
            agerasimsunw Alexander Gerasimov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: