bonsoir ,
je suis débutant en développement java et je suis entrain de faire une application dans laquelle j'étais besoin d une JPanel ou je vais insérer une jtable ,
mais c'est pas ça le problème
mon problème c'est que a chaque fois j exécute l'application et je clique sur un bouton de mise a jour après je clique sur la table , ça pose pas de problème , il m affiche la ligne sur laquelle j'ai cliqué .Mais quand je clique sur le tableau après sur un bouton de mise a jour il m'affiche le ligne -1

sachant que moi je voulais juste obtenir la ligne sur la quelle j'ai cliqué


voici le code :



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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

class pers extends JPanel implements ActionListener , ListSelectionListener{
	 Container c;
	 JButton btn;
	 authentification au=new authentification();
	 pers pe;
	 Dimension d;
	 ResultSet rs;
	 Statement st;
	 ResultSetMetaData rm;
	 JTable table;
	 
	  String str;
	  JButton ajouter,modifier,chercher,supprimer,trier;
	  JLabel id ,nom , prenom , cin , date_naiss , tele , adresse , profession , date_paiement,date_inscription ,etat,gratuite , discipline;
	  JTextField id_t,nom_t,prenom_t,cin_t,tele_t,profession_t;
	  JComboBox date_naiss_t_j,date_naiss_t_m,date_naiss_t_a;
	  JComboBox date_inscription_t_j,date_inscription_t_m,date_inscription_t_a;
	  JComboBox date_paiement_t_j,date_paiement_t_m,date_paiement_t_a;
	  TextArea adresse_t;
	  JComboBox discipline_choix;
	  JCheckBox present , gratuit;
	  Photo photo;
	  DefaultTableModel tabM;
	public pers(){
		 
		 setLayout(null);
		 str="select * from adherent";
		 grid(str);
		 
         //les boutons
		 
		 chercher=new JButton("Chercher");
		 chercher.setBounds(10, 406, 100, 20);
		 chercher.addActionListener(this);
		add(chercher);
		ajouter=new JButton("Ajouter");
		ajouter.setBounds(120, 406, 100, 20);
		ajouter.addActionListener(this);
		add(ajouter);
		modifier=new JButton("Appliquer les modifications");
		modifier.setBounds(230, 406, 200, 20);
		modifier.addActionListener(this);
		add(modifier);
		supprimer=new JButton("Supprimer");
		supprimer.setBounds(440, 406, 100, 20);
		supprimer.addActionListener(this);
		add(supprimer);
		trier=new JButton("Trier par ");
		trier.setBounds(550, 406, 100, 20);
		trier.addActionListener(this);
		add(trier);
		
		//les Label et zone de text
		
		id=new JLabel("Numero : ");
		id.setBounds(10, 450, 100, 20);
		add(id);
		
		id_t=new JTextField(10);
		id_t.setBounds(120, 450, 200, 20);
		id_t.setEditable(false);
		add(id_t);
		
		nom=new JLabel("Nom : ");
		nom.setBounds(10, 480, 100, 20);
		add(nom);
		
		nom_t=new JTextField(10);
		nom_t.setBounds(120, 480, 200, 20);
		add(nom_t);
		
		
		prenom=new JLabel("Prenom : ");
		prenom.setBounds(10, 510, 100, 20);
		add(prenom);
		
		prenom_t=new JTextField(10);
		prenom_t.setBounds(120, 510, 200, 20);
		add(prenom_t);
		
		cin=new JLabel("CIN : ");
		cin.setBounds(10, 540, 100, 20);
		add(cin);
		
		cin_t=new JTextField(10);
		cin_t.setBounds(120, 540, 200, 20);
		add(cin_t);
		
		date_naiss=new JLabel("Date Naissance : ");
		date_naiss.setBounds(10, 570, 100, 20);
		add(date_naiss);
		
		date_naiss_t_j=new JComboBox();
		date_naiss_t_j.setBounds(120, 570, 60, 20);
		remplir_date('j',date_naiss_t_j);
		add(date_naiss_t_j);
		date_naiss_t_m=new JComboBox();
		date_naiss_t_m.setBounds(190, 570, 60, 20);
		remplir_date('m',date_naiss_t_m);
		add(date_naiss_t_m);
		date_naiss_t_a=new JComboBox();
		date_naiss_t_a.setBounds(260, 570, 60, 20);
		remplir_date('a',date_naiss_t_a);
		add(date_naiss_t_a);
		
		
		tele=new JLabel("Telephone : ");
		tele.setBounds(10, 600, 100, 20);
		add(tele);
		
		tele_t=new JTextField(10);
		tele_t.setBounds(120, 600, 200, 20);
		add(tele_t);
		
		adresse=new JLabel("Adresse : ");
		adresse.setBounds(10, 630, 100, 20);
		add(adresse);
		
		adresse_t=new TextArea();
		adresse_t.setBounds(120, 630, 200, 60);
		add(adresse_t);
		
		profession=new JLabel("Profession : ");
		profession.setBounds(350, 450, 120, 20);
		add(profession);
		
		profession_t=new JTextField(10);
		profession_t.setBounds(490, 450, 200, 20);
		add(profession_t);
		
		date_inscription=new JLabel("Date d'inscription : ");
		date_inscription.setBounds(350, 480, 120, 20);
		add(date_inscription);
		
		date_inscription_t_j=new JComboBox();
		date_inscription_t_j.setBounds(490, 480, 60, 20);
		remplir_date('j',date_inscription_t_j);
		add(date_inscription_t_j);
		date_inscription_t_m=new JComboBox();
		date_inscription_t_m.setBounds(560, 480, 60, 20);
		remplir_date('m',date_inscription_t_m);
		add(date_inscription_t_m);
		date_inscription_t_a=new JComboBox();
		date_inscription_t_a.setBounds(630, 480, 60, 20);
		remplir_date('a',date_inscription_t_a);
		add(date_inscription_t_a);
		
		date_paiement=new JLabel("Date de paiement : ");
		date_paiement.setBounds(350, 510, 120, 20);
		add(date_paiement);
		
		date_paiement_t_j=new JComboBox();
		date_paiement_t_j.setBounds(490, 510, 60, 20);
		remplir_date('j',date_paiement_t_j);
		add(date_paiement_t_j);
		date_paiement_t_m=new JComboBox();
		date_paiement_t_m.setBounds(560, 510, 60, 20);
		remplir_date('m',date_paiement_t_m);
		add(date_paiement_t_m);
		date_paiement_t_a=new JComboBox();
		date_paiement_t_a.setBounds(630, 510, 60, 20);
		remplir_date('a',date_paiement_t_a);
		add(date_paiement_t_a);

		
		discipline=new JLabel("Discipline : ");
		discipline.setBounds(350, 540, 120, 20);
		add(discipline);
		
		discipline_choix=new JComboBox();
		discipline_choix.setBounds(490, 540, 200, 20);
		add(discipline_choix);
		
		etat=new JLabel("Present(e) : ");
		etat.setBounds(350, 570, 120, 20);
		add(etat);
		
		present=new JCheckBox();
		present.setBounds(490, 570,19,19);
		present.setBackground(Color.yellow);
		add(present);
		
		gratuite=new JLabel("Gratuit : ");
		gratuite.setBounds(350, 600, 120, 20);
		add(gratuite);
		
		gratuit=new JCheckBox();
		gratuit.setBounds(490, 600,19,19);
		gratuit.setBackground(Color.yellow);
		add(gratuit);
		
		photo=new Photo("");
		photo.setBounds(720, 450, 150, 150);
		
		add(photo);
	}

 
 // la fonction pour remplir la table


	public void grid(String str){
		 try {
				
			     
				 st=au.con.createStatement();
				 rs=st.executeQuery(str);
				 rm=rs.getMetaData();
				
				int nbr=rm.getColumnCount();
				Vector colName=new Vector();
				Vector data=new Vector();
				
				for(int i=1;i<=nbr;i++){
					
					colName.addElement(rm.getColumnName(i));
				}
				while(rs.next()){
					Vector row=new Vector();
					for(int j=1;j<=nbr;j++){
						row.addElement(rs.getObject(j));
					}
					data.addElement(row);
				}
				    
				    tabM=new DefaultTableModel(data,colName);
				    table=new JTable(tabM);
				    table.getSelectionModel().addListSelectionListener(this);
				
				    JScrollPane scrollPane = new JScrollPane(table);
				    scrollPane.setSize(952, 400);
				    scrollPane.setLocation(0,0);
				    this.add(scrollPane, BorderLayout.CENTER);
				    
				rs.close();
				st.close();
				
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
	}
	
	
	
	public void actionPerformed(ActionEvent ev) {
		if(ev.getSource()==chercher){
			str="select * from adherent where id_adherent=1";
			MajTab(str);
		}
		if(ev.getSource()==modifier){
			str="select * from adherent";
			MajTab(str);
		}
		 
		 
	}
	public void valueChanged(ListSelectionEvent e) {
		if(!e.getValueIsAdjusting()){

			System.out.println(table.getSelectedRow());
			
		}

	}
	public void remplir_date(char c,JComboBox j){
		if(c=='m'){
			for(int i=1;i<=12;i++)
				j.addItem(i);
		}
		if(c=='j'){
			for(int i=1;i<=31;i++)
				j.addItem(i);
		}
		if(c=='a'){
			for(int i=1980;i<=2030;i++)
				j.addItem(i);
		}
	}

	class Photo extends JPanel{
		Image st;
		public Photo(String ph){
			super();
			try {
				st=Toolkit.getDefaultToolkit().getImage(ph);
				MediaTracker mt=new MediaTracker(this);
				mt.addImage(st,0);
				mt.waitForAll();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			g.drawImage(st, 0, 0,150,150, null);
			
		}
		
		public void setPh(String s){
		
			try {
				st=Toolkit.getDefaultToolkit().getImage(s);
				MediaTracker mt=new MediaTracker(this);
				mt.addImage(st,0);
				mt.waitForAll();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			repaint();
		}

	}

	public void naviguer(int id){
		
		try {
			String str="select * from adherent where id_adherent="+id;
			st=au.con.createStatement();
			rs=st.executeQuery(str);
			if(rs.next()){
				id_t.setText(rs.getObject(1).toString());
				nom_t.setText((String)rs.getObject(2));
				prenom_t.setText((String)rs.getObject(3));
				cin_t.setText((String)rs.getObject(4));
				Object dt[]=new Object[5];
				//dt=rs.getObject(5).toString().split("-");
				//for(int i=0;i<=2;i++)
					//System.out.println(dt[i]);
				//date_naiss_t_a.setSelectedItem(0);
				tele_t.setText((String)rs.getObject(6));
				adresse_t.setText((String)rs.getObject(7));
				profession_t.setText((String)rs.getObject(8));
				photo.setPh(rs.getObject(9).toString());
				
				//date insc
				//date paie
				rs.close();
				st.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}


//la fonction pour la mise a jour de la table 


	public void MajTab(String str){
		 try {
				
			     
				 st=au.con.createStatement();
				 rs=st.executeQuery(str);
				 rm=rs.getMetaData();
				
				int nbr=rm.getColumnCount();
				Vector colName=new Vector();
				Vector data=new Vector();
				for(int i=1;i<=nbr;i++){
					
					colName.addElement(rm.getColumnName(i));
				}
				while(rs.next()){
					Vector row=new Vector();
					for(int j=1;j<=nbr;j++){
						row.addElement(rs.getObject(j));
					}
					data.addElement(row);
				}
				    tabM.fireTableDataChanged();
				    tabM=new DefaultTableModel(data,colName);
				    
				    table.setModel(tabM);
				    
				rs.close();
				st.close();
				
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
	}

	}
merci bcp