访问JavaFx TableView/TableColumn中的嵌套属性 [英] Accessing nested properties in JavaFx TableView / TableColumn
问题描述
我有一个TableView,并且按如下方式访问列表对象的属性.这样就可以了.
I havea TableView, and I access properties of the list's objects as follows. This works just fine.
<TableColumn fx:id="dateColumn" editable="false" prefWidth="135.0" text="Date">
<cellValueFactory>
<PropertyValueFactory property="date" />
</cellValueFactory>
</TableColumn>
但是,我想访问对象的嵌套属性,例如:
However, I'd like to access a nested property of the object, for example:
<TableColumn prefWidth="100.0" text="Course">
<cellValueFactory>
<PropertyValueFactory property="house.bathroom"/>
</cellValueFactory>
</TableColumn>
其中我的列表对象有一个getHouse()
,而House有一个getBathroom()
.不幸的是,这是行不通的.我尝试了一些拼写变化,但没有运气.
where my list object has a getHouse()
, and House has a getBathroom()
. Unfortunately, this doesn't work. I've tried a few spelling variations, but no luck.
推荐答案
我不相信您可以通过FXML来做到这一点(尽管我可能错了).
I don't believe you can do this via FXML (though I could be wrong).
假设您使用的是正确的JavaFX属性,则只需在控制器中设置自己的CellValueFactory
即可,如下所示:
Assuming you're using proper JavaFX properties, you'd just setup your own CellValueFactory
in the controller like so:
bathroomColumn.setCellValueFactory(tf -> tf.getValue().getHouse().bathroomProperty());
由于尚不清楚您要在浴室列中显示什么,因此将返回BathroomProperty
.
Since it's unclear what you want to display in the bathroom column, this would return the BathroomProperty
.
但是,如果您想在Bathroom
对象的内中返回属性,则只需调用getBathroom().yourProperty()
:
If, however, you wanted to return a property within the Bathroom
object, you'd simply call getBathroom().yourProperty()
as well:
bathroomColumn.setCellValueFactory(tf -> tf.getValue().getHouse().getBathroom().myStringProperty());
也许一个例子可能有助于说明这一概念:
Perhaps an example might help to demonstrate the concept:
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值.
If you're using plain Java beans, the process is similar. But instead of setting the CellValueProperty
to a Property
within your data model classes, you'd create a new StringProperty
inline and pass it the bean value you need.
因此,在上面的Car
示例中,您将改为执行以下操作:
So, in the Car
example above, you'd do this instead:
colName.setCellValueFactory(tf -> new SimpleStringProperty(tf.getValue().getName()));
colCar.setCellValueFactory(tf -> new SimpleStringProperty(tf.getValue().getCar().getModel()));
Side Note: The
tf
you see referenced above is just a variable I use to refer to theCellDataFeatures
object in Java which we can use to get a reference to that row's data model object (using thegetValue()
method). Perhapscdf
would be a better choice, but habits are hard to break.
这篇关于访问JavaFx TableView/TableColumn中的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!