带有按钮的Javafx tableview columview我需要单击按钮自动选择行

Javafx tableview columview with button I need to click button autoselect row

我需要单击列中已有的按钮以自动 select 行,以便能够获取和设置 selected 项目。否则,生成空指针。我想添加一个监听器,当我直接点击按钮 select 该行时,但我不知道。

截图:

这是代码:

botonVisitar.setOnAction((ActionEvent event) -> {

    TextInputDialog dialog1 = new TextInputDialog();
    Stage stage1 = (Stage) dialog1.getDialogPane().getScene().getWindow();
    stage1.getIcons().add(new Image(this.getClass().getResource("icono.jpg").toString()));
    dialog1.setTitle("Visita:");
    dialog1.setContentText("Ingresar Tipo de visita: (por ejemplo: llamada, mail, mensaje, etc)");
    Optional<String> result1 = dialog1.showAndWait();
    if (result1.isPresent()) {

        TablaVisita.getSelectionModel().getSelectedItem().setTipoVisita(result1.get());
        TablaVisita.getSelectionModel().getSelectedItem().setFechaVisita(LocalDate.now());

        //If it is not selected i get nullPointer. 
    }

我假设您是在与 cellFactory 一起使用的自定义 TableCell 中执行此操作,这意味着您可以使用 TableRow 来获取 table 项目:

new TableCell<MyItem, Void>() {
    private final Button botonVisitar = new Button("Visitar");

    {
        botonVisitar.setOnAction((ActionEvent event) -> {

            ...

            if (result1.isPresent()) {
                MyItem item = (MyItem) getTableRow().getItem();

                item.setTipoVisita(result1.get());
                item.setFechaVisita(LocalDate.now());
            }
        });

    }

    @Override
    public void updateItem(Void item, boolean empty) {
        super.updateItem(item, empty);
        setGraphic(empty ? null : botonVisitar);
    }
}