Bonjour,

Je vous explique tout d'abord ce que j'essaye de faire...
J'ai un Panel avec 2 JComboBox et un JButton.
Le bouton sert à faire une recherche dans une base de donnée et les deux comboBox sont, pour la première, la liste des espèces animales et pour la seconde la liste des nom des animaux correspondant à la première comboBox (se trouvant dans la base de données, bien évidement).

Jusque là, pas trop de problème.

Cependant, je souhaiterai appuyer sur le bouton Recherche et rafraîchir le JPanel en gardant les selections des comboBox (via un setSelectedIndex()) mais plusieurs problèmes me barre le chemin de la réussite...

Voici le code de la classe qui devrait réaliser tout cela.

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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package userInterface;
 
import Controller.ApplicationController;
import Model.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.*;
 
public class menuRechercheAnimal extends JPanel{
 
    private ApplicationController app;
 
    private JLabel titreRechercheAnimal, labelEspece, labelNom, labelResultat;
    private JButton bRecherche;
    private JComboBox comboEspece, comboAnimal;
    private ArrayList<Espece> ListeEspece;
    private ArrayList<Animal> ListeAnimal;
    private ArrayList<Ordonnance> ListeOrdonnance;
    private Integer indexComboAnimal, indexComboEspece, rechercheNum;
    private Animal animal;
    private MainJFrame mainJFrame;
    private GridBagConstraints gbc;
 
 
 
    public menuRechercheAnimal(MainJFrame main){
 
        app = new ApplicationController();
        mainJFrame = main;
 
        titreRechercheAnimal = new JLabel("Recherche d'ordonnances par animal");
        Font font = new Font("Arial",Font.BOLD,20);
        titreRechercheAnimal.setFont(font);
        bRecherche = new JButton("Recherche");
        labelEspece = new JLabel("Espèce : ");
        labelNom = new JLabel("Nom :");
        labelResultat = new JLabel("Résultat");
        font = new Font("Arial",Font.BOLD,16);
        labelResultat.setFont(font);
 
        ListeEspece = app.getAllEspece();
        comboEspece = new JComboBox();
 
        comboEspece.addItem(" - ");
        for (int i = 0; i < ListeEspece.size(); i++){
            comboEspece.addItem(ListeEspece.get(i).getLibelle());
        }
 
        MonGestionnaireComboEspece gCE = new MonGestionnaireComboEspece();
        comboEspece.addItemListener(gCE);
 
 
        comboAnimal = new JComboBox();
        comboAnimal.setEnabled(false);
 
 
        menuRechercheAnimal.MonGestionnaireRecherche gC = new menuRechercheAnimal.MonGestionnaireRecherche();
        bRecherche.addActionListener(gC);
 
        //On regarde si on animal a été selectionné
        indexComboAnimal = comboAnimal.getSelectedIndex()-1; 
            // animal non selectionné  
            if(indexComboAnimal < 0){ 
                //On regarde si une espece a été selectionné
                indexComboEspece = comboEspece.getSelectedIndex()-1; 
                // sinon on demande toutes les ordonnances
                if(indexComboEspece < 0) {ListeOrdonnance = app.getAllOrdonnanceAnimal();} 
                // si oui on demande les ordonnances par espece
                else ListeOrdonnance = app.getAllOrdonnanceAnimal(ListeEspece.get(comboEspece.getSelectedIndex()-1).getLibelle()); 
 
            } else { // sinon on demande les ordonnances pour l'animal selectionné
                animal = ListeAnimal.get(indexComboAnimal);
                rechercheNum = animal.getNumero();
                ListeOrdonnance = app.getAllOrdonnanceAnimal(rechercheNum);
            }
 
        AllOrdonnanceAnimal modelTable = new AllOrdonnanceAnimal(ListeOrdonnance);
        JTable tableRechercheAnimal = new JTable(modelTable);  
        tableRechercheAnimal.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        JScrollPane defilant = new JScrollPane(tableRechercheAnimal);
 
        this.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
 
        gbc.gridx = gbc.gridy = 0;       
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(10, 5, 0, 0);
        this.add(titreRechercheAnimal, gbc);
 
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(20, 15, 0, 10);
        this.add(labelEspece, gbc);
 
        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(comboEspece, gbc);
 
        gbc.gridx = 2;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(labelNom, gbc);
 
        gbc.gridx = 3;
        gbc.fill = GridBagConstraints.RELATIVE;
        gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(comboAnimal, gbc);
 
        gbc.gridx = 4;
        gbc.weightx = 1.;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(bRecherche, gbc);
 
        gbc.gridy = 2;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(15, 15, 0, 10);
        this.add(labelResultat, gbc);
 
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.;
        gbc.weighty = 1.;
        gbc.insets = new Insets(10, 15, 10, 10);
        this.add(defilant, gbc);
 
    }
 
    public menuRechercheAnimal(MainJFrame main, int indexAnimal, int indexEspece){
 
        app = new ApplicationController();
        mainJFrame = main;
 
        titreRechercheAnimal = new JLabel("Recherche d'ordonnances par animal");
        Font font = new Font("Arial",Font.BOLD,20);
        titreRechercheAnimal.setFont(font);
        bRecherche = new JButton("Recherche");
        labelEspece = new JLabel("Espèce : ");
        labelNom = new JLabel("Nom :");
        labelResultat = new JLabel("Résultat");
        font = new Font("Arial",Font.BOLD,16);
        labelResultat.setFont(font);
 
        ListeEspece = app.getAllEspece();
        comboEspece = new JComboBox();
 
        comboEspece.addItem(" - ");
        for (int i = 0; i < ListeEspece.size(); i++){
            comboEspece.addItem(ListeEspece.get(i).getLibelle());
        }
 
        comboEspece.setSelectedItem(indexEspece); 
 
        MonGestionnaireComboEspece gCE = new MonGestionnaireComboEspece();
        comboEspece.addItemListener(gCE);
 
 
        comboAnimal = new JComboBox();
        comboAnimal.setEnabled(false);
 
 
        menuRechercheAnimal.MonGestionnaireRecherche gC = new menuRechercheAnimal.MonGestionnaireRecherche();
        bRecherche.addActionListener(gC);
 
        indexComboAnimal = comboAnimal.getSelectedIndex()-1; //On regarde si on animal a été selectionné
 
 
            if(indexComboAnimal < 0){ // animal non selectionné
                indexComboEspece = comboEspece.getSelectedIndex()-1; //On regarde si une espece a été selectionné
                if(indexComboEspece < 0) {ListeOrdonnance = app.getAllOrdonnanceAnimal();} // sinon on demande toutes les ordonnances
                else ListeOrdonnance = app.getAllOrdonnanceAnimal(ListeEspece.get(comboEspece.getSelectedIndex()-1).getLibelle()); // si oui on demande les ordonnances par espece
 
            } else { // sinon on demande les ordonnances pour l'animal selectionné
                animal = ListeAnimal.get(indexComboAnimal);
                rechercheNum = animal.getNumero();
                ListeOrdonnance = app.getAllOrdonnanceAnimal(rechercheNum);
            }
 
        comboAnimal.setSelectedItem(indexAnimal);    
 
        AllOrdonnanceAnimal modelTable = new AllOrdonnanceAnimal(ListeOrdonnance);
        JTable tableRechercheAnimal = new JTable(modelTable);  
        tableRechercheAnimal.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        JScrollPane defilant = new JScrollPane(tableRechercheAnimal);
 
        this.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
 
        gbc.gridx = gbc.gridy = 0;       
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(10, 5, 0, 0);
        this.add(titreRechercheAnimal, gbc);
 
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(20, 15, 0, 10);
        this.add(labelEspece, gbc);
 
        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(comboEspece, gbc);
 
        gbc.gridx = 2;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(labelNom, gbc);
 
        gbc.gridx = 3;
        gbc.fill = GridBagConstraints.RELATIVE;
        gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(comboAnimal, gbc);
 
        gbc.gridx = 4;
        gbc.weightx = 1.;
        gbc.insets = new Insets(0, 15, 0, 10);
        this.add(bRecherche, gbc);
 
        gbc.gridy = 2;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(15, 15, 0, 10);
        this.add(labelResultat, gbc);
 
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.;
        gbc.weighty = 1.;
        gbc.insets = new Insets(10, 15, 10, 10);
        this.add(defilant, gbc);
 
    }
 
    private class MonGestionnaireComboEspece implements ItemListener{
        public void itemStateChanged(ItemEvent e){
            switch(comboEspece.getSelectedIndex()){
                case 0 : if(e.getStateChange() == ItemEvent.SELECTED)
                    comboAnimal.setEnabled(false);
                    break;
                default : comboAnimal.setEnabled(true);
                          ListeAnimal = app.getAllAnimal(comboEspece.getSelectedIndex(), ListeEspece.get(comboEspece.getSelectedIndex()-1).getLibelle());
                          comboAnimal.removeAllItems();
                          comboAnimal.addItem(" - ");
                          for(int i = 0; i < ListeAnimal.size(); i++){
                          comboAnimal.addItem(ListeAnimal.get(i).getNom());
                          }
                    break;
            }
        }
    }
 
    private class MonGestionnaireRecherche implements ActionListener {
        public void actionPerformed(ActionEvent e){
 
            indexComboAnimal = comboAnimal.getSelectedIndex()-1;
            indexComboEspece = comboEspece.getSelectedIndex()-1;
 
            menuRechercheAnimal menuRA = new menuRechercheAnimal(mainJFrame, indexComboAnimal, indexComboEspece);
            mainJFrame.setPanel(menuRA);
 
        }
    }
Comme vous pouvez le voir, j'ai 2 constructeurs pour cette classe, le premier qui est appelé lors du clique sur la bar de menu, et le second qui est appelé lorsque l'on a choisit l'espèce et l'animal.

Si quelqu'un pourrait m'aiguiller, je lui en serais reconnaissant ! :-)