首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX TableView编辑未编辑

JavaFX TableView编辑未编辑
EN

Stack Overflow用户
提问于 2017-06-11 22:13:40
回答 1查看 3.2K关注 0票数 0

在JavaFX 8中,我试图让一个单元格在新行添加到表中后进行编辑,以优化用户体验。

代码语言:javascript
运行
复制
    hourTableView.getSelectionModel().select(newHour);
    int row = hourTableView.getSelectionModel().getSelectedIndex();
    hourTableView.edit(row, hourTableAmountTableColumn);

选择了正确的行,但单元格未进入编辑模式。嗯,我看到它发生得很偶然,但几乎不能重现。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2017-06-12 16:06:26

如果您想要在刚插入的行上开始编辑,则该表的行为很奇怪。我可以毫不费力地开始编辑任何行。但是新添加的行并没有像预期的那样工作。我用一个线程延迟了编辑调用,然后它就工作了.请参阅以下示例代码:

代码语言:javascript
运行
复制
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class jfxtest extends Application {

    private ObservableList<Person> data;
    private TableView<Person> tableView;
    private TableColumn<Person, String> firstNameCol;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button addButton = new Button("add new row");
        addButton.setOnMouseClicked(event -> addRow());
        VBox vbox = new VBox(addButton, createContent());
        primaryStage.setScene(new Scene(vbox));
        primaryStage.show();
    }

    private void addRow() {
        data.add(new Person("peter", "parker", "spiderman@aol.com"));
        new Thread(() -> {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Platform.runLater(() -> {
                int row = data.size() - 1;
                tableView.getSelectionModel().select(row);
                tableView.getSelectionModel().focus(row);
                tableView.edit(row, firstNameCol);
            });
        }).start();
    }

    private TableView createContent() {
        data = FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com")
        );
        firstNameCol = new TableColumn<>("First");
        firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        firstNameCol.setEditable(true);
        firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
        firstNameCol.setOnEditCommit(
            t -> t.getTableView().getItems().get(
                t.getTablePosition().getRow()).firstNameProperty().setValue(t.getNewValue())
        );

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last");
        lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));

        tableView = new TableView<>();
        tableView.setEditable(true);
        tableView.setItems(data);
        tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
        return tableView;
    }


    public class Person {
        private StringProperty firstName;
        private StringProperty lastName;
        private StringProperty email;

        Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

        public StringProperty emailProperty() {
            return email;
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44484813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档