在JavaFX TableView中插入行并开始编辑工作不正常

在JavaFX TableView中插入行并开始编辑工作不正常,javafx,tableview,Javafx,Tableview,我们正在运行一个包含一些可编辑表视图的JavaFX应用程序。一个新请求的功能是:一个按钮,在当前选定的行下方添加一个新行,并立即开始编辑该行的第一个单元格 我们实现了这个功能,虽然没有那么复杂,但我们遇到了一个非常奇怪的行为,在调查了几天之后,我们仍然不知道出了什么问题 发生的情况是,当单击按钮时,它会添加一个新行,但开始编辑第一个单元格,而不是新创建的行,而是任意其他行。不幸的是,这个问题不是100%可复制的。有时,它会按预期工作,但最常见的情况是,新添加的行下面的行会被编辑,但有时甚至在当前

我们正在运行一个包含一些可编辑表视图的JavaFX应用程序。一个新请求的功能是:一个按钮,在当前选定的行下方添加一个新行,并立即开始编辑该行的第一个单元格

我们实现了这个功能,虽然没有那么复杂,但我们遇到了一个非常奇怪的行为,在调查了几天之后,我们仍然不知道出了什么问题

发生的情况是,当单击按钮时,它会添加一个新行,但开始编辑第一个单元格,而不是新创建的行,而是任意其他行。不幸的是,这个问题不是100%可复制的。有时,它会按预期工作,但最常见的情况是,新添加的行下面的行会被编辑,但有时甚至在当前选定行之前和之后有完全不同的行

下面您可以找到JavaFXTableView的精简版本的源代码,该版本可用于查看问题。如前所述,它不是100%可复制的。要查看问题,必须多次添加新行。有时,在上下滚动几次表格时,问题会更频繁地出现

感谢您的帮助

提示:我们已经经常使用Platform.runlater(),将按钮的操作实现放在runlater()中,但尽管问题在那时发生的次数较少,但它从未完全消失

TableView:

package tableview;

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class SimpleTableViewTest extends Application {

    private final ObservableList<Person> data = FXCollections.observableArrayList(createData());

    private final TableView table = new TableView();

    public static void main(String[] args) {
        launch(args);
    }

    private static List<Person> createData() {
        List<Person> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add(new Person("Jacob", "Smith", "jacob.smith_at_example.com", "js_at_example.com"));
        }

        return data;
    }

    @Override
    public void start(Stage stage) {

        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(700);
        stage.setHeight(550);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        // Create a customer cell factory so that cells can support editing.
        Callback<TableColumn, TableCell> cellFactory = (TableColumn p) -> {
            return new EditingCell();
        };

        // Set up the columns
        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
        firstNameCol.setCellFactory(cellFactory);

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
        lastNameCol.setCellFactory(cellFactory);
        lastNameCol.setEditable(true);

        TableColumn primaryEmailCol = new TableColumn("Primary Email");
        primaryEmailCol.setMinWidth(200);
        primaryEmailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("primaryEmail"));
        primaryEmailCol.setCellFactory(cellFactory);
        primaryEmailCol.setEditable(false);

        TableColumn secondaryEmailCol = new TableColumn("Secondary Email");
        secondaryEmailCol.setMinWidth(200);
        secondaryEmailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("secondaryEmail"));
        secondaryEmailCol.setCellFactory(cellFactory);

        // Add the columns and data to the table.
        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, primaryEmailCol, secondaryEmailCol);
        table.setEditable(true);

        // --- Here comes the interesting part! ---
        //
        // A button that adds a row below the currently selected one
        // and immediatly starts editing it.
        Button addAndEdit = new Button("Add and edit");
        addAndEdit.setOnAction((ActionEvent e) -> {
            int idx = table.getSelectionModel().getSelectedIndex() + 1;

            data.add(idx, new Person());
            table.getSelectionModel().select(idx);
            table.edit(idx, firstNameCol);
        });

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.getChildren().addAll(label, table, addAndEdit);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

}

按钮操作实现的正确代码如下所示。解决所述问题的重要一行是“table.layout()”。 非常感谢费边

addAndEdit.setOnAction((ActionEvent e) -> {
    int idx = table.getSelectionModel().getSelectedIndex() + 1;

    data.add(idx, new Person());
    table.getSelectionModel().select(idx);

    table.layout();

    table.edit(idx, firstNameCol);
 });

它似乎是重复的请参见此处可能的重复它肯定不是重复的这可能是一个问题,在调用
edit
时单元格/行未正确初始化。因为这是在布局
平台期间发生的。如果在下一个布局过程之前运行
Runnable
,runLater
可能会失败。如果在
TableView
上调用
applyCss()
+
layout()
有帮助,您可以检查……我不知道如何解决这个问题,但是,是否会因为在JavaFX中重用了TableCell而发生这种情况?单击“添加并编辑”按钮后向下滚动,似乎正在编辑的单元格有一个固定的间隔,就像每16个单元格中有一个单元格一样。可能只有一个单元格被编辑,但它被反复使用,导致在滚动表格时以不同的索引呈现。谢谢,这真的很有帮助,我错过了“table.layout()”
package tableview;

import javafx.beans.property.SimpleStringProperty;

public class Person {
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty primaryEmail;
    private final SimpleStringProperty secondaryEmail;

    public Person() {
        this(null, null, null, null);
    }

    public Person(String firstName, String lastName, String primaryEmail, String secondaryEmail) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
        this.primaryEmail = new SimpleStringProperty(primaryEmail);
        this.secondaryEmail = new SimpleStringProperty(secondaryEmail);
    }

    public SimpleStringProperty firstNameProperty() {
        return firstName;
    }

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

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

    public String getPrimaryEmail() {
        return primaryEmail.get();
    }

    public SimpleStringProperty getPrimaryEmailProperty() {
        return primaryEmail;
    }

    public String getSecondaryEmail() {
        return secondaryEmail.get();
    }

    public SimpleStringProperty getSecondaryEmailProperty() {
        return secondaryEmail;
    }

    public SimpleStringProperty lastNameProperty() {
        return lastName;
    }

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

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

    public void setPrimaryEmail(String primaryEmail) {
        this.primaryEmail.set(primaryEmail);
    }

    public void setSecondaryEmail(String secondaryEmail) {
        this.secondaryEmail.set(secondaryEmail);
    }
}
addAndEdit.setOnAction((ActionEvent e) -> {
    int idx = table.getSelectionModel().getSelectedIndex() + 1;

    data.add(idx, new Person());
    table.getSelectionModel().select(idx);

    table.layout();

    table.edit(idx, firstNameCol);
 });