JDK-8060137 : Removing Text from TextField / TextArea is not possible after typing
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7u40,8,9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-10-06
  • Updated: 2017-08-17
  • Resolved: 2015-12-08
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 9
9 b100Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
Bug occurs on all java-versions from 1.7 onwards, for example:
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b19)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode, sharing)
and
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) Client VM (build 25.11-b03, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Bug occurs on all Windows-versions (x86/x64), for example:
Microsoft Windows XP [Version 5.1.2600]
Microsoft Windows [Version 6.1.7601]
Microsoft Windows [Version 6.2.9200]

A DESCRIPTION OF THE PROBLEM :
Since java-version 1.7 it is not possible to remove (clear/empty) text from the awt.TextField or awt.TextArea after the user has typed text in it. Calling the methods setText("") or setText( new String() ) or setText( null ) does not work as expected. If a java-program sets text in the TextComponent, removing the text by calling the method setText("") is possible. It seems that from java-version 1.7 onwards the awt.TextComponent does not recognize that a user has changed the text by typing on a keyboard.  

REGRESSION.  Last worked in version 6u45

ADDITIONAL REGRESSION INFORMATION: 
All java-versions until 1.7, for example:
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.) Compile the example program with java 1.6, run it on Windows with java 1.6, 1.7 and 1.8.
2.) Type some text in the TextField and the TextArea.
3.) Press the button 'Remove'

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
With all java-versions on Windows the TextField and TextArea should be empty after pressing the Button 'Remove' which calls the method setText("") regardless of whether a user typed text in an awt.TextComponent or a java-program sets text in a TextComponent. 
ACTUAL -
Beginning with java-version 1.7 (including 1.8) the awt.TextArea and the  awt.TextField is not empty after the user typed text in that TextComponent and called the method setText("").

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;

public class TextClearBug extends Frame
{
  Label     myLabel       = new Label("Type something and press Remove");
  
  TextField myTextField   = new TextField();
  TextArea  myTextArea    = new TextArea(1,30);
  Button    myClearButton = new Button("Remove");
  
  public TextClearBug()
  {
	this.setLayout(new GridLayout(4, 1, 5, 5));

	this.add( myLabel );
	this.add( myTextField );
	this.add( myTextArea );
	this.add( myClearButton );


	myClearButton.addActionListener(new ActionListener() 
	{
	  public void actionPerformed(ActionEvent e) 
		{
		  String clearString = new String("");
		  
		  System.out.println("Set Text in TextField to <"+clearString+">");
		  System.out.println("Set Text in TextArea to <"+clearString+">");

		  myTextField.setText( clearString );
		  myTextArea.setText( clearString );
        }
    });

   }

  public static void main(String[] args)
  {
	TextClearBug myTextClearBug = new TextClearBug();
	myTextClearBug.pack();	
	myTextClearBug.setVisible( true );	
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Just before calling setText(""), call getText(), or setText(" "):

// clear Bugfix1
myTextField.getText();
myTextField.setText("");

// clear Bugfix2
myTextField.setText(" ");
myTextField.setText("");


Comments
regression of JDK-7184365
16-11-2015

Removing "regression" label, since difference in behavior between release families like in this case - JDK 6 and JDK 7, cannot be considered as a regression.
10-10-2014

The bug was reproduced by means of the test case provided in the description of the bug on MS Windows 7 OS with JDK 9 b34, JDK 8u20 b26, JDK 7u67 b01.
10-10-2014