[英]Binding double value to JavaFX gui
I have implemented fft for my school project(tuner), although, im not able to pass calculated frequency to GUI. 尽管我无法将计算出的频率传递给GUI,但我已经为学校的项目(调谐器)实施了fft。 I tried binding, keyframes, i just cant seem to get a grasp of it, im really new to java. 我尝试了绑定,关键帧,但我似乎无法掌握它,这对Java真的很新。
public class FrequencyBean {
double freq;
private SimpleDoubleProperty value = new SimpleDoubleProperty(this, "value");
public void setValue(double value){
this.value.set(value);
System.out.println(value+" set");
}
public DoubleProperty getDoublePropertyValue(){
System.out.println("gotvals");
return value;
}
public FrequencyBean(){
freq = 10.0;
}
that is part of my controller, also i got reccomended to use something called tight binding or so, which would be abstracting of this class. 这是我控制器的一部分,我也被推荐使用一种叫做紧密绑定的东西,这将是该类的抽象。 Is that good for my code? 这样对我的代码好吗?
This is my main controller: 这是我的主控制器:
public class Controller implements Initializable{
FrequencyBean fbean;
@FXML
private Label otherFq;
@FXML
private Text frequency;
@FXML
private Text sharpFq;
@FXML
private Rectangle sharp6;
@FXML
private Text flatFq;
@FXML
private Rectangle center_rectangle;
@FXML
private Rectangle sharp1;
@FXML
private Rectangle sharp2;
@FXML
private Rectangle sharp3;
@FXML
private Rectangle sharp4;
@FXML
private Rectangle sharp5;
@FXML
private Text centerFq;
@FXML
private Rectangle flat6;
@FXML
private Rectangle flat5;
@FXML
private Rectangle flat4;
@FXML
private Rectangle flat3;
@FXML
private Rectangle flat2;
@FXML
private Rectangle flat1;
@Override
public void initialize(URL location, ResourceBundle resources) {
fbean = new FrequencyBean();
otherFq = new Label();
frequency = new Text();
boolean stop = false;
InputThread input = new InputThread();
Task<Void> in = new Task<Void>() {
@Override
protected Void call() throws Exception {
input.run();
return null;
}
};
Thread th0 = new Thread(in);
th0.start();
frequency.textProperty().bind(fbean.getDoublePropertyValue());
}
Rewrite your FrequencyBean correctly as a 'JavaFX-Bean': 正确地将FrequencyBean重写为“ JavaFX-Bean”:
public class FrequencyBean {
private SimpleDoubleProperty frequency = new SimpleDoubleProperty();
/**
* @param frequency the frequency to set
*/
public void setFrequency(double value){
this.frequency.set(value);
}
/**
* @return the frequency as double
*/
public double getFrequency(){
return this.frequency.get();
}
/**
* @return the frequency property
*/
public DoubleProperty frequencyProperty(){
return value;
}
public FrequencyBean(){
frequency = 10.0;
}
}
As Jame_D pointed it out: don't initialize a control annotated with @FXML. 正如Jame_D指出的那样:不要初始化带有@FXML注释的控件。 Just bind the control in question like so: 像这样绑定有问题的控件:
...
@FXML
TextField tf_Frequency;
...
fbean = new FrequencyBean(20.3);
tfFrequency.textProperty().bind(fbean.frequencyProperty().asString("%.2f"));
Note that this is correct if you need a uni-directional binding. 请注意,如果您需要单向绑定,这是正确的。 There is also a bindBidirectional method. 还有一个bindBidirectional方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.