Bonjour à tous et à toutes.
je viens vers vous car je ne comprends pas j'ai un problème de placement d'une JTable que je souhaitais placer dans un ScrollPane contenu dans un panel.

en fait, d'apres ce que j'ai lu sur le forum et la doc java c'est que si on ne précise pas, le JPanel a par defaut l'option de posistionnement BorderLayout.

D'un autre coté si on place un element comme BorderLayout.CENTER alors si il n'y a rien à EAST et WEST alors l'élement prend la place qui lui ai possible de prendre.

Mon probleme c'est que dans mon cas je me retrouve avec une table qui est compressé entre EAST et WEST et je ne vois pas pourquoi ??

Donc je me suis dis il faut que je précise pour chacun des éléments leurs positionnements.

En fait ca me paraissait simple car j'ai une image type bandeau de la largeur de mon appli et une zone qui se devait etre dynamique.

Alors j'ai mis BorderLayout.NORTH pour mon image et BoerderLayout.CENTER pour mon JScrollPane. Si je fais ca marche mais des que je souhaite charger une nouvelle pas (par exemple un formulaire de recherche), il me charge mon image au nord mais me charge sur toute place qui reste uniquement mon bouton.

je ne comprend pas ce qu'il se passe

ptete qu'un ti bout de code pourrait vous eclaircir :

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
 
 
 
 
 
public class FenetreApp extends JFrame {
 
 
 
	// Panneau principal
	JPanel PanneauPpal = null;
 
	// scrollpane dans center de panneauPpal pour affichage des oeuvres
	JScrollPane scrollPane = null;
 
 
 
	//-----------------------------Menu
	JMenuBar barreDeMenu = null;
	JMenu menuRecherche = null;
 
 
	// --------- items des menus
	JMenuItem afficherToutes = null;
    JMenuItem rechercheRapide = null;
    JMenuItem quitterApplication = null;
 
    //------------------------------------Formulaire RECHERCHE
    // recherche
 
    JTextField f_nomOeuvre;
    JLabel labelNomOeuvre;
 
    JButton btnRecherche;
 
 
	// image à placer dans le panneauppal sud
	JLabel imagefond = null;
 
 
 
 
 
	//------------------------
	Controleur musee;
 
	//----------------------------------------------------Constructeur 
	public FenetreApp(Controleur c){
 
		super();
		musee=c;
		musee.generationOeuvres();
 
		setLayout(new BorderLayout());
 
		this.setJMenuBar(getBarreDeMenu());
		this.setContentPane(build());
 
	}
 
 
 
 
	private JPanel build(){
 
		if (PanneauPpal==null){
		//-----------------------------------------------construction de la fenetre
		setTitle("Application Gestion des Oeuvres"); 
		setSize(900,600); 
		setLocationRelativeTo(null);  
		setResizable(false); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
		PanneauPpal = new JPanel();
	    imagefond = new JLabel( new ImageIcon("img/fond.jpg"));
	    PanneauPpal.add(imagefond);
 
	   }
	    return PanneauPpal;
	}
 
 
 
 
 
 
 
		//------------------------------------------------------------------------------------------
		//----------------------------------------------- M E N U    R E C H E R C H E
		JMenu getJMenu1() {
			if (menuRecherche == null){
				menuRecherche = new JMenu();
				menuRecherche.setText("Recherche");
				menuRecherche.add(getRechercheRapide());
				menuRecherche.add(getRechercheThema());
				menuRecherche.add(getRechercheAvancee());
				menuRecherche.add(getAfficherToutes());
			}
			return menuRecherche;
		}
 
		//----------------------------------------------- RECH  RAPIDE
		JMenuItem getRechercheRapide() {
			if (rechercheRapide==null){
				rechercheRapide = new JMenuItem();
				rechercheRapide.setText("Recherche rapide");
				rechercheRapide.addActionListener(new java.awt.event.ActionListener(){
					public void actionPerformed(java.awt.event.ActionEvent e){
						System.out.println("vous avez cliqué sur recherche rapide");
 
						//----------- Supprime contenu de JPanel et recharge
						System.out.println("on vide le panneau ppal");
						PanneauPpal.removeAll();
						System.out.println("on le remplit");
 
						imagefond = new JLabel( new ImageIcon("img/fond.jpg"));
					    PanneauPpal.add(imagefond);
 
 
 
						labelNomOeuvre = new JLabel("Nom de l'oeuvre : ");
						f_nomOeuvre = new JTextField("", 50);
						btnRecherche = new JButton("Rechercher");
 
 
						PanneauPpal.add(labelNomOeuvre);
						PanneauPpal.add(f_nomOeuvre);
						PanneauPpal.add(btnRecherche);
						setContentPane(PanneauPpal);
 
 
					    btnRecherche.addActionListener(new java.awt.event.ActionListener(){
							public void actionPerformed(java.awt.event.ActionEvent e){
								System.out.println("vous avez cliqué sur le bouton");
								System.out.println("------------------------------");
 
								String nomOeuvre = f_nomOeuvre.getText();
								//System.out.println(nomOeuvre.getText());
								scrollPane = retourneTableRechercheR(nomOeuvre);
 
								PanneauPpal.removeAll();
 
								imagefond = new JLabel( new ImageIcon("img/fond.jpg"));
							    PanneauPpal.add(imagefond);
 
								if (scrollPane!=null)
								{	
									PanneauPpal.add(new JLabel("Recherche de "+nomOeuvre+" a donné "));	
									PanneauPpal.add(scrollPane, BorderLayout.CENTER);
									setContentPane(PanneauPpal);
								}
								else
								{	
									PanneauPpal.add(new JLabel("Recherche de "+nomOeuvre+" infructueuse "));	
									setContentPane(PanneauPpal);
								}
							}
					   });
 
 
					}
				});
			}
			return rechercheRapide;
		}
 
 
 
		public JScrollPane retourneTableRechercheR(String nomOeuvre){
 
				JScrollPane scrollPaneTemp=null; 
 
				String[] columnNames = {"Nom Oeuvre", 
		             "Type Oeuvre",
		             "Thème Oeuvre",
		             "Nom Artiste",
		             "Date de Création", "Description", "Image"};
			    System.out.println("on cherche "+nomOeuvre);
 
				Object[][] data = musee.rechercheR(nomOeuvre);
 
				if (data!=null) {
 
					//System.out.println("on a trouve ");
 
					JTable table = new JTable(data, columnNames);
					table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					table.setSelectionBackground(Color.orange); 
					table.setSelectionForeground(Color.black);
					table.setShowVerticalLines(false);
					table.setShowHorizontalLines(true);
					table.setGridColor(Color.lightGray);
					table.setRowHeight(30);
 
 
 
			    // parametre de la table 
			    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
			    TableColumn col0 = table.getColumnModel().getColumn(0);
			    int width0 = 175;
			    col0.setPreferredWidth(width0);
 
			    TableColumn col1 = table.getColumnModel().getColumn(1);
			    int width1 = 80;
			    col1.setPreferredWidth(width1);
 
			    TableColumn col2 = table.getColumnModel().getColumn(2);
			    int width2 = 90;
			    col2.setPreferredWidth(width2);
 
			    TableColumn col3 = table.getColumnModel().getColumn(3);
			    int width3 = 180;
			    col3.setPreferredWidth(width3);
 
			    TableColumn col4 = table.getColumnModel().getColumn(4);
			    int width4 = 100;
			    col4.setPreferredWidth(width4);
 
			    TableColumn col5 = table.getColumnModel().getColumn(5);
			    int width5 = 255;
			    col5.setPreferredWidth(width5);
 
			    TableColumn col6 = table.getColumnModel().getColumn(6);
			    int width6 = 30;
			    col6.setPreferredWidth(width6);
 
 
			    // -----------------------------------------------ajout de la table au conteneur JScrollPane
			    scrollPaneTemp = new JScrollPane(table);
 
			    }
				return scrollPaneTemp;
 
 
		}
 
 
 
 
 
		//----------------------------------------------- AFFICHER  TOUTES
		JMenuItem getAfficherToutes() {
			if (afficherToutes==null){
				afficherToutes = new JMenuItem();
				afficherToutes.setText("Afficher toutes les oeuvres");
				afficherToutes.addActionListener(new java.awt.event.ActionListener(){
					public void actionPerformed(java.awt.event.ActionEvent e){
							//System.out.println("on essaie de modifier le panneautable");
						    //panneauTable.add(scrollPane);
 
 
						//----------- Supprime contenu de JPanel et recharge
						PanneauPpal.removeAll();
						imagefond = new JLabel( new ImageIcon("img/fond.jpg"));
					    PanneauPpal.add(imagefond);
 
 
 
					    PanneauPpal.add(getAfficherTable());
 
							rafraichir();
					}
				});
			}
			return afficherToutes;
		}
 
		// ---------------------------------------- Mettre a zero
		// ---------------------------------------- RAFRAICHIR
		private void rafraichir(){
		this.setContentPane(PanneauPpal);
		}
 
 
		//----------------------------------------------- Afficher Oeuvres
		public JScrollPane getAfficherTable(){
 
			String[] columnNames = {"Nom Oeuvre",
		             "Type Oeuvre",
		             "Thème Oeuvre",
		             "Nom Artiste",
		             "Date de Création", "Description", "Image"};
 
			    Object[][] data = musee.AfficheTableau();
 
			    JTable table = new JTable(data, columnNames);
			    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			    table.setSelectionBackground(Color.orange); 
			    table.setSelectionForeground(Color.black);
			    table.setShowVerticalLines(false);
			    table.setShowHorizontalLines(true);
			    table.setGridColor(Color.lightGray);
			    table.setRowHeight(30);
 
 
 
			    // parametre de la table 
			    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
			    TableColumn col0 = table.getColumnModel().getColumn(0);
			    int width0 = 175;
			    col0.setPreferredWidth(width0);
 
			    TableColumn col1 = table.getColumnModel().getColumn(1);
			    int width1 = 80;
			    col1.setPreferredWidth(width1);
 
			    TableColumn col2 = table.getColumnModel().getColumn(2);
			    int width2 = 90;
			    col2.setPreferredWidth(width2);
 
			    TableColumn col3 = table.getColumnModel().getColumn(3);
			    int width3 = 180;
			    col3.setPreferredWidth(width3);
 
			    TableColumn col4 = table.getColumnModel().getColumn(4);
			    int width4 = 100;
			    col4.setPreferredWidth(width4);
 
			    TableColumn col5 = table.getColumnModel().getColumn(5);
			    int width5 = 255;
			    col5.setPreferredWidth(width5);
 
			    TableColumn col6 = table.getColumnModel().getColumn(6);
			    int width6 = 30;
			    col5.setPreferredWidth(width6);
 
 
			    // -----------------------------------------------ajout de la table au conteneur JScrollPane
			    scrollPane = new JScrollPane(table);
			return scrollPane;
		}
 
}
Si qqn pouvait me venir en aide... Merci bcp d'avance.

Aurore