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

The caret position of a TextComponent is wrong (counting the '\r's too).

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.2, 1.1.3
    • client-libs
    • None
    • x86
    • windows_95



      Name: el35337 Date: 07/24/97


      The getCaretPosition returns wrong values for TextComponents.
      The value is should_be_value + numberOf(\n).
      The reason might be the internal storage of Windows edit controls
      (storing a '\n' AND a '\r' for every new line). On Solaris
      the position is correct.
      Example, text content:

      xx <- invisible '\n'
      | <- Cursor is here

      getCaretPosition should be (and it is on Solaris) 3 but it's 4
      on Windows 95.
      For every additional line the gap increases by one.

      This problem can be tested with every (multi line) TextComponent.
      company - Duisburg University Mathematics / Computer Science , email - ###@###.###
      ======================================================================


      roger.lewis@Eng 1997-07-25

      In JDK1.1.3/Win95, TextComponentPeer.getCarentPosition()
      does not properly convert carrige returns and line
      feeds before returning the caret position. So,
      for each new line between the beginning of the
      text and the actual caret position, the caret
      position gets an extra, erroneous count.

      For example, for the text "Line 1\nLine 2\nLine 3",
      if the caret is positioned after the 3,
      getCaretPosition() returns 22 instead of 20.
      Or, if the caret is position at the beginning of
      line 2, getCaretPosition() return 8 instead of 7.

      Here's the source to a java application to
      demonstrate the problem.

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

      public class TextAreaBug implements ActionListener
      {
          Frame frame;
          TextArea textarea;
          Button button;

          public TextAreaBug()
          {
              frame = new Frame();
              textarea = new TextArea(
                  "Line 1\nLine 2\nLine 3",
                  10,
                  5,
                  TextArea.SCROLLBARS_NONE );
              button = new Button("Click for caret position.");

              frame.add(button, "North");
              frame.add(textarea, "Center");
              frame.setSize(150,150);
              frame.show();

              button.addActionListener(this);
          }

          static public void main(String args[])
          {
              new TextAreaBug();
          }

          public void actionPerformed(ActionEvent e)
          {
              button.setLabel("Caret position = " +
                  textarea.getCaretPosition());
              textarea.requestFocus();
          }
      }
      ----- end source -----

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

            ehawkessunw Eric Hawkes (Inactive)
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: