【问题标题】:TableView autorefresh表视图自动刷新
【发布时间】: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


    【解决方案1】:

    首先,您的表格单元格实现无法正确处理单元格为空的情况。因此,如果一个不为空的单元格变为空(当您从后备列表中删除项目时正是这种情况),则该单元格不会正确更新;它继续显示单元格先前表示的项目的值。

    正确的实现是

    @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);
            }
    
        });
    

    【讨论】:

    • 首先要说的是“对不起”。我已经工作了 10 个小时,我开始越来越头晕。谢谢你的帮助,这对我很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多