gpt4 book ai didi

java - 如何为 TableView 创建条件 CellValueFactory?

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:12 25 4
gpt4 key购买 nike

我有一个 TableView,其中有一列需要显示我的数据对象中的两个值之一。

在下面的 MCVE 中,我有一个 Person 对象,它可能有也可能没有 nickname 属性。此 nickname 可以填充、为空或 null


Person.java:

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Person {

private IntegerProperty id = new SimpleIntegerProperty();
private StringProperty firstName = new SimpleStringProperty();
private StringProperty lastName = new SimpleStringProperty();
private StringProperty nickname = new SimpleStringProperty();

public Person(int id, String firstName, String lastName, String nickname) {
this.id.set(id);
this.firstName.set(firstName);
this.lastName.set(lastName);
this.nickname.set(nickname);
}

public int getId() {
return id.get();
}

public IntegerProperty idProperty() {
return id;
}

public void setId(int id) {
this.id.set(id);
}

public String getFirstName() {
return firstName.get();
}

public StringProperty firstNameProperty() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName.set(firstName);
}

public String getLastName() {
return lastName.get();
}

public StringProperty lastNameProperty() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName.set(lastName);
}

public String getNickname() {
return nickname.get();
}

public StringProperty nicknameProperty() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname.set(nickname);
}
}

主.java:

import javafx.application.Application;
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.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main 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));

// Build the simple TableView
TableView<Person> tableView = new TableView<>();
TableColumn<Person, String> colId = new TableColumn<>("ID");
TableColumn<Person, String> colName = new TableColumn<>("Name");

// Set the cell value factories
colId.setCellValueFactory(new PropertyValueFactory<>("id"));
colName.setCellValueFactory(new PropertyValueFactory<>("nickname"));

// Add the column to the tableview
tableView.getColumns().addAll(colId, colName);

// Populate the tableview with a couple of samples
tableView.getItems().addAll(
new Person(1, "John", "Williams", null),
new Person(2, "Marty", "McFly", "Chicken"),
new Person(3, "Emmett", "Brown", "Doc"),
new Person(4, "Hans", "Zimmer", "")
);

// Add the table to the scene
root.getChildren().add(tableView);

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

我需要的是一个 CellValueFactory,如果它有值,它将使用 nickname 属性,但如果 nickname,则使用此人的完整名字和姓氏 为空或 null。

如何创建有条件的 CellValueFactory我假设它需要为工厂创建一个新的回调函数,但我不确定它是如何工作的。 p>

最佳答案

作为@kleopatra hints at ,一种方法是在 cellValueFactory 中创建条件绑定(bind)。这是通过 Bindings.when(ObservableBooleanValue) 完成的.

nameCol.setCellValueFactory(features -> {
var firstName = features.getValue().firstNameProperty();
var lastName = features.getValue().lastNameProperty();
var nickname = features.getValue().nicknameProperty();

return Bindings.when(nickname.isEmpty())
.then(firstName.concat(" ").concat(lastName))
.otherwise(nickname);
});

isEmpty()绑定(bind)将 null 值视为空。

Note: If the value of this StringExpression is null, it is considered to be empty.

关于java - 如何为 TableView 创建条件 CellValueFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52669045/

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