vedran
vedran

Reputation: 2188

Java, how can I reset JTextField

I made a simple search gui method to search for products in DB and it works flawlessly. However, after the search is done I'd like to reset (JTextFields which are used to get parameters of search) to blank. Is there a method to do this without invoking another instance?

Upvotes: 1

Views: 10902

Answers (2)

DayTimeCoder
DayTimeCoder

Reputation: 4332

How about setting text content to empty strings,like this

myTextField.setText("");

And further more I think you might need a class which is inherited from JTextField and you can add all sorts of methods,Getters and setters in it (such as Clear() ) which may assists you and meets your needs..

Upvotes: 5

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You want to give the class that holds the JTextFields a public void reset() method, and in that method simply call setText("") on all the JTextFields that need to be cleared. If you place all of the JTextFields in a collection such as a List<JTextField> then you can easily close them all with a for loop:

public void reset() {
   for(JTextField field : fieldList) {
      field.setText("");
   }
}

Upvotes: 4

Related Questions