jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Identify a component that has been added by iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Identify a component that has been added by iteration" Watch "Identify a component that has been added by iteration" New topic
Author

Identify a component that has been added by iteration

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello there,

I hope you want to help me, because I can't figure this one out for myself.

I have a GUI with some JTextFields in it. I want to use text that is typed into the JTextFields as arguments for 4 different setter-methods of another class. That way I can store the textinput from the textFields into different columns of a database.

First I added the JTextFields, manualy, one by one like this:



After that I could connect the textFields to settermethods from another class like this:



Then I thought it was nicer to add the TextFields by iteration, so I deleted the code above and added the next code to my GUI class:



The code works, the JTextFields are added to my GUI. Problem is I don't know how to connect the 4 textField to my setter methods. I can't identify them by name or whatsoever, they are just called texField, and I can't reach them because the are inside the loop. How can I send the text typed in the 4 textField as an argument for my setter methods in the other class?

I've tried something like this, but I doesn't work:



Offcourse I can go back and delete the for loop and add the textFields one by one, but it just doesn't feel right.

Hope you can help me!

Thanks,

Patrick


------------------8<------------------
please cut here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3989
    
  18

I would probably still do them one by one, since every text field (I assume) stands for a different thing. There's a logical separation, so a for loop seems forced to me.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47



Live Curious!!!
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Sagar Dabas wrote:


Thanks for your idea, but I think that wouldn't work for two reasons:

1) OtherClass.setDatabaseField4(textField[i].getText()); needs to run when a 'save' button is pressed. Now OtherClass.setDatabaseField4(textField[i].getText()); only runs when the gui is build.
2) What about setDatabaseField1, setDatabaseField2, setDatabaseField3 ? How would I connect the the textField from the first iteration to the first settermethod, the textfield from the second iteration to the second settermethod etc ?
Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47

Patrick de Kruijf wrote:

Thanks for your idea, but I think that wouldn't work for two reasons:

1) OtherClass.setDatabaseField4(textField[i].getText()); needs to run when a 'save' button is pressed. Now OtherClass.setDatabaseField4(textField[i].getText()); only runs when the gui is build.
2) What about setDatabaseField1, setDatabaseField2, setDatabaseField3 ? How would I connect the the textField from the first iteration to the first settermethod, the textfield from the second iteration to the second settermethod etc ?


Oops..... didn't see those different names....


why don't you make one setter function for all textfields ....like



OR



Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Thanks for your help guys,

Sorry for my late reply, I always have to think about it for a couple of days to get it sorted out in my head . Yesterday I gave it a go and your idea works exactly like I hoped. Here is my solution, maybe I can help someone else with it who has the same question:

Class Frontend
Constructor method:

Adds jlabels and jtextfields to a jpanel, names for the labels are taken from a String array called Columns from another class called Core.



Class Core
String array declaration:

This String array is used for naming the jlabels in the frontend class, iteration (for loops) and other stuff.



Class Core
setDatabaseField method (combined setterMethod):

This method takes the JTextFields created in the constructor in the Frontend class and gets the text that is typed in the Frontend jtextfields for use in other methods. I use it in my code to store text that is typed by a user in different fields of a mysql database.



Thanks again for your help, time and effort, couldn't do it without you!
 
Have you checked out Aspose?
 
subject: Identify a component that has been added by iteration
 
It's not a secret anymore!