textfield.setText("") doesn't work

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      java <TM> SE Runtime Environment (build 1.8.0_101-b13)
      Java Hotspot(TM) 64-bit Server VM (build 25.101-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 7 Professional (64-bit)

      [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      Button btnReset;

      btnReset.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      txtName.setText("");
      txtRollNo.setText("");
      txtMarks.setText("");
      }
      });

      In the above code setText("") method doesn't change the value of the Textfiled string to empty string.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      In the following program pressing the Reset button doesn't reset the Strings in textboxes.

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
            
      class Ideone extends Frame
      {
      Label lblName, lblRollNo, lblMarks;
      TextField txtName, txtRollNo, txtMarks;
      Button btnReset, btnSave;

      Ideone()
      {
      setLayout(new GridLayout(4, 2));
      setTitle("Student Record Maintainer");
      setSize(280,300);
      setVisible(true);

      lblName = new Label("Name: ");
      txtName = new TextField("", 20);

      lblRollNo = new Label("Roll No.: ");
      txtRollNo = new TextField("", 20);

      lblMarks = new Label("Marks: ");
      txtMarks = new TextField("", 20);

      btnReset = new Button("Reset");
      btnSave = new Button("Save");

      btnReset.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      txtName.setText("");
      txtRollNo.setText("");
      txtMarks.setText("");
      }
      });

      btnSave.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
      System.out.println(txtName.getText());
      System.out.println(txtRollNo.getText());
      System.out.println(txtMarks.getText());
      }
      });

      addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
      System.exit(0);
      }
      }
      );

      add(lblName); add(txtName);
      add(lblRollNo); add(txtRollNo);
      add(lblMarks); add(txtMarks);
      add(btnReset); add(btnSave);
          }
            
          public static void main(String args[]) throws java.lang.Exception
      {
      new Ideone();
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Upon pressing the Reset button the textfields (i.e. Roll No, Name, Marks)
      should be reset.
      ACTUAL -
      Upon pressing the Reset button the textfields (i.e. Roll No, Name, Marks)
      values are not reset. Nothing happens to textfield values.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      // 1) Example of event handling within class

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
            
      class Ideone extends Frame
      {
      Label lblName, lblRollNo, lblMarks;
      TextField txtName, txtRollNo, txtMarks;
      Button btnReset, btnSave;

      Ideone()
      {
      setLayout(new GridLayout(4, 2));
      setTitle("Student Record Maintainer");
      setSize(280,300);
      setVisible(true);

      lblName = new Label("Name: ");
      txtName = new TextField("", 20);

      lblRollNo = new Label("Roll No.: ");
      txtRollNo = new TextField("", 20);

      lblMarks = new Label("Marks: ");
      txtMarks = new TextField("", 20);

      btnReset = new Button("Reset");
      btnSave = new Button("Save");

      btnReset.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      txtName.setText("");
      txtRollNo.setText("");
      txtMarks.setText("");
      }
      });

      btnSave.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
      System.out.println(txtName.getText());
      System.out.println(txtRollNo.getText());
      System.out.println(txtMarks.getText());
      }
      });

      addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
      System.exit(0);
      }
      }
      );

      add(lblName); add(txtName);
      add(lblRollNo); add(txtRollNo);
      add(lblMarks); add(txtMarks);
      add(btnReset); add(btnSave);
          }
            
          public static void main(String args[]) throws java.lang.Exception
      {
      new Ideone();
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      1) before setting the text by setText() method If we call setText() method with non-empty string it works.

      2) Before setting the text by setText() method If we call getText() method and then call setText method it also works.





            Assignee:
            Unassigned
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: