Recently i have started to learn Java FX. Right now i'm practicing properties binding. I use Inetellij IDE at the moment. I have a problem that i cannot solve and i don't understand what is wrong. I declared some Properties eg. StringProperty in Person class and created constructor and getters and setters as well. I'm trying to bind (or bindBidirectional) those properties in initialize method but i can't. When i put (as in the code) person.getNameProperty as an argument of a method bind or bindBidirectional there is an error. Could anyone help me with this? What should i do? I saw quite similar code written in Netbeans IDE and there was no such problem.
MAIN
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 350));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
CONTROLLER:
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private TextField nameTextField;
//other declarations
private Person person = new Person();
@Override
public void initialize(URL location, ResourceBundle resources) {
nameTextField.textProperty().bindBidirectional(person.getNameProperty());
}
}
CLASS PERSON:
package sample;
import javafx.beans.property.*;
import java.time.LocalDate;
/**
* Created by User on 2018-02-16.
*/
public class Person {
//person's name
private StringProperty nameProperty = new SimpleStringProperty();
//Other properties
//consturctor
public Person() {
}
public String getNameProperty() {
return nameProperty.get();
}
public StringProperty namePropertyProperty() {
return nameProperty;
}
public void setNameProperty(String nameProperty) {
this.nameProperty.set(nameProperty);
} }
FXML file looks fine - i just run the layout.
You don't bind to a string you bind to the property itself.
nameTextField.textProperty().bindBidirectional(person.namePropertyProperty());
And going to throw in some personal opinion here ;) IntelliJ is THE BEST IDE for Java development. There's your plug for the day.
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.