java - How to lookup the thumb of a slider in a controller class for fxml? -
i want customize slider
, found working example here on stackoverflow:
import javafx.application.application; import javafx.geometry.insets; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.control.slider; import javafx.scene.layout.pane; import javafx.scene.layout.stackpane; import javafx.stage.stage; public class sliderwithlabeledthumb extends application { @override public void start(stage primarystage) { slider slider = new slider(); stackpane root = new stackpane(slider); root.setpadding(new insets(20)); scene scene = new scene(root); slider.applycss(); slider.layout(); pane thumb = (pane) slider.lookup(".thumb"); label label = new label(); label.textproperty().bind(slider.valueproperty().asstring("%.1f")); thumb.getchildren().add(label); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
unfortunately, if thumb of slider in controller class fxml view, nullpointerexception
when trying add label
thumb
's children… thumb can not looked up. found answer here says lookup()
must called after stage.show()
, here question again:
how can slider thumb in controller class manages fxml components (that not have stage.show()
)?
tried creating slider
in code , used 1 fxml, both lookups return null
when calling in controller's initialize(…)
method.
is somehow possible customize slider
's thumb in controller class?
edit:
the code produces exception in controller class gets automatically instantiated due entry in corresponding xml file. no matter if use slider
loaded fxml annotation, having member , initialize or create new slider
object. long in controller class, keep getting nullpointerexceptions
line commented in code below:
public class mainviewcontroller implements initializable { @fxml private vbox root; private slider timeslider; @override public void initialize(url location, resourcebundle resources) { timeslider = new slider(); root.getchildren.add(timeslider); timeslider.setmax(360); timeslider.setmin(0); timeslider.setcursor(cursor.open_hand); timeslider.applycss(); timeslider.layout(); pane thumb = (pane) timeslider.lookup(".thumb"); label label = new label(); label.textproperty().bind(timeslider.valueproperty().asstring("%.1f")); // following line points thumb, null then… (debugger proof) thumb.getchildren().add(label); } }
in main.java
load fxml file:
public class main extends application { @override public void start(stage primarystage) { try { fxmlloader loader = new fxmlloader(); loader.setlocation(getclass().getresource("mainview.fxml")); parent root = loader.load(); scene scene = new scene(root,800,600); scene.getstylesheets().add(getclass().getresource("application.css").toexternalform()); primarystage.setscene(scene); primarystage.show(); } catch(exception e) { e.printstacktrace(); } } public static void main(string[] args) { launch(args); } }
finally, fxml file:
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.vbox?> <vbox fx:id="root" maxheight="600" maxwidth="800" prefheight="600" prefwidth="800" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.mainviewcontroller"> </vbox>
wiki
Comments
Post a Comment