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

TextArea.setCaretPosition() does not work properly if textarea contains newline

XMLWordPrintable

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



      Name: sgC58550 Date: 06/26/97


      // this small source illustrates the apparent bug; this makes it difficult to
      do text editing; also it does not seem to be bossible to set the caret to the end of text;
      this also seems to be off by the number of newline characters above it



      /* from ###@###.###
      setCaretPosition() method in awt.TextArea doesn't work properly when the
      textarea contains \n or other end-of-line characters; position seems to be
      off by the number of newlines above the desired index position; this source
      illustrates the problem . getText() and String.IndexOf() both seem to work
      properly.
      */

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
           import java.applet.Applet;

      public class Frame1 extends Frame {
                 TextArea textArea1 ;
              static public void main(String args[]) {
                   Frame1 f1 = new Frame1();
                   f1.setBounds(20,20,700,430);
                  f1.show();
              }
              public Frame1() {
                 addWindowListener(new w111());
                              textArea1 = new TextArea(5,25);
                      textArea1.setBounds(5,5,235,170);
              setLayout(new BorderLayout());
                      add(textArea1,"Center");
                      show();

                    {
                    String trial1 = "012345\n\n\n\n6789";
                    String trial2 = "abcdef\n\n\n\nghijk";
                    String trial3 = "12345678\n\n\nabcd\n\n";
        textArea1.setText("");
        textArea1.append(trial1);
        textArea1.append(trial2 );
        textArea1.append(trial3 );
        String string4 = trial1 + trial2 + trial3 ;
       System.out.println( textArea1.getText().length());
       int cv1 = string4.indexOf("678");
       System.out.println("\n index of 678 in original string is " + cv1);

       int cv = textArea1.getText().indexOf("678");
       System.out.println("\n index of 678 in textarea is " + cv);

       System.out.println(" index of second 678 in textarea is " +
        textArea1.getText().indexOf("678",cv+1));
          textArea1.requestFocus();
       // index is correct but setCaretPosition is off by the number of
       // \n characters above it
       textArea1.append("\n caret should be at the first 6 " );
          textArea1.setCaretPosition( textArea1.getText().indexOf("678"));
          // the following doesn't work either
          //textArea1.setCaretPosition( textArea1.getText().length());
              } } }
      class w111 extends WindowAdapter {
      public void windowClosing(WindowEvent winev){
      Window window = (Window)winev.getSource();
      window.dispose();
      System.exit(0);
      }
      }



      company - , email - ###@###.###
      ======================================================================

            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: