Reputation: 13
how to delete a textfields value when a button clicked in java because textfield.setText(" "); does not work.
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==ok){
String temp=textField.getText();
textField.setText("hello "+ temp);
}
if(arg0.getSource()==cancel)
{
textField.setText(null);
}
}
Upvotes: 1
Views: 2960
Reputation: 2580
If you talking about Class JTextField
you need exactly method setText("")
to delete text from textfield.
Maybe your problem in action listener which you linked with button, try this:
JButton button = new JButton("Clear TextField");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textfield.setText("");
//textfield.setText(null); // or try this
}
});
Upvotes: 1