Bonsoir, je bosse sur un projet en javaFx et je suis confronté à un petit probléme. En effet, j'ai inséré un tableview avec des boutons à l'intérieur. Jusque la tout va bien. Le probléme je l'ai rencontré lorsque j'ai voulu optimiser le tout et faire en sorte que le tableau se raffraichisse automatiquement lorsque je supprime un élément.

Pour commencer voici ma classe controller :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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<Record, Boolean> implements Initializable{
 
    @FXML
    private TableView<Article> tabArt;
    @FXML
    private TableColumn<Article, Integer> refCol;
    @FXML
    private TableColumn<Article, Hyperlink> titreCol;
    @FXML
    private TableColumn<Article, String> descCol;
    @FXML
    private TableColumn<Article, String> contCol;
    @FXML
    private TableColumn<Article, String> medCol;
    @FXML
    private TableColumn supCol;
    @FXML
    private TableColumn modifCol;
    @FXML
    private TableColumn affichCol;
    @FXML
    private Button reload;
 
 
    GestionArticle ga = new GestionArticle();
    ArrayList<Article> listA = (ArrayList<Article>) ga.afficherArticle();
    public ObservableList<Article> articleData = FXCollections.observableArrayList(listA);
 
 
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
 
        tabArt.setItems(articleData);
 
        refCol.setCellValueFactory( new PropertyValueFactory<Article,Integer>("Reference"));
 
        titreCol.setCellValueFactory(
                   new PropertyValueFactory<Article,Hyperlink>("Nom"));
 
        descCol.setCellValueFactory(
                   new PropertyValueFactory<Article,String>("Description"));
 
        contCol.setCellValueFactory(
                   new PropertyValueFactory<Article,String>("Contenu"));
 
        medCol.setCellValueFactory(
                   new PropertyValueFactory<Article,String>("IdMed"));
 
 
        supCol.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<Record, Boolean>, ObservableValue<Boolean>>() {
 
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Record, Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
            }
        });
 
        supCol.setCellFactory(
                new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {
 
            @Override
            public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p) {
                return new DelButton();
            }
 
        });
 
 
        modifCol.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<Record, Boolean>, ObservableValue<Boolean>>() {
 
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Record, Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
            }
        });
 
        modifCol.setCellFactory(
                new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {
 
            @Override
            public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p) {
                return new ModifButton();
            }
 
        });
 
        affichCol.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<Record, Boolean>, ObservableValue<Boolean>>() {
 
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Record, Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
            }
        });
 
        affichCol.setCellFactory(
                new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {
 
            @Override
            public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p) {
                return new AffichButton();
            }
 
        });
 
    }
 
    public void refresh(){
        articleData.clear();
        List<Article> la=new ArrayList<>();
        la=ga.afficherArticle();
        articleData.setAll(la);
        tabArt.setItems(articleData);
    }
 
    public void setTabArt(TableView<Article> tabArt) {
        this.tabArt = tabArt;
    }
 
    public TableColumn<Article, Integer> getRefCol() {
        return refCol;
    }
 
    public void setRefCol(TableColumn<Article, Integer> refCol) {
        this.refCol = refCol;
    }
 
    public TableColumn<Article, Hyperlink> getTitreCol() {
        return titreCol;
    }
 
    public void setTitreCol(TableColumn<Article, Hyperlink> titreCol) {
        this.titreCol = titreCol;
    }
 
    public TableColumn<Article, String> getDescCol() {
        return descCol;
    }
 
    public void setDescCol(TableColumn<Article, String> descCol) {
        this.descCol = descCol;
    }
 
    public TableColumn<Article, String> getContCol() {
        return contCol;
    }
 
    public void setContCol(TableColumn<Article, String> contCol) {
        this.contCol = contCol;
    }
 
    public TableColumn<Article, String> getMedCol() {
        return medCol;
    }
 
    public void setMedCol(TableColumn<Article, String> medCol) {
        this.medCol = medCol;
    }
 
    public ObservableList<Article> getArticleData() {
        return articleData;
    }
 
 
 
}
et voici la classe du bouton supprimer :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
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<Record, Boolean> {
 
    ListeArticleFXMLController l = new ListeArticleFXMLController();
    final Button supp = new Button("Supprimer");
 
    public DelButton() {
        supp.setOnAction(new EventHandler<ActionEvent>() {
            @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<ButtonType> 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);
        }
    }
 
 
}
et voici comment ceci apparaît :

Nom : capture.PNG
Affichages : 399
Taille : 20,1 Ko

J'ai cherché la solution à mon probléme partout sur le web mais sans résultat Je viens donc vers vous en espérant trouver LA solution

Merci infiniment