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

Incorrect indexing in TextArea when using indexOf()

XMLWordPrintable

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



      Name: sgC58550 Date: 05/08/97


      Create a TextArea, determine position of substring using indexOf(),
      ^Dse the returned index in eg. replaceWith or TextArea.select().^W
      In the first line of the TextArea it will work OK.....
      In every next line an extra difference between the meant position
      and the result occurs. It seems to me that indexOf() doesn't count
      any carriage returns and all the other functions does.
      Try to type three lines of text in the
      TextEditTest program eg. this is a test [enter] it doesn't work
      [enter] what's the problem[enter].
      Now, if you try to replace it (second line) with "this" it won't
      work correct !! If you would try to do the same thing in the
      third line the result would be worse.....

      The following piece of code will demonstrate the problem:

      import java.awt.*;

      public class TextEditTest extends Frame {

      public TextEditTest() {
      setTitle("TextEditTest");
      Panel p = new Panel();
      p.setLayout(new FlowLayout());
      p.add(new Button("Replace"));
      from = new TextField(10);
      p.add(from);
      p.add(new Label("with"));
      to = new TextField(10);
      p.add(to);
      add("South", p);
      ta = new TextArea(8, 40);
      add("Center", ta);
      }

      public boolean handleEvent(Event evt) {
      if (evt.id == Event.WINDOW_DESTROY)
      System.exit(0);
      return super.handleEvent(evt);
      }

      public boolean action(Event evt, Object arg) {
      if (arg.equals("Replace")) {
      String f = from.getText();
      int n = ta.getText().indexOf(f);
      if (n >= 0 && f.length() >0)
      ta.replaceText(to.getText(), n, n + f.length());
      }
      else {
      return super.action(evt, arg);
      }
      return true;
      }

      public static void main(String[] args) {
      Frame f = new TextEditTest();
      f.setSize(300, 200);
      f.setVisible(true);
      }

      private TextArea ta;
      private TextField from, to;
      }



      company - Columbus Group Communications Inc.
      ======================================================================

            ehawkessunw Eric Hawkes (Inactive)
            sgoodsunw Sheri Good (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: