java - JavaFx: fxmlLoader.load returns Parent? -
i have scrollpane @ root level in fxml file , have following code:
import javafx.scene.parent; ... parent = (parent)fxmlloader.load(getfxmlstream("my.fxml")); if (parent.getclass().isassignablefrom(parent.class)){ system.out.println("this parent"); }else{ system.out.println("this not parent");//this printeed }
why load function returns not assignable parent class?
from javadocs isassignablefrom
:
determines if class or interface represented class object either same as, or superclass or superinterface of, class or interface represented specified class parameter.
so testing if runtime type of object got fxml loader equal to, or superclass of, parent
. if strict subclass of parent
(vbox
, borderpane
etc), false
.
if want test if value have kind of parent
, usual way instanceof
:
if (parent instanceof parent)){ system.out.println("this parent"); }else{ system.out.println("this not parent");//this printeed }
if want use getclass()
, isassignablefrom()
, have things wrong way around:
if (parent.class.isassignablefrom(parent.getclass())){ system.out.println("this parent"); }else{ system.out.println("this not parent");//this printeed }
though recommend standard instanceof
approach anyway.
also note can use fxml pretty load object, there no guarantee subclass of parent
.
Comments
Post a Comment