JDK-4226384 : JTextField.setText() from EventDispatchThread ignores alignment?
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.7,1.2.2,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-04-02
  • Updated: 2002-06-20
  • Resolved: 2002-06-20
Related Reports
Duplicate :  
Relates :  
Relates :  
Description

Name: dbT83986			Date: 04/02/99


When I call JTextField.setText() with a string that is too long to fit in the JTextField, the text is Left Justified if I make the call before
event handling has started. But if the call to setText() originates from the EventDispatchThread, then if the text is too long, it is
Right Justified even if I call setHorizontalAlignment(JTextField.LEFT) or call setScrollOffset(0).

I'm just guessing that the EventDispatchThread is significant. That's the only difference I can detect between the 2 setText() calls
in the program that follows.

The code below isolates the behavior I'm seeing in a very large panel rendering application. This bug makes some fields very
difficult to read.

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

public class RJText extends JFrame
{
   private JTextField text = new JTextField();
   String[] strings = {"What will happen when the text is too long to fit??", "This fits"};
   private int setTextCount = 1;

   public RJText()
   {
      super("Text Justification Test");
      Container cp = getContentPane();
      cp.setLayout(null);
      
      text.setHorizontalAlignment(JTextField.LEFT);
      cp.add(text);
      text.setBounds(20, 20, 150, 19);
      
      JButton button = new JButton("Click to setText");
      cp.add(button);
      button.setBounds(20, 100, 190, 20);
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e)
         {
            // This will cause left truncation when we use strings[0]
            text.setText(strings[setTextCount % 2]);
            // next line has no effect 
            text.setScrollOffset(0);
            setTextCount++;
         }
      });
      
      setSize(300, 200);
      setVisible(true);
      
      // This won't truncate on the left
      text.setText(strings[0]);
   }
   
	public static void main(String[] args)
	{
      new RJText();
	}
}
(Review ID: 56151)
======================================================================

Comments
EVALUATION Name: apR10133 Date: 09/11/2001 The problem is that the DefaultCursor doesn't updates position on the main thread but do it on the EventDispatchThread. So, when the JTextComponent.setText() method is called on the main thread the DefaultCursor doesn't update its position (and first time it stay at the start of line). On the EventDispatchThread it is moved to the end of line. The cursor should be positioned similarly for all threads, as the setText() is a thread safe. This bug is related to 4424708. ###@###.### ====================================================================== The previous evaluation is not necessarily correct. The caret doesn't respond to the update because the value of DefaultCaret.getAsynchronousMovement() is false. Investigation into the fix for this bug should include a determination as to whether or not this property can be used. ###@###.### 2002-06-05 Name: slR10134 Date: 06/19/2002 (###@###.###) This is not a bug. The text in JTextField is aligned correctly. To see this change the line 'text.setHorizontalAlignment(JTextField.LEFT);' to the line 'text.setHorizontalAlignment(JTextField.RIGHT);' -- the text 'This fits' will be RIGHT aligned. That means alignment works correctly. The reason of such visual effect is that after calling setText in event thread the cursor is set at the end of JTextField. The specification of method setText doesn't determine the cursor position after calling of this method. So the behavior of JTextField is correct. To avoid situation described in this bug user can add the following line into the method actionPerformed: 'text.getCaret().setDot(0);'. Another way is to file new bug against method setText with synopsis like: "Cursor incorrect position after invoking setText". I propose to close this bug as NOT a bug. ======================================================================
11-06-2004

WORK AROUND Name: dbT83986 Date: 04/02/99 I sure hope there is one, becuase I'm afraid this won't get fixed for quite a while. ====================================================================== Name: apR10133 Date: 05/17/2001 Call the text.updateUI() ater setting the text action. ###@###.### ======================================================================
11-06-2004