访问JavaFx TableView / TableColumn中的嵌套属性

时间:2019-08-14 19:13:34

标签: java javafx properties tablecolumn

我有一个TableView,并且按如下方式访问列表对象的属性。这样就可以了。

    <TableColumn fx:id="dateColumn" editable="false" prefWidth="135.0" text="Date">
        <cellValueFactory>
            <PropertyValueFactory property="date" />
        </cellValueFactory>
    </TableColumn>

但是,我想访问对象的嵌套属性,例如:

        <TableColumn prefWidth="100.0" text="Course">
            <cellValueFactory>
                <PropertyValueFactory property="house.bathroom"/>
            </cellValueFactory>
        </TableColumn>

其中我的列表对象有一个getHouse(),而House有一个getBathroom()。不幸的是,这不起作用。我尝试了几种拼写形式,但没有运气。

1 个答案:

答案 0 :(得分:5)

我不相信您可以通过FXML来做到这一点(尽管我可能错了)。

假设您使用的是正确的JavaFX属性,只需在控制器中设置自己的CellValueFactory,如下所示:

bathroomColumn.setCellValueFactory(tf -> tf.getValue().getHouse().bathroomProperty());

由于尚不清楚您想在浴室列中显示什么,因此将返回BathroomProperty

但是,如果您想在Bathroom对象中返回一个属性,则只需调用getBathroom().yourProperty()

bathroomColumn.setCellValueFactory(tf -> tf.getValue().getHouse().getBathroom().myStringProperty());

也许一个例子可能有助于说明这一概念:

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewValues extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple Interface
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        // Simple TableView
        TableView<Person> personTableView = new TableView<>();
        TableColumn<Person, String> colName = new TableColumn<>("Name");
        TableColumn<Person, String> colCar = new TableColumn<>("Car");

        // Setup the CellValueFactories
        colName.setCellValueFactory(tf -> tf.getValue().nameProperty());
        colCar.setCellValueFactory(tf -> tf.getValue().getCar().modelProperty());

        personTableView.getColumns().addAll(colName, colCar);

        root.getChildren().add(personTableView);

        // Sample Data
        personTableView.getItems().addAll(
                new Person("Jack", new Car("Accord")),
                new Person("John", new Car("Mustang")),
                new Person("Sally", new Car("Yugo"))
        );

        // Show the stage
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Sample");
        primaryStage.show();
    }
}

class Person {

    private final StringProperty name = new SimpleStringProperty();
    private final ObjectProperty<Car> car = new SimpleObjectProperty<>();

    public Person(String name, Car car) {
        this.name.set(name);
        this.car.set(car);
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public StringProperty nameProperty() {
        return name;
    }

    public Car getCar() {
        return car.get();
    }

    public void setCar(Car car) {
        this.car.set(car);
    }

    public ObjectProperty<Car> carProperty() {
        return car;
    }
}

class Car {
    private final StringProperty model = new SimpleStringProperty();

    public Car(String model) {
        this.model.set(model);
    }

    public String getModel() {
        return model.get();
    }

    public void setModel(String model) {
        this.model.set(model);
    }

    public StringProperty modelProperty() {
        return model;
    }
}

结果:

screenshot


如果您使用纯Java Bean,则过程类似。但是,您无需在数据模型类中将CellValueProperty设置为Property,而是创建一个新的StringProperty内联并向其传递所需的bean值。

因此,在上面的Car示例中,您应该这样做:

colName.setCellValueFactory(tf -> new SimpleStringProperty(tf.getValue().getName()));
colCar.setCellValueFactory(tf -> new SimpleStringProperty(tf.getValue().getCar().getModel()));
  

侧面说明:您在上面看到的tf只是一个变量,我使用它来引用Java中的CellDataFeatures对象,我们可以使用该变量来获取对该行的数据模型对象(使用getValue()方法)。也许cdf是一个更好的选择,但是习惯很难打破。

相关问题