passing a variable string through a textfield for a digital clock in java fxml -
im building stop watch digital , analog clock having trouble printing digital clock value because im unsure how pass string fxml.
why when try id="digitaltext" textfield in fxml program fails if write using text="00:00" work properly(just text 00:00 though no updating digital clock of course). have hoped giving id print value inside it.
the controller
package tds7xbstopwatchfxml; import java.awt.textfield; import java.net.url; import java.util.resourcebundle; import javafx.animation.animation; import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.util.duration; public class fxmldocumentcontroller implements initializable { @fxml private imageview clockface; @fxml private imageview hand; @fxml private textfield digitaltext; string digital; double rotation = 0.0; integer ms = 00; integer mm = 00; timeline timeline = new timeline( new keyframe(duration.millis(1000), (actionevent actionevent) -> { updatetimer(); updatedigital(); }) ); @fxml private void stopbutton(actionevent event) { timeline.stop(); } @fxml private void startbutton(actionevent event) { timeline.play(); } @fxml private void resetbutton(actionevent event) { rotation = 0.0; ms = 0; mm = 0; hand.setrotate(0); digital="00:00"; timeline.stop(); } public void updatetimer() { ms += 1000; if (ms >= 60000) { ms = 00; mm++; } rotation = (ms / 60000.0) * 360.0; hand.setrotate(rotation); } public void updatedigital(){ if(ms/1000<10 && mm<10){ digital=("0" + string.valueof(mm) + ":" + "0" + string.valueof(ms/1000)); } if(ms/1000>=10 && mm <10){ digital=("0" + string.valueof(mm) + ":" + "" + string.valueof(ms/1000)); } if(mm>=10 && ms/1000<10){ digital=("" + string.valueof(mm) + ":" + "0" + string.valueof(ms/1000)); } if(mm>=10 && ms/1000>=10){ digital=("" + string.valueof(mm) + ":" + "" + string.valueof(ms/1000)); } } @override public void initialize(url url, resourcebundle rb) { clockface.setimage(new image(this.getclass().getresourceasstream("clockface.png"))); hand.setimage(new image(this.getclass().getresourceasstream("hand.png"))); timeline.setcyclecount(animation.indefinite); digitaltext.settext(digital); } }
i know there better ways implement code digital clock want know passing changing string printed. digitaltext.settext(digital); cause crash
and fxml
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.text.*?> <?import javafx.scene.image.*?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <anchorpane id="anchorpane" prefheight="600.0" prefwidth="600.0" styleclass="mainfxmlclass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tds7xbstopwatchfxml.fxmldocumentcontroller"> <stylesheets> <url value="@stopwatchui.css" /> </stylesheets> <children> <stackpane layouty="94.0" prefheight="150.0" prefwidth="200.0" anchorpane.bottomanchor="0.0" anchorpane.leftanchor="0.0" anchorpane.rightanchor="0.0" anchorpane.topanchor="175.0"> <children> <imageview fx:id="clockface" fitheight="400.0" fitwidth="400.0" pickonbounds="true" preserveratio="true" /> <imageview fx:id="hand" fitheight="400.0" fitwidth="400.0" pickonbounds="true" preserveratio="true" /> </children> </stackpane> <textfield fx:id="digitaltext" alignment="center" editable="false" layoutx="204.0" layouty="123.0" prefheight="43.0" prefwidth="194.0"> <font> <font name="arial" size="23.0" /> </font> </textfield> <button fx:id="stopbutton" layoutx="280.0" layouty="75.0" onaction="#stopbutton" text="stop" /> <button fx:id="startbutton" layoutx="204.0" layouty="75.0" onaction="#startbutton" text="start" /> <button fx:id="resetbutton" layoutx="353.0" layouty="75.0" onaction="#resetbutton" text="reset" /> </children> </anchorpane>
you imported swing textfield, not javafx text field
change
import java.awt.textfield
to
import java.scene.control.textfield;
Comments
Post a Comment