title: TableView autorefresh
body: <p>I am working on an JavaFx project and am facing a problem.</p>
  
  <p>So there is my <code>Controller</code> class :</p>
  
  <pre><code>package ArticleGUI;
  
  import Entities.Article;
  import Gestion.GestionArticle;
  import com.sun.prism.impl.Disposer.Record;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.ResourceBundle;
  import javafx.beans.property.SimpleBooleanProperty;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Button;
  import javafx.scene.control.Hyperlink;
  import javafx.scene.control.TableCell;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.util.Callback;
  
  public class ListeArticleFXMLController extends TableCell&lt;Record, Boolean&gt; implements Initializable{
  
      @FXML
      private TableView&lt;Article&gt; tabArt;
      @FXML
      private TableColumn&lt;Article, Integer&gt; refCol;
      @FXML
      private TableColumn&lt;Article, Hyperlink&gt; titreCol;
      @FXML
      private TableColumn&lt;Article, String&gt; descCol;
      @FXML
      private TableColumn&lt;Article, String&gt; contCol;
      @FXML
      private TableColumn&lt;Article, String&gt; medCol;
      @FXML
      private TableColumn supCol;
      @FXML
      private TableColumn modifCol;
      @FXML
      private TableColumn affichCol;
      @FXML
      private Button reload;
  
  
      GestionArticle ga = new GestionArticle();
      ArrayList&lt;Article&gt; listA = (ArrayList&lt;Article&gt;) ga.afficherArticle();
      public ObservableList&lt;Article&gt; articleData = FXCollections.observableArrayList(listA);
  
  
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
  
          tabArt.setItems(articleData);
  
          refCol.setCellValueFactory( new PropertyValueFactory&lt;Article,Integer&gt;("Reference"));
  
          titreCol.setCellValueFactory(
                     new PropertyValueFactory&lt;Article,Hyperlink&gt;("Nom"));
  
          descCol.setCellValueFactory(
                     new PropertyValueFactory&lt;Article,String&gt;("Description"));
  
          contCol.setCellValueFactory(
                     new PropertyValueFactory&lt;Article,String&gt;("Contenu"));
  
          medCol.setCellValueFactory(
                     new PropertyValueFactory&lt;Article,String&gt;("IdMed"));
  
  
          supCol.setCellValueFactory(
                  new Callback&lt;TableColumn.CellDataFeatures&lt;Record, Boolean&gt;, ObservableValue&lt;Boolean&gt;&gt;() {
  
              @Override
              public ObservableValue&lt;Boolean&gt; call(TableColumn.CellDataFeatures&lt;Record, Boolean&gt; p) {
                  return new SimpleBooleanProperty(p.getValue() != null);
              }
          });
  
          supCol.setCellFactory(
                  new Callback&lt;TableColumn&lt;Record, Boolean&gt;, TableCell&lt;Record, Boolean&gt;&gt;() {
  
              @Override
              public TableCell&lt;Record, Boolean&gt; call(TableColumn&lt;Record, Boolean&gt; p) {
                  return new DelButton();
              }
  
          });
  
  
          modifCol.setCellValueFactory(
                  new Callback&lt;TableColumn.CellDataFeatures&lt;Record, Boolean&gt;, ObservableValue&lt;Boolean&gt;&gt;() {
  
              @Override
              public ObservableValue&lt;Boolean&gt; call(TableColumn.CellDataFeatures&lt;Record, Boolean&gt; p) {
                  return new SimpleBooleanProperty(p.getValue() != null);
              }
          });
  
          modifCol.setCellFactory(
                  new Callback&lt;TableColumn&lt;Record, Boolean&gt;, TableCell&lt;Record, Boolean&gt;&gt;() {
  
              @Override
              public TableCell&lt;Record, Boolean&gt; call(TableColumn&lt;Record, Boolean&gt; p) {
                  return new ModifButton();
              }
  
          });
  
          affichCol.setCellValueFactory(
                  new Callback&lt;TableColumn.CellDataFeatures&lt;Record, Boolean&gt;, ObservableValue&lt;Boolean&gt;&gt;() {
  
              @Override
              public ObservableValue&lt;Boolean&gt; call(TableColumn.CellDataFeatures&lt;Record, Boolean&gt; p) {
                  return new SimpleBooleanProperty(p.getValue() != null);
              }
          });
  
          affichCol.setCellFactory(
                  new Callback&lt;TableColumn&lt;Record, Boolean&gt;, TableCell&lt;Record, Boolean&gt;&gt;() {
  
              @Override
              public TableCell&lt;Record, Boolean&gt; call(TableColumn&lt;Record, Boolean&gt; p) {
                  return new AffichButton();
              }
  
          });
  
      }
  
      public void refresh(){
          articleData.clear();
          List&lt;Article&gt; la=new ArrayList&lt;&gt;();
          la=ga.afficherArticle();
          articleData.setAll(la);
          tabArt.setItems(articleData);
      }
  
      public void setTabArt(TableView&lt;Article&gt; tabArt) {
          this.tabArt = tabArt;
      }
  
      public TableColumn&lt;Article, Integer&gt; getRefCol() {
          return refCol;
      }
  
      public void setRefCol(TableColumn&lt;Article, Integer&gt; refCol) {
          this.refCol = refCol;
      }
  
      public TableColumn&lt;Article, Hyperlink&gt; getTitreCol() {
          return titreCol;
      }
  
      public void setTitreCol(TableColumn&lt;Article, Hyperlink&gt; titreCol) {
          this.titreCol = titreCol;
      }
  
      public TableColumn&lt;Article, String&gt; getDescCol() {
          return descCol;
      }
  
      public void setDescCol(TableColumn&lt;Article, String&gt; descCol) {
          this.descCol = descCol;
      }
  
      public TableColumn&lt;Article, String&gt; getContCol() {
          return contCol;
      }
  
      public void setContCol(TableColumn&lt;Article, String&gt; contCol) {
          this.contCol = contCol;
      }
  
      public TableColumn&lt;Article, String&gt; getMedCol() {
          return medCol;
      }
  
      public void setMedCol(TableColumn&lt;Article, String&gt; medCol) {
          this.medCol = medCol;
      }
  
      public ObservableList&lt;Article&gt; getArticleData() {
          return articleData;
      }
  
  
  
  }
  </code></pre>
  
  <p>And there is my <code>Button</code> class :</p>
  
  <pre><code>package ArticleGUI;
  
  import Entities.Article;
  import Gestion.GestionArticle;
  import com.sun.prism.impl.Disposer.Record;
  import java.util.Optional;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Alert;
  import javafx.scene.control.Button;
  import javafx.scene.control.ButtonType;
  import javafx.scene.control.TableCell;
  
  public class DelButton extends TableCell&lt;Record, Boolean&gt; {
  
      ListeArticleFXMLController l = new ListeArticleFXMLController();
      final Button supp = new Button("Supprimer");
  
      public DelButton() {
          supp.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              @Override
              public void handle(ActionEvent t) {
                  GestionArticle ga = new GestionArticle();
                  try {
                      Article currentArt = (Article) DelButton.this.getTableView().getItems().get(DelButton.this.getIndex());
                      Alert a1 = new Alert(Alert.AlertType.CONFIRMATION);
                      a1.setTitle("Suppresion Article");
                      a1.setHeaderText(null);
                      a1.setContentText("Etes vous vraiment sur de vouloir supprimer " + currentArt.getNom() + " ?\n");
                      Optional&lt;ButtonType&gt; button = a1.showAndWait();
                      if (button.get() == ButtonType.OK) {
                          l.getArticleData().remove(currentArt);
                          ga.supprimerArticle(currentArt);
                          l.refresh();
                      }
                  } catch (Exception e) {
                      System.out.println(e.getMessage());
                  }
              }            
          });
      }
  
      @Override
      protected void updateItem(Boolean t, boolean empty) {
          super.updateItem(t, empty);
          if (!empty) {
              setGraphic(supp);
          }
      }
  
  
  }
  </code></pre>
  
  <p>and <a href="https://i.stack.imgur.com/Wv1E3.png" rel="nofollow noreferrer">this is how it appears</a> (supprimer=delete) :</p>
  
  <p>So i want my TableView to refresh automatically when I delete an article. Is there anyone who could help me?</p>
  
  <p>Thanks everyone.</p>