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

Caret.getMagicCaretPosition returns incorrect position

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Run within Eclipse, not tested outside Eclipse

      A DESCRIPTION OF THE PROBLEM :
      The Point returned by Caret.getMagicCaretPosition() is incorect when called on a JTextPane that is inside a JScrollPane when the up and down arrow keys are used to navigate through the text inside the JTextArea.

      Please note that I see this behaviour only with the up and down arrow keys, not when the left and right arrow keys are pressed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run attached class
      2. Type up-arrow three times
      3. The output on the console is:

      keyPressed getMagicCaretPosition = java.awt.Point[x=18,y=64]
      keyPressed getMagicCaretPosition = java.awt.Point[x=18,y=64]
      keyPressed getMagicCaretPosition = java.awt.Point[x=18,y=64]

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      keyPressed getMagicCaretPosition = java.awt.Point[x=21,y=48]
      keyPressed getMagicCaretPosition = java.awt.Point[x=28,y=32]
      keyPressed getMagicCaretPosition = java.awt.Point[x=19,y=16]
      ACTUAL -
      keyPressed getMagicCaretPosition = java.awt.Point[x=18,y=64]
      keyPressed getMagicCaretPosition = java.awt.Point[x=18,y=64]
      keyPressed getMagicCaretPosition = java.awt.Point[x=18,y=64]

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.tf.trans.test;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.HeadlessException;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;

      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;

      public class BugReport extends JFrame {
          JTextArea editor;


          public BugReport(String arg0) throws HeadlessException {
              super(arg0);
          }


          public static void main(String[] args) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      try {
                          BugReport mainFrame = new BugReport("getMagicCaretPosition Bug");
                          mainFrame.createAndShowGUI();
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              });
          }


          private void createAndShowGUI() {
              JFrame.setDefaultLookAndFeelDecorated(true);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              rootPane.getContentPane().setLayout(new BorderLayout());

              editor = new JTextArea();
              editor.setText("one\ntwo\nthree\nfour\nfive");
              editor.addKeyListener(new KeyListener() {
                  public void keyTyped(KeyEvent arg0) {
                  }


                  public void keyPressed(KeyEvent arg0) {
                  }


                  public void keyReleased(KeyEvent arg0) {
                      System.out.println("keyPressed getMagicCaretPosition = "
                              + editor.getCaret().getMagicCaretPosition());
                  }
              });
              
              JScrollPane editorScrollPane = new JScrollPane(editor);
              editorScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
              editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
              
              add(editorScrollPane);

              pack();
              setSize(new Dimension(500, 500));
              setVisible(true);
          }
      }
      ---------- END SOURCE ----------

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: