tags:

views:

49

answers:

2

I have a form in Swing with a lot of textfields receiving data. The idea is when a button is clicked, the app gathers all the data from the textfields at once. Do you know a good practice for this? Or is it necessary to gather the data textfield by textfield?

+2  A: 

If you are looking for a framework, try BeansBinding from JDesktop. It is quite well integrated with Netbeans IDE's Matisse GUI builder, and should be sufficient for most needs in a simple program. If you are using Netbeans, here's a good primer to help you get started.

However, as BeansBinding works by abstracting all the complexity away, what you lose is more granular control. If you are developing a highly complex application that requires more control and tweaks, then I would suggest that roll up your sleeves and prepare to get your hands dirty. Register a class as a listener for each of the editable fields on the form, and save the data to a bean class that stores each of the necessary values.

bguiz
+2  A: 

You will have to read text from each text field/area one at a time. In order to make your life easier - you'd want to put all the fields you want to read inside a List or a Vector when they get created; in this way, you will be able to read them in a simple for loop. If you need to know which field is which, use a HashMap and name each JTextField with a unique name to serve as key for the HashMap. Conversely, you can use .setName("SomeName") and use a List/Vector and use getName() to see what field it is.

As far as good practices go, unless you have many-many-many fields in your form, you don't need to worry because it won't take too long to do and you can put all the reading code in your listener method. However, if we're talking about many dozens or even hundreds of text fields, you'd want to start a new Thread to do that for you and disable the button so that the user knows something is going and can't press the same button again until the reading is done.

Warlax
hey guys! thx.. I was think about a hashmap.. jejeje.. but i was not sure, however, thx =)
RhigoHR