【发布时间】:2018-07-27 14:36:16
【问题描述】:
我正在处理一个 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 在我删除一篇文章时自动刷新。有没有人可以帮助我?
谢谢大家。
【问题讨论】:
标签: java javafx tableview refresh scenebuilder