Gloups je me suis trompé de chan, je le copie/colle donc ici.


Bonjour,

j'ai un petit programme, comportant 2 JFrame + 1 JApplication.

Dans une des JFrame, j'appelle l'ouverture de la 2 ème :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
btnAjoutQualification.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
        frameAjoutQualification f = new frameAjoutQualification();
        f.setVisible(true);
    }
});
il y a 2 TextField dans cette 2ème et 2 boutons, dont 1 bouton ok. Quand je clique sur le bouton ok, ma base mysql est mise à jour, jusque là pas de soucis, mais je voudrais mettre à jour automatiquement la combobox se trouvant sur ma 1ère JFrame.

Sauriez vous comment utiliser une méthode pour la mettre à jour, je n'y parvient pas car ce sont 2 classes séparées.

Voici mon code :

frameAjoutModele.class
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
 
package goeland.visual;
 
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Point;
import java.sql.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
 
public class frameAjoutModele extends JFrame {
 
	private static final long serialVersionUID = 1L;
 
	private JPanel jContentPane = null;
 
	private JLabel lblNom = null;
 
	private JLabel lblPoids = null;
 
	private JLabel lblCharge = null;
 
	private JLabel lblCapacite = null;
 
	private JLabel lblAltitude = null;
 
	private JLabel lblVitesse = null;
 
	private JLabel lblQualification = null;
 
	private JTextField tfNom = null;
 
	private JTextField tfPoids = null;
 
	private JTextField tfCharge = null;
 
	private JTextField tfCapacite = null;
 
	private JTextField tfAltitude = null;
 
	private JTextField tfVitesse = null;
 
	private JComboBox cbQualification = null;
 
	private JButton btnOk = null;
 
	private JButton btnAnnuler = null;
 
	private JLabel lblPoidsUnite = null;
 
	private JLabel lblChargeUnite = null;
 
	private JLabel lblCapaciteUnite = null;
 
	private JLabel lblAltitudeUnite = null;
 
	private JLabel lblVitesseUnite = null;
 
	private JButton btnAjoutQualification = null;
 
 
	public frameAjoutModele() {
		super();
		initialize();
	}
 
	private void initialize() {
		this.setSize(412, 348);
		this.setLocation(new Point(300, 300));
		this.setName("frmAjoutModele");
		this.setContentPane(getJContentPane());
		this.setTitle("Ajout Modèle");
	}
 
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lblVitesseUnite = new JLabel();
			lblVitesseUnite.setBounds(new Rectangle(268, 186, 38, 16));
			lblVitesseUnite.setFont(new Font("Dialog", Font.PLAIN, 12));
			lblVitesseUnite.setText("km/h");
			lblAltitudeUnite = new JLabel();
			lblAltitudeUnite.setBounds(new Rectangle(268, 154, 44, 16));
			lblAltitudeUnite.setFont(new Font("Dialog", Font.PLAIN, 12));
			lblAltitudeUnite.setText("mètres");
			lblCapaciteUnite = new JLabel();
			lblCapaciteUnite.setBounds(new Rectangle(268, 122, 45, 16));
			lblCapaciteUnite.setFont(new Font("Dialog", Font.PLAIN, 12));
			lblCapaciteUnite.setText("tonnes");
			lblChargeUnite = new JLabel();
			lblChargeUnite.setBounds(new Rectangle(268, 90, 44, 16));
			lblChargeUnite.setFont(new Font("Dialog", Font.PLAIN, 12));
			lblChargeUnite.setText("tonnes");
			lblPoidsUnite = new JLabel();
			lblPoidsUnite.setBounds(new Rectangle(268, 58, 54, 16));
			lblPoidsUnite.setFont(new Font("Dialog", Font.PLAIN, 12));
			lblPoidsUnite.setText("tonnes");
			lblQualification = new JLabel();
			lblQualification.setText("Qualification requise :");
			lblQualification.setLocation(new Point(33, 218));
			lblQualification.setSize(new Dimension(128, 16));
			lblVitesse = new JLabel();
			lblVitesse.setText("Vitesse Maximale :");
			lblVitesse.setLocation(new Point(33, 186));
			lblVitesse.setSize(new Dimension(113, 16));
			lblAltitude = new JLabel();
			lblAltitude.setText("Altitude Vol :");
			lblAltitude.setLocation(new Point(33, 154));
			lblAltitude.setSize(new Dimension(81, 16));
			lblCapacite = new JLabel();
			lblCapacite.setText("Capacité réservoir :");
			lblCapacite.setLocation(new Point(33, 122));
			lblCapacite.setSize(new Dimension(116, 16));
			lblCharge = new JLabel();
			lblCharge.setText("Charge maximale :");
			lblCharge.setLocation(new Point(33, 90));
			lblCharge.setSize(new Dimension(114, 16));
			lblPoids = new JLabel();
			lblPoids.setText("Poids :");
			lblPoids.setLocation(new Point(33, 58));
			lblPoids.setSize(new Dimension(44, 16));
			lblNom = new JLabel();
			lblNom.setText("Nom :");
			lblNom.setLocation(new Point(33, 26));
			lblNom.setSize(new Dimension(38, 16));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lblNom, null);
			jContentPane.add(lblPoids, null);
			jContentPane.add(lblCharge, null);
			jContentPane.add(lblCapacite, null);
			jContentPane.add(lblAltitude, null);
			jContentPane.add(lblVitesse, null);
			jContentPane.add(lblQualification, null);
			jContentPane.add(getTfNom(), null);
			jContentPane.add(getTfPoids(), null);
			jContentPane.add(getTfCharge(), null);
			jContentPane.add(getTfCapacite(), null);
			jContentPane.add(getTfAltitude(), null);
			jContentPane.add(getTfVitesse(), null);
			jContentPane.add(getCbQualification(), null);
			jContentPane.add(getBtnOk(), null);
			jContentPane.add(getBtnAnnuler(), null);
			jContentPane.add(lblPoidsUnite, null);
			jContentPane.add(lblChargeUnite, null);
			jContentPane.add(lblCapaciteUnite, null);
			jContentPane.add(lblAltitudeUnite, null);
			jContentPane.add(lblVitesseUnite, null);
			jContentPane.add(getBtnAjoutQualification(), null);
		}
		return jContentPane;
	}
 
	private JTextField getTfNom() {
		if (tfNom == null) {
			tfNom = new JTextField();
			tfNom.setBounds(new Rectangle(180, 24, 204, 20));
		}
		return tfNom;
	}
 
	private JTextField getTfPoids() {
		if (tfPoids == null) {
			tfPoids = new JTextField();
			tfPoids.setBounds(new Rectangle(180, 56, 75, 20));
		}
		return tfPoids;
	}
 
	private JTextField getTfCharge() {
		if (tfCharge == null) {
			tfCharge = new JTextField();
			tfCharge.setLocation(new Point(180, 88));
			tfCharge.setSize(new Dimension(75, 20));
		}
		return tfCharge;
	}
 
	private JTextField getTfCapacite() {
		if (tfCapacite == null) {
			tfCapacite = new JTextField();
			tfCapacite.setLocation(new Point(180, 120));
			tfCapacite.setSize(new Dimension(75, 20));
		}
		return tfCapacite;
	}
 
	private JTextField getTfAltitude() {
		if (tfAltitude == null) {
			tfAltitude = new JTextField();
			tfAltitude.setLocation(new Point(180, 152));
			tfAltitude.setSize(new Dimension(75, 20));
		}
		return tfAltitude;
	}
 
	private JTextField getTfVitesse() {
		if (tfVitesse == null) {
			tfVitesse = new JTextField();
			tfVitesse.setLocation(new Point(180, 184));
			tfVitesse.setSize(new Dimension(75, 20));
		}
		return tfVitesse;
	}
 
	private JComboBox getCbQualification() {
		if (cbQualification == null) {
			cbQualification = new JComboBox();
			cbQualification.setBackground(Color.white);
			cbQualification.setSize(new Dimension(177, 20));
			cbQualification.setLocation(new Point(180, 216));
			updateQualification();
		}
		return cbQualification;
	}
 
	private void updateQualification() {
		String strClassName = "com.mysql.jdbc.Driver";
		String strUrl = "jdbc:mysql://localhost/goeland";
 
		if (cbQualification == null) {
			try {
				Class.forName(strClassName);
				Connection conn = DriverManager.getConnection(strUrl,"root","iverson");
				String strSelect = "select NOM_QUALIFICATION from QUALIFICATION;";
				Statement stQualif = conn.createStatement();
				ResultSet rsQualif = stQualif.executeQuery(strSelect);
				while (rsQualif.next()) {
					cbQualification.addItem(rsQualif.getString("NOM_QUALIFICATION"));
				}
				cbQualification.setSelectedIndex(-1);
				conn.close();
			}
			catch (ClassNotFoundException e) {
				System.out.println("Problème avec le driver MySQL");
			}
			catch (SQLException e) {
				System.out.println("Problème de connexion avec la base Roger\t "+e.toString());
			}
		}
	}
 
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setText("Ok");
			btnOk.setSize(new Dimension(86, 26));
			btnOk.setLocation(new Point(192, 269));
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ajouter();
					setVisible(false);
					dispose();
				}
			});
		}
		return btnOk;
	}
 
	private JButton getBtnAnnuler() {
		if (btnAnnuler == null) {
			btnAnnuler = new JButton();
			btnAnnuler.setBounds(new Rectangle(85, 269, 86, 26));
			btnAnnuler.setText("Annuler");
			btnAnnuler.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
					dispose();
				}
			});
		}
		return btnAnnuler;
	}
 
	private void ajouter() {
		String strClassName = "com.mysql.jdbc.Driver";
		String strUrl = "jdbc:mysql://localhost/goeland";
 
		try {
			Class.forName(strClassName);
			Connection conn = DriverManager.getConnection(strUrl,"root","iverson");
			String strSelect = "select ID_QUALIFICATION from QUALIFICATION where NOM_QUALIFICATION='"
				+ (String)cbQualification.getSelectedItem() + "';";
			Statement stQualif = conn.createStatement();
			ResultSet rsQualif = stQualif.executeQuery(strSelect);
			// Insertion des données du nouveau modèle
			rsQualif.first();
			String strInsert = "insert into MODELE (NOM,POIDS,CHARGE,CAPACITE,ALTITUDE,VITESSE,ID_QUALIFICATION) values ('" +
			tfNom.getText() + "'," + tfPoids.getText() + "," + tfCharge.getText() +
			"," + tfCapacite.getText() + "," + tfAltitude.getText() +
			"," + tfVitesse.getText() + ",'" + rsQualif.getString("ID_QUALIFICATION") + "');";
			Statement stAddUser = conn.createStatement();
			stAddUser.executeUpdate(strInsert);
			conn.close();
		}
		catch (ClassNotFoundException e) {
			System.out.println("Problème avec le driver MySQL");
		}
		catch (SQLException e) {
			System.out.println("Problème de connexion avec la base Roger\t "+e.toString());
		}
	}
 
	private JButton getBtnAjoutQualification() {
		if (btnAjoutQualification == null) {
			btnAjoutQualification = new JButton();
			btnAjoutQualification.setText("+");
			btnAjoutQualification.setSize(new Dimension(20, 20));
			btnAjoutQualification.setLocation(new Point(366, 216));
			btnAjoutQualification.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					frameAjoutQualification f = new frameAjoutQualification();
					f.setVisible(true);
				}
			});
		}
		return btnAjoutQualification;
	}
}
frameAjoutQualification.class
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
 
package goeland.visual;
 
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Point;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
public class frameAjoutQualification extends JFrame {
 
	private static final long serialVersionUID = 1L;
 
	private JPanel jContentPane = null;
 
	private JLabel lblID = null;
 
	private JLabel lblNom = null;
 
	private JTextField tfID = null;
 
	private JTextField tfNom = null;
 
	private JButton btnAnnuler = null;
 
	private JButton btnOk = null;
 
	public frameAjoutQualification() {
		super();
		initialize();
	}
 
	private void initialize() {
		this.setSize(348, 215);
		this.setLocation(new Point(300, 300));
		this.setContentPane(getJContentPane());
		this.setTitle("Ajout Qualification");
	}
 
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lblNom = new JLabel();
			lblNom.setBounds(new Rectangle(30, 74, 38, 16));
			lblNom.setText("Nom :");
			lblID = new JLabel();
			lblID.setBounds(new Rectangle(30, 32, 26, 16));
			lblID.setText("ID :");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lblID, null);
			jContentPane.add(lblNom, null);
			jContentPane.add(getTfID(), null);
			jContentPane.add(getTfNom(), null);
			jContentPane.add(getBtnAnnuler(), null);
			jContentPane.add(getBtnOk(), null);
		}
		return jContentPane;
	}
 
	private JTextField getTfID() {
		if (tfID == null) {
			tfID = new JTextField();
			tfID.setBounds(new Rectangle(79, 30, 94, 20));
		}
		return tfID;
	}
 
	private JTextField getTfNom() {
		if (tfNom == null) {
			tfNom = new JTextField();
			tfNom.setBounds(new Rectangle(79, 72, 227, 20));
		}
		return tfNom;
	}
 
	private JButton getBtnAnnuler() {
		if (btnAnnuler == null) {
			btnAnnuler = new JButton();
			btnAnnuler.setText("Annuler");
			btnAnnuler.setSize(new Dimension(86, 26));
			btnAnnuler.setLocation(new Point(71, 129));
			btnAnnuler.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
					dispose();
				}
			});
		}
		return btnAnnuler;
	}
 
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setText("Ok");
			btnOk.setSize(new Dimension(86, 26));
			btnOk.setLocation(new Point(187, 129));
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ajouter();
 
					setVisible(false);
					dispose();
				}
			});
		}
		return btnOk;
	}
 
	private void ajouter() {
		String strClassName = "com.mysql.jdbc.Driver";
		String strUrl = "jdbc:mysql://localhost/goeland";
 
		try {
			Class.forName(strClassName);
			Connection conn = DriverManager.getConnection(strUrl,"root","iverson");
			String strInsert = "insert into QUALIFICATION values ('" +
				tfID.getText() + "','" + tfNom.getText() + "');";
			Statement stAddUser = conn.createStatement();
			stAddUser.executeUpdate(strInsert);
			conn.close();
		}
		catch (ClassNotFoundException e) {
			System.out.println("Problème avec le driver MySQL");
		}
		catch (SQLException e) {
			System.out.println("Problème de connexion avec la base Roger\t "+e.toString());
		}
	}
}
Merci beaucoup pour votre temps, et votre aide précieuse.