Results 1 to 3 of 3
  1. #1
    RaymondF is offline Member
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    0

    Default Problem TextField setText

    Is there an explanation for the following behavior?
    The class
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    
    public class Why 
    extends Frame
    {
      private TextField name = new TextField("Name", 50);
      private Button reset = new Button("Reset");
    
      public static void main(String [] arg)  {
        new Why ();
      }
    
      public Why ()
      {
        addWindowListener(new WindowAdapter() {  	
                                     public void windowClosing(WindowEvent we) {
                                        System.exit(0);
                                     }
                                   });
        reset.addActionListener(new ActionListener() {
                                           public void actionPerformed(ActionEvent ae) {
                                              name.setText(""); 
                                          }  	
        	                            });
        
        add(name, BorderLayout.NORTH);
        add(reset, BorderLayout.SOUTH); 
        pack();
        setVisible(true);
      }
    }
    With jdk1.7.0_21 the text in the TextField name ist set to "" on every click of the Button reset correctly.
    With jdk1.7.0_51 (and 1.8.0) only the first click clears the content of the TextField, the second click doesnít reset the (new) content. If I use name.setText("XY") then the content is always replaced with XY. Whatís the reason for the difference between "" and "XY"?
    With JTextField the behavior is "correct" (= setText("") works correct on every click).

    Please no answer like "use Swing or JavaFX", Iím only interested in the reason for this behavior.
    Thanks
    Raymond

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problem TextField setText

    Interesting question. I see the same behavior.

    Digging through the source for the JDK, your call to TextField.setText() eventually leads to TextComponent.setText(), which looks like this in Java 8:

    Java Code:
    public synchronized void setText(String t) {
            boolean skipTextEvent = (text == null || text.isEmpty())
                    && (t == null || t.isEmpty());
            text = (t != null) ? t : "";
            TextComponentPeer peer = (TextComponentPeer)this.peer;
            // Please note that we do not want to post an event
            // if TextArea.setText() or TextField.setText() replaces an empty text
            // by an empty text, that is, if component's text remains unchanged.
            if (peer != null && !skipTextEvent) {
                peer.setText(text);
            }
        }
    And the source for Java 6 looks like this:

    Java Code:
    public synchronized void More ...setText(String t) {
             text = (t != null) ? t : "";
             TextComponentPeer peer = (TextComponentPeer)this.peer;
             if (peer != null) {
                 peer.setText(text);
             }
         }
    It seems like some extra logic has been added, which might be screwing things up for you.

    It should be noted that calling System.out.println("Text: " + name.getText()); from your actionPerformed() method changes the behavior of the program, and it seems to work with Strings other than the empty String.

    I couldn't find a bug report on this, so you might consider filing one. Not sure if this is the right link for that or not: Bug Database
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    RaymondF is offline Member
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    0

    Default Re: Problem TextField setText

    Dear KevinWorkman,
    thanks for your very fast answer.
    Raymond

Similar Threads

  1. JTextField setText problem
    By LancePaxton in forum AWT / Swing
    Replies: 6
    Last Post: 09-09-2012, 12:14 PM
  2. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 05:44 AM
  3. If statement and JLabel.setText() problem
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 07-25-2010, 11:11 AM
  4. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 06:29 AM
  5. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 11:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •