Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I get the text from a javafx TextField added dynamically?

 
Ivan de la Fuente
Greenhorn
Posts: 2
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people, I´m newbie too... I don´t know how to access to the objects I create at runtime, I would appreciate any help!!

I have two buttons in the scene, one triggers the method `newFields()` which makes two new `TextField` inside an `HBox` and the other through the `getText()` method, I would like it to get the typed texts.

sample.fxml:


Controller.java:


I have the node of the `HBox` containing the `Texfield`s but i´m stuck there, I have tried to get the children of the `HBox` but the IDE throws an error.

Thanks in advance.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan, a warm welcome to CodeRanch!

In future posts, please enclose any code snippet inside code tags as explained in http://www.coderanch.com/how-to/java/UseCodeTags. It makes your post more readable. I've already done it for you this time.

Coming to your question, make the two TextFields in newFields() private members of Controller class and access them from getTexts().
 
Ivan de la Fuente
Greenhorn
Posts: 2
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, thank you for the formatting... I´ll do it from now on.

The question is about TextField added at runtime, and simple getText is not working here.

My problem was that I supposed that the Pane getChildren method was inherited from Node, but I was wrong.

So I cast the Node over an HBox and aplied the getChildren method to access the TextField.

This is the new code:



Thank you very much anyway for the response!!!
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan, apologies for completely misunderstanding your original question. Glad you found the solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic