テーブルビューの条件付きCellValueFactoryを作成する方法?

ゼファー:

私が持っているTableView自分のデータ・オブジェクトから2つの値のいずれかを表示する必要がある列を持つが。

以下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);
    }
}

Main.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私はそれが工場のための新しいコールバック関数を作成する必要が想定していますが、私は確かにどのような作品ではないと思います。

なスロー:

@kleopatraのようにヒント、これを実行するための1つの方法はで結合条件を作成することですcellValueFactoryこれは、を介して行われます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()結合は考慮null値が空であることを。

注:この値は場合StringExpressionnull、空であると考えられています。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=190404&siteId=1