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

JTextField.setText() from EventDispatchThread ignores alignment?

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 1.1.7, 1.2.2, 1.3.0
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
    • CPU:
      generic, x86
    • OS:
      generic, windows_nt

      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)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                idk Igor Kushnirskiy (Inactive)
                Reporter:
                dblairsunw Dave Blair (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: