![](/img/trans.png)
[英]automatically resize TableColumn of TableColumn in TableView with JavaFX -8
[英]Accessing nested properties in JavaFx TableView / 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>
我的list對象有一個getHouse()
,而House有一個getBathroom()
。 不幸的是,這不起作用。 我嘗試了一些拼寫錯誤,但沒有運氣。
我不相信你可以通過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
你看到上面提到的僅僅是一個變量我用指CellDataFeatures
對象在Java中,我們可以用它來獲得該行的數據模型對象的引用(使用getValue()
方法)。 也許cdf
會是更好的選擇,但習慣很難打破。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.