Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am trying to load a gridpane using a for loop

 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code I am using and I understand why it is not working because of the duplicate tfNameField.

So how do I code something like this?

gpFormatter is a grid pane.

 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've not used GridPane as it's a JavaFX feature but if you need to store multiple TextFields in a grid you probably need to move the new TextField(); inside the loop to create a new TextField for each iteration of the loop.
If you need to keep a reference to all these TextFields you can store them in an array.
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that did the trick

How do I now read the data that has been entered into the fields with a for loop?



Thanks
Roger
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I now read the data that has been entered into the fields

You need a reference to the textfield to be able to call its methods.
One way to have references to all the textfields is to save the references.  For example in an array or in a collection of some type, say a Map.

If there are multiple textfields how do you know what is in each textfield?  How do you associate a textfield with the data that is in it?
When should the program get data from a textfield?  Does the user cause an event to tell the program its time to get the data? For example by pressing a button or by pressing Enter when focus is in the textfield.
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to start at the first one and read each one in sequence. This will be caused by the user clicking a button.

Then the fields will be appended together to make a message.


Roger

 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  start at the first one and read each one in sequence

Save the references to the textfields in an ArrayList that could be iterated through to get each of the references one at a time from first to last.
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK here is what I am trying.. Line 11 will not work. How do I save the textfield name to fetch it?


 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I save the textfield name to fetch it? 

You do not save the variable name.  You save a reference to the textfield in an ArrayList.  Use the add() method to save the reference and the get() method to retrieve the reference.

Line 11 will not work

Please explain what "not work" means.
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the code where line 8 has an error in the code as it  cannot find variable TextField one or TextField two. I understand that because I have not defined them properly.

I am lost with the add and get method. Can you show me an example using this code so that line 8 will print out the text I enter in the text fields created in the loop?

I appreciate your help and patience working with a greenhorn...


 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I  have not defined them properly.
Ok. The compiler sees that. Not sure what that code is intended to do.

I am lost with the add and get method. 

Start by reading the API doc for the ArrayList class: http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Also the tutorial: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html

A small example of an ArrayList:
 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your example and how the array list works.

My problem is how do i get a reference to the textfield so that I can fetch the value entered by the user.

The for loop adds the textfields to the gridpane.
the format entry trys to get the text the user has entered and print it out.


 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i get a reference to the textfield 

Define an ArrayList at the class level so that it was in scope where it was being accessed. 
In the loop that creates the textfields and adds them to the GUI, also add the textfield reference to the ArrayList.
Later When you want to get the references to the textfields, in a loop call the ArrayList's get method to get the references to the textfields.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic