• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to clear Multiple text fields?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot i have solved it
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
}
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I HAVE SAME PROBLEM i posted in this https://coderanch.com/t/616207/GUI/java/reset-JtextFields-multiple-panel-form#2814164 thread please view and help me i request you
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried it is still not working
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic