Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to clear textfields

Status
Not open for further replies.

badbhoy

Programmer
Apr 11, 2002
12
GB
i would like to clear all the textfields on my interface to insert more information how do i do this by means of a button
 
Call the setText ("") method of whatever u want to clear ...
 
To do this by means of a button you need to add an action listener to the button in question, and in the listner's actionPerformed method you would cycle through all the text fields and call it's setText() method passing either an empty ("") String or null. The Empty string would be best. See the example below:
Code:
JTextField textFieldOne   = new JTextField();
JTextField textFieldTwo   = new JTextField();
JTextField textFieldThree = new JTextField();
JTextField textFieldFour  = new JTextField();
JButton    clearButton    = new JButton("Clear");

clearButton.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
    textFieldOne.setText("");
    textFieldTwo.setText("");
    textFieldThree.setText("");
    textFieldFour.setText("");
  }
});
If your text fields are dynamic, then you could store references to them in an ArrayList, and then just loop through them in the actionPerformed method with a "for" loop.

I hope this helps...

Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top