gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:59:38 26 4
gpt4 key购买 nike

我有一个 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()。不幸的是,这不起作用。我尝试了一些拼写变体,但没有成功。

最佳答案

我不相信你可以通过 FXML 做到这一点(尽管我可能是错的)。

假设您正在使用正确的 JavaFX 属性,您只需在 Controller 中设置自己的 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()));

Side Note: The tf you see referenced above is just a variable I use to refer to the CellDataFeatures object in Java which we can use to get a reference to that row's data model object (using the getValue() method). Perhaps cdf would be a better choice, but habits are hard to break.

关于java - 访问 JavaFx TableView/TableColumn 中的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com