JavaFXStringBindings
我目前正在学习JavaFX,在字符串绑定方面遇到了一些问题。我有以下三个问题:JavaFXStringBindings,java,javafx,javafx-8,Java,Javafx,Javafx 8,我目前正在学习JavaFX,在字符串绑定方面遇到了一些问题。我有以下三个问题: 我的目标是在模型(model.runningWorkpackage.durationformatted和model.moneyernedsum)中的值更新时更新视图(labelTime和labelmoneedSum)。我试图通过以下绑定实现这一点:labelTime.textProperty().bind(model.runningWorkpackage.durationformatted)和labelMoneDre
model.runningWorkpackage.durationformatted
和model.moneyernedsum
)中的值更新时更新视图(labelTime
和labelmoneedSum
)。我试图通过以下绑定实现这一点:labelTime.textProperty().bind(model.runningWorkpackage.durationformatted)
和labelMoneDreaghedSum.textProperty().bind((Bindings.format(“%.2f Euro”,model.moneyEarnedSum))编码>但标签不更新。我理解错了什么
公共工作包runningWorkpackage=new Workpackage()代码>如何在构造函数中初始化它们。那是合适的地方吗
public StringBinding durationFormated = new StringBinding()
{
{
super.bind(duration);
}
@Override
protected String computeValue()
{
return Util.formatTime(duration.intValue());
}
};
及
相关代码: 控制器 模型 线
我做了一些实验,我想我找到了解决办法。 您有一个自己的
TimerThread
类,但是您可以使用java内置的java.util.Timer
。我的课程是这样的:
这是型号:
package stackoverflow;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Model {
private StringProperty string;
public Model(String string) {
this.string = new SimpleStringProperty(string);
}
public String getString() {
return string.get();
}
public StringProperty stringProperty() {
return string;
}
}
package stackoverflow;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.time.LocalTime;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
public class TestController implements Initializable {
private static final int DELAY = 0;
private static final int PERIOD = 1000;
@FXML
private Label myLabel;
private Model model;
@Override
public void initialize(URL location, ResourceBundle resources) {
model = new Model("");
myLabel.textProperty().bind(model.stringProperty());
doTheTask();
}
private void doTheTask() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override public void run() {
Platform.runLater(() -> model.stringProperty().set(LocalTime.now().toString()));
}
}, DELAY, PERIOD);
}
}
这是控制器:
package stackoverflow;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Model {
private StringProperty string;
public Model(String string) {
this.string = new SimpleStringProperty(string);
}
public String getString() {
return string.get();
}
public StringProperty stringProperty() {
return string;
}
}
package stackoverflow;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.time.LocalTime;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
public class TestController implements Initializable {
private static final int DELAY = 0;
private static final int PERIOD = 1000;
@FXML
private Label myLabel;
private Model model;
@Override
public void initialize(URL location, ResourceBundle resources) {
model = new Model("");
myLabel.textProperty().bind(model.stringProperty());
doTheTask();
}
private void doTheTask() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override public void run() {
Platform.runLater(() -> model.stringProperty().set(LocalTime.now().toString()));
}
}, DELAY, PERIOD);
}
}
因此,这个小示例显示每秒更新的时间
我认为这个例子可以帮助你解决你的问题。
如果您在javafx中使用线程、计时器,您应该使用
Platform.runLater()
,因此如果我理解正确,您希望显示模型中的一些数据,并且如果模型已更新,标签也将更新?我说得对吗?是的,你说得对!我编辑了问题我看到您使用整数来保存持续时间的值,但是您可以轻松地将整数
转换为字符串
,例如在构造函数中,当您想要显示其值时,使用字符串
要容易得多。
public class TimerThread extends Thread
{
Model model;
public TimerThread(Model model)
{
this.model = model;
}
@Override
public void run()
{
while(Util.running)
{
try {Thread.sleep(1000);} catch (InterruptedException e){}
model.runningWorkpackage.setDuration(model.runningWorkpackage.getDuration() + 1);
}
}
}
package stackoverflow;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Model {
private StringProperty string;
public Model(String string) {
this.string = new SimpleStringProperty(string);
}
public String getString() {
return string.get();
}
public StringProperty stringProperty() {
return string;
}
}
package stackoverflow;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.time.LocalTime;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
public class TestController implements Initializable {
private static final int DELAY = 0;
private static final int PERIOD = 1000;
@FXML
private Label myLabel;
private Model model;
@Override
public void initialize(URL location, ResourceBundle resources) {
model = new Model("");
myLabel.textProperty().bind(model.stringProperty());
doTheTask();
}
private void doTheTask() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override public void run() {
Platform.runLater(() -> model.stringProperty().set(LocalTime.now().toString()));
}
}, DELAY, PERIOD);
}
}