Problème de rafraîchissement d'une JComboBox
Bonjour/Bonsoir,
j'ai actuellement un petit souci avec une JComboBox qui ne se rafraîchit pas après ma modification des données qu'elle reçoit.
J'utilise donc SWING et MVC pour afficher mes composants (donc une frame comme vue principale et le principe des CardLayout pour afficher les panels correspondants grâce à un système de menu)
Les données proviennent d'une base de données. Jusque là pas de problème, j'arrive à ajouter des données, à en supprimer mais malheureusement les données dans la jcombobox ne se mettent jamais à jour.
Voici le code du panel avec la jcombobox en question :
Code:
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
| package Vue.produit;
import Controleur.produit.ProduitAjoutCon;
import Modele.Categorie;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class ProduitAjoutPanel extends JPanel{
private JLabel nomLabel, catLabel;
private JTextField nom;
private JComboBox cat;
private DefaultComboBoxModel modeleBox;
private JButton ajouter;
private ProduitAjoutCon con;
private ArrayList<Categorie> listeCategorie;
private GridBagConstraints gbc;
public ProduitAjoutPanel(ProduitAjoutCon con) {
this.con = con;
con.setPanel(this);
this.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
nomLabel = new JLabel("Nom du produit :");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 10, 10);
this.add(nomLabel, gbc);
nom = new JTextField(15);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.fill = GridBagConstraints.HORIZONTAL;
this.add(nom, gbc);
ajouter = new JButton("Ajouter");
gbc.gridx = 1;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.fill = GridBagConstraints.HORIZONTAL;
this.add(ajouter, gbc);
ajouter.addActionListener(con);
}
public JTextField getNom() {
return nom;
}
public void setNom(JTextField nom) {
this.nom = nom;
}
public void reset() {
this.nom.setText("");
}
public void setListe(ArrayList<Categorie> listeCategorie) {
this.listeCategorie = listeCategorie;
}
public void afficheCombo(boolean b) {
if(b) {
catLabel = new JLabel("Catégorie :");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 10, 10);
this.add(catLabel, gbc);
modeleBox = new DefaultComboBoxModel(listeCategorie.toArray());
cat = new JComboBox(modeleBox);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.fill = GridBagConstraints.HORIZONTAL;
this.add(cat, gbc);
}
}
} |
Et l'écouteur de cette classe :
Code:
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
| package Controleur.produit;
import Controleur.Controleur;
import Modele.*;
import Vue.*;
import Vue.produit.*;
import java.awt.event.ActionEvent;
import java.util.*;
public class ProduitAjoutCon extends Controleur {
private ProduitAjoutPanel pan;
private ProduitDAO dao;
private CategorieDAO catDAO;
public ProduitAjoutCon(MainFrame vue, ProduitDAO dao, CategorieDAO catDAO) {
super(vue);
this.dao = dao;
this.catDAO = catDAO;
}
public void setPanel(ProduitAjoutPanel pan) {
this.pan = pan;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "Ajout") {
try {
ArrayList<Categorie> liste = catDAO.readAll();
pan.setListe(liste);
System.out.println(liste);
if(!liste.isEmpty()) {
pan.afficheCombo(true);
} else {
pan.afficheCombo(false);
}
} catch (Exception ex) {
System.out.println(ex);
}
vue.setPanel("produitajoutpanel");
}
}
} |
Je n'arrive pas à comprendre le problème, j'ai essayé un removeAllItems() mais ça ne marche pas.
Merci de votre aide éventuelle.