java - Javafx: Recovering the text of a label contained within a pane through pane.getChildren().get(index) -


this problem i've been trying deal hour now, figured might ask question. goal/question behaviour of list supplied pane.getchildren(). explain better, here's bit of example code.

vbox pane1 = new vbox(); label label1 = new label("a"); label label2 = new label("b"); pane1.getchildren().addall(label1,label2); system.out.println(pane1.getchildren().size()); (int i=0; i<pane1.getchildren().size(); i++) {     system.out.println(i + pane1.getchildren().get(i).(??????????) } 

the list pane1.getchildren() of size 2, pane1.getchildren().get(i) doesn't allow me use label related methods (such gettext(), 1 i'm interested in accessing). happening here, why isn't pane1.getchildren().get(i) acknowledged label?

also worth adding, if run

for (int i=0; i<pane1.getchildren().size(); i++) {     system.out.println(i + pane1.getchildren().get(i).getclass().getname()); } 

the console output says, name of class "javafx.scene.control.label".

i'd love clarification on little problem, , thank in advance!

pane.getchildren() returns observablelist<node>, pane1.getchildren().get(i) has compile-time type of node, not label.

it's not clear why need this: have references labels without iterating through pane's list of children. can do

stream.of(label1, label2).map(label::gettext).foreach(text -> {   // whatever need text... }); 

if want pane's children list, obvious downcast:

for (node n : pane1.getchildren()) {    string text = ((label) n).gettext();    // ... } 

or

pane1.getchildren().stream()     .map(label.class::cast)     .map(label::gettext)     .foreach(text -> {         // whatever need text...     }); 

but of course not work (without checks) if put in pane not label.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -