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

TextArea.getSelectedText(), getSelectionStart(), getSelectionEnd() return incorr

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1
    • client-libs
    • None
    • x86
    • windows_nt



      Name: mc57594 Date: 02/07/97


      When retrieving selected text from a TextArea, getSelectedText(),
       getSelectionStart(),
      and getSelectionEnd() return incorrect values. The values are
      generally down one line farther in the text than the actual
      selected text.

      The following code (which works correctly on JDK 1.0.2) gets the
      currently selected text in a textfield named ta, and makes sure
      that the entire line of text is selected.

          /**
           * Used to make sure that full lines are obtained from the selection
           */
          private String findSelection() {
              int start = ta.getSelectionStart();
              int end = ta.getSelectionEnd();
              String text = ta.getText();
          System.out.println(start+" "+end+"\n"+ta.getSelectedText());
              if(start < 0) start = 0;
              if(end >= text.length() || end == -1) end = text.length()-1;
              start = text.lastIndexOf('\n', start)+1;
              if(text.charAt(start) == '\r') start++;
              end = text.indexOf('\n', end);
              if(start < 0) start = 0;
              if(end >= text.length() || end == -1) end = text.length()-1;
              ta.select(start, end);
          System.out.println(start+" "+end+"\n"+ta.getSelectedText());
              return ta.getSelectedText();
          }

      company - John Deere, WWAG Division , email - ###@###.###
      ======================================================================

            ehawkessunw Eric Hawkes (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: