单击占位符按钮即可编辑JavaFX 8表格单元格

单击占位符按钮即可编辑JavaFX 8表格单元格,javafx,javafx-8,Javafx,Javafx 8,在我的TableView中,当表中没有可用项时,我使用一个按钮作为占位符。单击此按钮,我想添加一个新行,然后使此行中的第一个单元格可编辑。我能够成功地添加新行,但使第一个单元格可编辑的操作不起作用。我尝试使用Platform.runLater开始编辑,但也不起作用。下面是我试图做的代码。任何帮助都将不胜感激。谢谢 public class TableEditTest extends Application { @Override public void start(Stage

在我的
TableView
中,当表中没有可用项时,我使用一个按钮作为占位符。单击此按钮,我想添加一个新行,然后使此行中的第一个单元格可编辑。我能够成功地添加新行,但使第一个单元格可编辑的操作不起作用。我尝试使用Platform.runLater开始编辑,但也不起作用。下面是我试图做的代码。任何帮助都将不胜感激。谢谢

public class TableEditTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableColumn<Person, String> colA = new TableColumn<>("Name");
        colA.setCellFactory(TextFieldTableCell.forTableColumn());
        colA.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Person, String> colB = new TableColumn<>("Age");
        colB.setCellFactory(TextFieldTableCell.forTableColumn());
        colB.setCellValueFactory(new PropertyValueFactory<>("age"));

        TableColumn<Person, String> colC = new TableColumn<>("Gender");
        colC.setCellValueFactory(new PropertyValueFactory<>("gender"));
        colC.setCellFactory(TextFieldTableCell.forTableColumn());

        TableView<Person> tableView = new TableView<>();
        tableView.setEditable(true);
        tableView.getColumns().addAll(colA, colB, colC);

        Button btn = new Button();
        btn.setText("Add Item");
        btn.setOnAction((ActionEvent event) -> {
            tableView.getItems().add(new Person());

            // Edit the first cell
            Platform.runLater(() -> {
                tableView.edit(0, colA);
            });
        });

        tableView.setPlaceholder(btn);

        StackPane root = new StackPane();
        root.getChildren().add(tableView);

        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
公共类TableEditTest扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
TableColumn=新的TableColumn(“名称”);
colA.setCellFactory(TextFieldTableCell.forTableColumn());
colA.setCellValueFactory(新属性ValueFactory(“名称”);
TableColumn colB=新的TableColumn(“年龄”);
colB.setCellFactory(TextFieldTableCell.forTableColumn());
colB.setCellValueFactory(新财产价值工厂(“年龄”);
TableColumn colC=新的TableColumn(“性别”);
colC.setCellValueFactory(新财产价值工厂(“性别”);
colC.setCellFactory(TextFieldTableCell.forTableColumn());
TableView TableView=新建TableView();
tableView.setEditable(true);
tableView.getColumns().addAll(colA、colB、colC);
按钮btn=新按钮();
btn.setText(“添加项”);
btn.setOnAction((ActionEvent事件)->{
tableView.getItems().add(newperson());
//编辑第一个单元格
Platform.runLater(()->{
tableView.edit(0,colA);
});
});
tableView.setPlaceholder(btn);
StackPane root=新的StackPane();
root.getChildren().add(tableView);
场景=新场景(根,300,250);
setTitle(“你好,世界!”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}