• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to clear Multiple text fields?  RSS feed

 
Azeem Rathore
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hilmy first language is German so please bear my English
i am new to java programming and making human resource management system
i have form which contain 76 textfield and comboBox and values populates from the database it also have next previous first last and clear record button
I have successfully written all the code and it works fine now i want some logic or code to clear all the text field data and make it empty basically i want to implement the insert new record functionality in it
check the image of my form please if you provide code consider it help
Thanks in advance
Untitled.jpg
[Thumbnail for Untitled.jpg]
image
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there

One dumb way to do setText("") on every text field individually in an action listener.

Another more smart way is to populate an array or list of these text fields when loading your GUI. Then loop this collection and setText("") on each text field.
 
Azeem Rathore
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have set each text field individually and it took so much time i want some function like you said populate array that is what i do not know how to do can you give me example please? how to populate array of fields?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Azeem Rathore wrote:i have set each text field individually and it took so much time i want some function like you said populate array that is what i do not know how to do can you give me example please? how to populate array of fields?


First declare a collection like List<JComponent>. When you add these components to the panel, add it to the list too. Or you can have 2 lists one for text field and one for combo box since the way to clear things for these are different.



Then in your action listener
 
Azeem Rathore
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot i have solved it
 
garrett sudicky
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this it my clear button on my program i am working on. it resets all the variables back to there original state.

add your handling code here:
}
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dont really need the extra array. JPanel being a container, already has a built in method to retrieve all the child components. Use this method, iterate over each child, check if it is the type which you want to reset (e.g. You dont want to "reset" buttons) and then invoke the appropriate method to reset/clear it
 
Nadia Ahmed
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I HAVE SAME PROBLEM i posted in this http://www.coderanch.com/t/616207/GUI/java/reset-JtextFields-multiple-panel-form#2814164 thread please view and help me i request you
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's the same problem, just apply the same solution already discussed here.
 
Nadia Ahmed
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried it is still not working
 
Santhosh Kumar Aalibilli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just try this simple code. I used this in my POS application design. Hope this helps.



Button3 in my application is a RESET Button which should functionally clear all the text entered in the TextFields.
Similarly you can try for other components as well. Let me know if this worked for you as well.
Note: This works if all you components are in single Panel. If you have multiple panels implement the same logic for all containers.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it be simpler to use instanceof?You can use different class names depending how far up the inheritance hierarchy you want to go.
 
Piet Souris
Rancher
Posts: 1946
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if Nadia has solved her problem in the last two years.

Do the last two methods above also work for components that extend
textfields? The simplest method to me is K. Tsang's method, from july 3, 2013,
15.19.
Simply create the list when you create the panel; at that time you know
exactly what components are textfields or subclasses and if they should be cleared.

Greetz,
Piet
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!