java - Constant label updation from different class in javafx-fxml -
i unable change text of label other class. need update date , time on main screen, while may able perform other functions too, simultaneously. have used timesetting class, extends thread , in it's run() method, i've called updation command in infinite loop, using settext() , slept method second. on running this, nothing happens , on closing output window, error saying nullpointerexcpetion
here's code 2 classes : fxmldocumentcontroller.java
package crt; import java.io.ioexception; import java.net.url; import java.util.date; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.fxml.initializable; import javafx.scene.parent; import javafx.scene.scene; import javafx.scene.control.label; import javafx.stage.modality; import javafx.stage.stage; import javafx.stage.stagestyle; public class fxmldocumentcontroller extends thread implements initializable { @fxml protected label check; @fxml **protected label date;** @fxml protected label time; @fxml protected label rrrr; @fxml protected label dddd; @fxml protected label ssss; @fxml protected label temp; @fxml protected label maxtemp; @fxml protected label mintemp; @fxml private void handlebuttonaction(actionevent event) throws ioexception { //dc.setdate(date.textproperty().bind(valueproperty)); fxmlloader fxmlloader = new fxmlloader(getclass().getresource("menu.fxml")); parent root1 = (parent) fxmlloader.load(); stage stage = new stage(); stage.initmodality(modality.application_modal); stage.initstyle(stagestyle.undecorated); stage.settitle("menu"); stage.setscene(new scene(root1)); stage.show(); } @override public void initialize(url url, resourcebundle rb) { }
fxmldocument.fxml
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.control.button?> <?import javafx.scene.control.label?> <?import javafx.scene.control.progressbar?> <?import javafx.scene.layout.anchorpane?> <?import javafx.scene.text.text?> <anchorpane id="anchorpane" prefheight="367.0" prefwidth="510.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="crt.fxmldocumentcontroller"> <children> <button fx:id="button" layoutx="387.0" layouty="302.0" minheight="25.0" minwidth="80.0" onaction="#handlebuttonaction" ontouchpressed="#handlebuttonaction" text="menu" /> <label fx:id="date" layoutx="56.0" layouty="64.0" minheight="25.0" minwidth="80.0" /> <label fx:id="time" layoutx="361.0" layouty="64.0" minheight="25.0" minwidth="80.0" text="s" /> <label fx:id="rrrr" layoutx="76.0" layouty="100.0" minheight="25.0" minwidth="70.0" /> <label fx:id="dddd" layoutx="195.0" layouty="100.0" minheight="25.0" minwidth="70.0" /> <label fx:id="ssss" layoutx="314.0" layouty="100.0" minheight="25.0" minwidth="70.0" /> <text layoutx="136.0" layouty="163.0" stroketype="outside" strokewidth="0.0" text="temp :-" /> <label fx:id="temp" layoutx="275.0" layouty="156.0" minheight="25.0" minwidth="70.0" text="a" /> <text layoutx="136.0" layouty="203.0" stroketype="outside" strokewidth="0.0" text="max temp :-" /> <label fx:id="maxtemp" layoutx="275.0" layouty="188.0" minheight="25.0" minwidth="70.0" text="b" /> <text layoutx="136.0" layouty="243.0" stroketype="outside" strokewidth="0.0" text="min temp :-" /> <label fx:id="maxtemp" layoutx="275.0" layouty="225.0" minheight="25.0" minwidth="70.0" text="c" /> <progressbar layoutx="401.0" layouty="21.0" prefheight="18.0" prefwidth="70.0" progress="0.0" /> <button fx:id="startbutton" layoutx="14.0" layouty="18.0" mnemonicparsing="false" onaction="#startstart" text="start" /> <label fx:id="check" layoutx="42.0" layouty="306.0" /> </children> </anchorpane>
timesetting.java
package crt; import java.util.date; public class timesetting extends thread { @override public void run() { fxmldocumentcontroller fdc = new fxmldocumentcontroller(); // fdc.load(); int i=0; while(true) { date d = new date(); fdc.date.settext("fd" + i); i++; try { thread.sleep(1000); } catch(interruptedexception e) { } } } }
crt.java
package crt; import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.stage.stage; public class crt extends application { @override public void start(stage stage) throws exception { parent root = fxmlloader.load(getclass().getresource("fxmldocument.fxml")); scene scene = new scene(root); stage.setscene(scene); stage.show(); } public static void main(string[] args) throws interruptedexception { launch(args); timesetting ts = new timesetting(); ts.start(); } }
launch()
not complete until application
exits. should start thread in start
method of application.
furthermore new fxmldocumentcontroller()
creates new instance of controller class - 1 isn't connected fxml, none of fields injected. can find examples of "communicating fxml" in stackoverflow docs, e.g. passing data fxml - accessing existing controller
also if work, you're still using thread different javafx application thread modify ui. should not done. instead use platform.runlater
update ui:
while(true) { date d = new date(); final string text = "fd" + i; platform.runlater(() -> { fdc.date.settext(text); }); i++; try { thread.sleep(1000); } catch(interruptedexception e) { } }
Comments
Post a Comment