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

JTextComponent.getText(int,int) throws unexpected StringIndexOutOfBoundsExceptio

XMLWordPrintable

    • kestrel
    • sparc
    • solaris_2.5.1



      Name: aaC67449 Date: 01/06/99


      JTextComponent.getText(int pos,int len) throws unexpected StringIndexOutOfBoundsException, when len<0, but should throw BadLocationException instead.

      javadoc says:"
      public String getText(int offs,
                            int len)
                     throws BadLocationException

            Fetches a portion of the text represented by the component. Returns an empty string if length is 0.
            Parameters:
                  offs - the offset >= 0
                  len - the length >= 0
            Returns:
                  the text
            Throws:
                  BadLocationException - if the offset or length are invalid

      "

      áccordingly, this method does not work correctly in subclasses (JTextField, JPasswordField)

      See example.


      --------------- Example---------------------------
      import javax.swing.text.*;

      public class Test {
          public static void main(String argv[]) {
             JTextField c= new JTextField();
             c.setText("Test");
             try {
                 c.getText(0,-2);
             } catch (javax.swing.text.BadLocationException e) {
                  System.out.println("Ok");
             }
          }
      }

      ----------------output------------------
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
              at java.lang.String.<init>(String.java:237)
              at javax.swing.text.GapContent.getString(GapContent.java:174)
              at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:498)
              at javax.swing.text.JTextComponent.getText(JTextComponent.java:798)
              at Test.main(Test.java:9)


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

            svioletsunw Scott Violet (Inactive)
            aalievsunw Artem Aliev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: