JDK-4425358 : Reopen 4214992: JTextField Always Right-Justifies Oversized Strings
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2001-03-14
  • Updated: 2003-10-02
  • Resolved: 2003-10-02
Description

Name: boT120536			Date: 03/13/2001


java version "1.3.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-beta-b15)
Java HotSpot(TM) Client VM (build 1.3.1beta-b15, mixed mode)

  Bug 4214992 (JTextField Always Right-Justifies Oversized Strings) was closed as
Will Not Fix, and I believe the evaluation to be short-sighted and wish to have
this bug reexamined.

The bug occurs when using setText() to modify the text of a JTextField - the
field will scroll to the end of the text, and no obvious way exists to prevent
this scrolling from occurring.  The scrolling behavior itself is surprising and
undesirable, and the fact that an invokeLater is necessary to prevent it is
ridiculous.  Swing should not require such stupid and nonobvious workarounds
for simple operations on basic components.

Additionally, the stated workaround of using an invokeLater to counter the
behavior does not itself address the entire issue, as the field may still
flicker as it rapidly scrolls to the end and then back to the beginning.

Please reevaluate this issue.
(Review ID: 118417) 
======================================================================

Comments
EVALUATION This is related to DefaultCaret and automatic scrolling. ###@###.### 2002-08-05 Name: anR10225 Date: 09/29/2003 I'm agreed that the current behavior of the setText() is not correct, but unfortunately it's risky to change it. Someone may have an application which relies upon caret position after setText() call. Thus we should close this as 'will not fix'. Fortunately there is a new mechanism implemented (bug 4201999) which allows to avoid the caret movement due to field content updates and avoid any flickers (as in the workaround). For this purpose you should turn off caret updates before setText() and turn it back after (to normally process user input). The following sample demonstrate this : public static void main(String[] args) { final JTextField field = new JTextField(); JFrame f = new JFrame("Test"); f.getContentPane().add(field); JButton b = new JButton("setText()"); final DefaultCaret caret = (DefaultCaret) field.getCaret(); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { int old = caret.getUpdatePolicy(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); field.setText("0123456789012345678901234567890123456789"); caret.setUpdatePolicy(old); } }); f.getContentPane().add(b, BorderLayout.SOUTH); f.pack(); f.setVisible(true); } ======================================================================
11-06-2004

WORK AROUND Name: boT120536 Date: 03/13/2001 Use an invokeLater to setCaretPosition back to zero (although the component still frequently flickers) ======================================================================
11-06-2004