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

DefaultCaret in a JTextPane causes a scrollbar to move to a wrong position

XMLWordPrintable

    • x86, sparc
    • linux, solaris_10

      FULL PRODUCT VERSION :
      java version "1.5.0_07"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
      Java HotSpot(TM) Server VM (build 1.5.0_07-b03, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.15-27-686 #1 SMP PREEMPT Fri Dec 8 18:00:07 UTC 2006 i686 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      When setText(String) of JTextPane is called the caret in the JTextPane will call repaintNewCaret() even if the caret is not visible. It will call scrollRectToVisible(Rectangle) of JTextPane and forces the knob of the vertical scroll bar to move to the position of the JTextPane.

      For example:
      A JTextPane, JButton and a bunch of JTextFields are in a JScrollPane. The window is not big enough to show all components in the JScrollPane. The vertical scroll bar of the JScrollPane is shown and the knob of the scroll bar stays at the top. At the time the JTextPane is not visible or doesn't gain focus.

      If we call setText(String) of the JTextPane the knob of the scroll bar will move down and make the JTextPane visible even though the JTextPane still doesn't gain focus.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the attached 'TestJTextPane' test case.
      2. Click the 'set text' button on the JFrame


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
        From visual point of view nothing should happen.
      ACTUAL -
      The knob of the vertical scroll bar moves to the bottom to make the JTextPane visible, which contains text 'text set by the JButton'

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.awt.*;
      import java.awt.event.*;

      import javax.swing.*;

      public class TestJTextPane extends JFrame {
          public TestJTextPane() {

      JPanel jPanel = new JPanel(new GridLayout(6, 0));

      JTextField field1 = new JTextField("Field1");
      JTextField field2 = new JTextField("Field2");
      JTextField field3 = new JTextField("Field3");
      JTextField field4 = new JTextField("Field4");

      final JTextPane textPane = new JTextPane();

      JButton b = new JButton("set text");
      b.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
      textPane.setText("text set by the JButton");
                  }});

      jPanel.add(b);
      jPanel.add(field1);
      jPanel.add(field2);
      jPanel.add(field3);
      jPanel.add(field4);
      jPanel.add(textPane);

      JScrollPane scrollPane = new JScrollPane(jPanel);
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(scrollPane, BorderLayout.CENTER);
      this.setTitle("");
      this.setSize(new Dimension(200, 100));
          }
          
          
          public static void main(String[] args) {
      JFrame view = new TestJTextPane();
      view.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
                  }
      });
      view.setVisible(true);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I think either the DefaultCaret should check whether it's visible or not before calling repaintNewCaret() or the JTextPane should override scrollRectToVisible(Rectangle) to check whether it gains focus or not .

      Our workaround is to create a subclass of JTextPane and override scrollRectToVisible(Rectangle) like the following:

      public void scrollRectToVisible(Rectangle aRect) {
      if(hasFocus() || getParent() instanceof JViewport)
      super.scrollRectToVisible(aRect);
              }

            vchoudhaorcl Vaibhav Choudhary (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: