2017-03-24 11 views
2

選択した項目を取得して設定できる行を自動的に選択するには、すでに列にあるボタンをクリックする必要があります。それ以外の場合は、nullpointerを生成します。私はボタンをクリックすると直接行を選択するリスナーを追加することを考えていましたが、わかりません。ボタン付きJavafxテーブルビューcolumviewボタン自動選択行をクリックする必要があります

スクリーンショット:

enter image description here

これはコードです:

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. 
    } 

答えて

0

私はあなたを意味cellFactoryで使用されるカスタムTableCell、でこれをやっていると仮定TableRowを使用してテーブル項目を取得することができます:

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); 
    } 
} 
+0

本当にありがとう兄弟!!!! :D クイックアンサーに感謝します! –

関連する問題