简体   繁体   中英

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. I tried binding, keyframes, i just cant seem to get a grasp of it, im really new to 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':

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM