JavaFX绑定到自定义对象

时间:2015-01-13 04:50:44

标签: binding javafx-2

我正在学习Java FX中的绑定概念。我有一个Person类,其firstName和lastName属于SimpleStringProperty类型。在我的FXML中,我有 两个文本字段和一个标签。当用户输入名字和姓氏时,我需要在标签中显示为全名。

我在尝试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 );
        }
    }

以下是Person类

public class Person{
    private SimpleStringProperty firstName=new SimpleStringProperty();
    private SimpleStringProperty lastName=new SimpleStringProperty();

    public String getFirstName(){
        return firstNameProperty().get();
    }

    public void setFirstName( String firstName ){
        firstNameProperty().set( firstName );
    }

    public SimpleStringProperty firstNameProperty(){
        return firstName;
    }

    public String getLastName(){
        return lastNameProperty().get();
    }

    public void setLastName( String lastName ){
        lastNameProperty().set( lastName );
    }

    public SimpleStringProperty lastNameProperty(){
        return lastName;
    }
}

这是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>

1 个答案:

答案 0 :(得分:0)

在您的代码中,您从未致电public void setPerson( Person person ){,因此ObjectProperty person保留值nullperson.get()返回null