JDK-6448455 : JTextField under Windows look and feel has caret position issue during scrolling
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-07-13
  • Updated: 2015-02-23
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b03Fixed
Related Reports
Relates :  
Description
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)

ADDITIONAL OS VERSION INFORMATION :
Windows XP version 2002 sp 2

A DESCRIPTION OF THE PROBLEM :
With the system look and feel set (Windows XP or Windows Classic), if a JTextfield is filled until the first character in the textfield is clipped, it is not possible to scroll back to the start of the texfield.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
- Set the 'system' look and feel on a Windows XP os
- Create a JTexfield of fixed length
- Enter enough characters to fill the length of the textfield perfectly (e.g. "WXYZ")
- Now append an additional character such that the first character of contents becomes clipped (e.g. appending "a" to "WXYZ" should force part of the "W" to disappear from view)
- Try to scroll back to the beginning of the texfield contents (or press the HOME key)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should be possible to scroll to the beginning of the texfield's contents, and view the first character fully (e.g. with "WXYZa" as the contents, scolling back, should allow the "W" to be fully visible.
ACTUAL -
Upon scrolling back (or pressing HOME key), the caret disappears and the first character of the contents remains clipped (e.g. with "WXYZa" as the contents, after scrolling back to the start, the "W" remains clipped.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error message produced.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/**
 * Testcase:
 *
 * - Run this class, a window with a textfield containing "WXYZ" should be displayed.
 * - Append "a" to the end of the textfield contents.
 * - Use back arrow to scroll back to the beginning of the textfield.
 */

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

public class TestJTextField extends JFrame
{
      JPanel contentPane;
      JPanel jPanel1 = new JPanel();
      JTextField jTextField1 = new JTextField(4);

      public static void main(String[] args){
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
          e.printStackTrace();
        }

        System.out.println(UIManager.getLookAndFeel());

        TestJTextField frame = new TestJTextField();
        frame.validate();
        frame.setVisible(true);
      }

      public TestJTextField() {
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        this.setSize(new Dimension(400, 300));
        jTextField1.setText("WXYZ");
        this.setTitle("Frame Title");
        contentPane.add(jPanel1, BorderLayout.CENTER);
        jPanel1.add("North",jTextField1);
      }
}

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

CUSTOMER SUBMITTED WORKAROUND :
This problem seems to be caused by the textfield margins that are set with the windows look and feel.  Overwriting these margin settings such that no margins are used fixes the issue, but alters the look of the textfield.

e.g. in the above test case, replace

        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
          e.printStackTrace();
        }

with

        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          UIManager.put("TextField.margin", new Insets(0,0,0,0));
        }
        catch(Exception e) {
          e.printStackTrace();
        }

Release Regression From : 1.4.2_12
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION This has never worked properly with the W letter because of variuos bugs, notably 4716362. Even after this has been fixed,
09-08-2006

EVALUATION This problem is specific to WinLAF which installs its own caret in text fields. This caret does not call into JTextField.scrollRectToVisible() but rather uses a private class called SafeScroller that implements slightly different logic. Unfortunately it does not take field insets into account. The problem gets very visible if insets are large, e.g. 10 pixels.
31-07-2006