Complete source code below will show you, how to clear text in JTextField.
**************************************************************************
COMPLETE SOURCE CODE FOR : ClearJTextFieldText.java
**************************************************************************
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class ClearJTextFieldText implements ActionListener
{
//Create text field with initial text ( PUT YOUR TEXT HERE )
JTextField textField=new JTextField("PUT YOUR TEXT HERE");
//Create button with text ( Click here to clear text )
JButton button=new JButton("Click here to clear text");
//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(2,1);
//Create window using JFrame with title ( Clear JTextField text )
JFrame frame=new JFrame("Clear JTextField text");
//Constructor
public ClearJTextFieldText()
{
button.addActionListener(this);
frame.setLayout(gl);
frame.add(textField);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,130);
frame.setVisible(true);
}
//override actionPerformed method
public void actionPerformed(ActionEvent event)
{
//Action that will perform when someone click the button
if(event.getSource()==button)
{
//Clear text in JTextField
textField.setText("");
}
}
//Main method
public static void main(String[]args)
{
ClearJTextFieldText cjtft=new ClearJTextFieldText();
}
}
**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************
**************************************************************************
COMPLETE SOURCE CODE FOR : ClearJTextFieldText.java
**************************************************************************
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class ClearJTextFieldText implements ActionListener
{
//Create text field with initial text ( PUT YOUR TEXT HERE )
JTextField textField=new JTextField("PUT YOUR TEXT HERE");
//Create button with text ( Click here to clear text )
JButton button=new JButton("Click here to clear text");
//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(2,1);
//Create window using JFrame with title ( Clear JTextField text )
JFrame frame=new JFrame("Clear JTextField text");
//Constructor
public ClearJTextFieldText()
{
button.addActionListener(this);
frame.setLayout(gl);
frame.add(textField);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,130);
frame.setVisible(true);
}
//override actionPerformed method
public void actionPerformed(ActionEvent event)
{
//Action that will perform when someone click the button
if(event.getSource()==button)
{
//Clear text in JTextField
textField.setText("");
}
}
//Main method
public static void main(String[]args)
{
ClearJTextFieldText cjtft=new ClearJTextFieldText();
}
}
**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************