Bonjour,
je dois développer une application en Java mais je ne suis pas très doué. Un de mes problèmes est de remplir un JComboBox à partir d'éléments d'un tableau créé dans le modèle.
Ma ComboBox récupère correctement les contacts que je crée dans le constructeur du modèle. Mais lorsque je veux ajouter un autre contact, à l'appui d'un bouton, il ne se passe rien. J'ai l'impression que le contact est vraiment rajouté car quand je fais afficher certains attributs pour tester cela fonctionne. En revanche, il n'y a aucune mise à jour de l'affichage.
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 /* VUE */ // Récupération des données du tableau Contact[] contactList = new Contact[model.getIndex()]; contactList = model.getListe_contact(); String[] listString = new String[model.getIndex()]; for(int i = 0; i < model.getIndex(); i++){ listString[i] = contactList[i].getNom()+" - "+contactList[i].getPrenom(); } this.labelContact = new JLabel("Contacts:"); JPanel onglet2 = new JPanel(); onglet2.add(labelContact); this.combo = new JComboBox(listString); combo.setEditable(false); combo.addActionListener(this); combo.setPreferredSize(new Dimension(200, 20)); ... //model instance de ma classe Modele this.update(model, pannel); // à l'appui du bouton contactAjouter je rajoute un contact contactAjouter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.addContact(new Contact("John","Rambo","0070070070")); } public void update (Observable observe, Object arg) { this.repaint(); this.validate(); }Est-ce que quelqu'un aurait une solution ?
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 /* MODELE */ public class Modele extends Observable implements TableModel{ private Contact[] liste_contact = new Contact[50]; private int index; public Modele(){ index = 0; setIndex(0); setChanged(); notifyObservers(); Contact contact1 = new Contact("Ali", "G", "0102030405"); Contact contact2 = new Contact("Toto", "Titi", "0908070605"); addContact(contact1); addContact(contact2); } public void addContact(Contact c) { this.liste_contact[this.index] = c; index++; this.setChanged(); this.notifyObservers(); }
Merci par avance
Partager