0
Java FX에서 바인딩 개념을 배우고 있습니다. firstName과 lastName이 SimpleStringProperty 유형 인 Person 클래스가 있습니다. 내 FXML에는 두 개의 텍스트 필드와 레이블이 있습니다. 사용자가 이름과 성을 입력하면 이름으로 레이블에 표시해야합니다.JavaFX 사용자 지정 개체에 바인딩
person.get().firstNameProperty()
을 시도하면 NullPointerException이 발생합니다. 이 샘플 요구 사항을 어떻게 구현합니까? 다음은
public class CustomControl extends Pane{
@FXML
private TextField firstNameTextField, lastNameTextField;
@FXML
private Label fullNameLabel;
private ObjectProperty<Person> person = new SimpleObjectProperty<Person>(this, "person");
public CustomControl(){
try{
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("EnterDetails.fxml"));
loader.setController(this);
loader.load();
registerListeners();
}
catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*/
private void registerListeners(){
firstNameTextField.textProperty().bind(person.get().firstNameProperty());
lastNameTextField.textProperty().bind(person.get().lastNameProperty());
fullNameLabel.textProperty().bind(Bindings.concat(person.get().firstNameProperty()).concat(person.get().lastNameProperty()));
}
public ObjectProperty<Person> personProperty(){
return person;
}
public Person getPerson(){
return personProperty().get();
}
public void setPerson(Person person){
personProperty().set(person);
}
}
이 당신이
public void setPerson(Person person){
그래서
ObjectProperty
person
이 값
null
및
person.get()
반환
null
보유를 호출되지 않습니다 코드에서 FXML 페이지
<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
</rowConstraints>
<columnConstraints>
<ColumnConstraints minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<children>
<Label text="FirstName" />
<Label text="Last Name" GridPane.rowIndex="1" />
<TextField fx:id="firstNameTextField" GridPane.columnIndex="1" />
<TextField fx:id="lastNameTextField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="fullNameLabel" GridPane.rowIndex="2" />
</children>
</GridPane>