我不相信您可以通过 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;
}
}
结果:
如果您使用的是纯 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 会是更好的选择,但习惯很难改掉。