JDK-8166693 : textfield.setText("") doesn't work
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8u101
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2016-09-20
  • Updated: 2017-11-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8-poolUnresolved
Related Reports
Relates :  
Description
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.