Search code examples
javafxscalafx

Get text values from HBox


Say I have an HBox like this:

val texts = new HBox {
  content = Seq(new TextArea, new TextArea)
}  

Now I'd like to get TextAreas' text values in a collection. How can I get to these TextAreas? texts.content is an ObservableList[javafx.scene.Node], not ObservableList[TextArea].

I've tried type casting like this:

texts.content.get(0).asInstanceOf[TextArea].getText  

And gott the following exception: java.lang.ClassCastException: javafx.scene.control.TextArea cannot be cast to scalafx.scene.control.TextArea


Solution

  • You can type cast the elements while fetching them, for example:

    TextArea txt1 = (TextArea)observableList.get(0);
    TextArea txt2 = (TextArea)observableList.get(1);
    

    Or, if you have multiple TextArea

    for(Node node: observableList){
       TextArea txt = (TextArea)node;
       //Do something with the txtArea
    }