我正在开发JavaFx项目并遇到问题。
所以有我的Controller
课程:
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;
}
}
还有我的Button
课程:
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);
}
}
}
和this is how it appears(supprimer = delete):
所以我想在删除文章时自动刷新TableView。有没有人可以帮助我?
谢谢大家。
答案 0 :(得分:0)
首先,您的表格单元格实现无法正确处理单元格为空的情况。因此,如果非空的单元格变空(当您从后备列表中删除项目时就是这种情况),则单元格未正确更新;它继续显示单元格先前表示的项目的值。
正确的实施是
@Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(supp);
}
}
其次,您需要与实际控制器通信,或者从表的实际支持列表中删除该项。您当前正在创建一个对象,并从对象包含的列表中删除项目:
ListeArticleFXMLController l = new ListeArticleFXMLController();
// ...
l.getArticleData().remove(currentArt);
最简单的方法是将列表传递给单元格实现:
public class DelButton extends TableCell<Record, Boolean> {
final Button supp = new Button("Supprimer");
public DelButton(ObservableList<Article> articleList) {
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) {
articleList.remove(currentArt);
ga.supprimerArticle(currentArt);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
});
}
@Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(supp);
}
}
}
然后
supCol.setCellFactory(
new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {
@Override
public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p) {
return new DelButton(articleData);
}
});