包含ListView更新的JavaFX TableView,CellValueFactory

时间:2016-02-24 16:44:09

标签: listview javafx-8

我有一个引擎,它包含列表中的部分,如:

enter image description here

更新 我更新了文本以提供示例

我想要一个包含2个列(名称,部分)的引擎TableView。我希望部件Column在TableCell中呈现为ListView,因此我重写了列的CellFactory。

import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty;

public class Part {
    private final SimpleStringProperty name = new SimpleStringProperty("");
    // Reference to the parent engine
    private final ObjectProperty<Engine> engine= new SimpleObjectProperty<>(null);

    public Part(String name, Engine engine) {
        this.setName(name);
        this.setEngine(engine);
    }

    public ObjectProperty<Engine> engineProperty() {
        return engine;
    }

    public Engine getEngine() {
        return engine.get();
    }

    public void setEngine(Engine engine) {
        this.engine.set(engine);
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    } }

引擎类:

import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.util.Callback;

public class Engine {
    private final SimpleStringProperty name = new SimpleStringProperty("");

    Callback<Part, Observable[]> partsExtractor = part -> new Observable[] {
            part.nameProperty(),
            part.engineProperty()
    };

    private final ObservableList<Part> parts = FXCollections.observableArrayList(partsExtractor);

    public Engine(String name) {
        this.setName(name);
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getName() {
        return name.get();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public ObservableList<Part> getParts() {
        return parts;
    }
}

申请表:

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * If you edit the field name of the engine on the table the changed name does not reflect on the listView within the
 * table cell, it should actually show the new name of the engine
 */

class PartListCell extends ListCell<Part> {
    private Label name = new Label();

    public PartListCell() {

    }

    @Override
    protected void updateItem(Part part, boolean empty) {
        super.updateItem(part, empty);

        if(!empty && part != null) {
            name.setText(part.getEngine().getName() + " / " + part.getName());
            setGraphic(name);
        } else {
            setGraphic(null);
        }
    }
}

class PartsTableCell extends TableCell<Engine, ObservableList<Part>> {
    private final ListView<Part> listView = new ListView<>();

    public PartsTableCell() {
        listView.setCellFactory(listView -> new PartListCell());
    }

    @Override
    protected void updateItem(ObservableList<Part> parts, boolean empty) {
        if(!empty && parts != null){
            listView.setItems(parts);
            setGraphic(listView);
        } else {
            setGraphic(null);
        }
    }
}

public class Main extends Application {

    Callback<Engine, Observable[]> engineExtractor = engine -> new Observable[] {
            engine.nameProperty(),
    };

    private final ObservableList<Engine> engines = FXCollections.observableArrayList(engineExtractor);

    public Main() {
        Engine engine = new Engine("Engine-1");
        Part part1 = new Part("Part-1", engine);
        Part part2 = new Part("Part-1", engine);

        engine.getParts().addAll(part1, part2);

        engines.add(engine);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane root = new StackPane();

        TableColumn<Engine, String> nameColumn = new TableColumn<>("Name");
        TableColumn<Engine, ObservableList<Part>> partsColumn = new TableColumn<>("Parts");

        // Set the Cell Factories
        nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        // Make an editable text field
        nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        nameColumn.setOnEditCommit(event -> event.getTableView().getItems().get(
                event.getTablePosition().getRow()).setName(event.getNewValue()));

        partsColumn.setCellFactory(param -> new PartsTableCell());
        partsColumn.setCellValueFactory(new PropertyValueFactory<>("parts"));

        TableView<Engine> tableView = new TableView<>(engines);
        tableView.setEditable(true);
        tableView.getColumns().add(nameColumn);
        tableView.getColumns().add(partsColumn);

        // Set the stage
        root.getChildren().add(tableView);
        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }
}

但我如何创建CellValueFactory,因为

partsColumn.setCellValueFactory(new PropertyValueFactory<>("parts"));

不会这样做,因为它会被包装,如果我更改零件清单中的零件,它不会更新零件listView中的零件。我需要创建一个Callback,它提供部件列表作为Table Cell的可观察对象,但我不知道如何做到这一点。有人帮忙吗?

2 个答案:

答案 0 :(得分:2)

您的PartListCell只是将其标签的文本设置为与部件名称连接的引擎名称的当前值。如果单元格显示的部分发生更改,它将更新(将再次调用updateItem(...)),但单元格和标签都不会观察引擎的名称。因此,如果引擎名称发生变化,则单元格不知道需要更新。

修复只是将标签的文本绑定到适当的StringExpression

class PartListCell extends ListCell<Part> {
    private Label name = new Label();

    public PartListCell() {

    }

    @Override
    protected void updateItem(Part part, boolean empty) {
        super.updateItem(part, empty);

        name.textProperty().unbind();

        if(!empty && part != null) {
            name.textProperty().bind(part.getEngine().nameProperty().concat(" / ").concat(part.nameProperty()));
            setGraphic(name);
        } else {
            name.setText(null);
            setGraphic(null);
        }
    }
}

答案 1 :(得分:2)

不是答案,评论的时间太长了:

如果我理解你的要求还是不太确定:但是这里有一个我认为你想要的提取器:

Callback<Part, Observable[]> partsExtractor = part -> new Observable[] {
            part.nameProperty(), part.engineProperty().get().nameProperty() };