Bonjour à tous,
Je suis un débutant en Java et je réalise un petit projet qui consiste à afficher des images de plantes avec JComBox à partir d'une base de données MySQL et un JLabel pour afficher un diaporama d'images.
Pour les images, je dois les importer dans un dossier local src/... et pour cela j'utilise une méthode byteArrayOutputStream qui fonctionne bien.
Lorsque je sélectionne un élément de la liste pour la première fois, les images sont bien affichées dans le JLabel et le silideshow fonctionne bien avec les boutons.
Par contre lorsque je change de sélection dans ma liste, mes informations sont bien changées dans les différents JTextField mais ce sont toujours les mêmes images de la première sélection qui sont affichées dans mon diaporama et pourtant elles sont bien changées dans le fichier source.
Voici mon code avec la classe constructeur :
Classe avec le code pour la connexion à la DB et l'enregistrement des images dans un dossier (src)
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 package com.dug.plantId; /*** Constructor images DB ***/ public class Plant { String id; byte[] image1, image2, image3, image4, image5; public Plant( byte[] image1, byte[] image2, byte[] image3, byte[] image4, byte[] image5, String id) {} public String getId() { return id; } public void setId(String id) { this.id = id; } public byte[] getImage1() { return image1; } public void setImage1(byte[] image1) { this.image1 = image1; } public byte[] getImage2() { return image2; } public void setImage2(byte[] image2) { this.image2 = image2; } public byte[] getImage3() { return image3; } public void setImage3(byte[] image3) { this.image3 = image3; } public byte[] getImage4() { return image4; } public void setImage4(byte[] image4) { this.image4 = image4; } public byte[] getImage5() { return image5; } public void setImage5(byte[] image5) { this.image5 = image5; } }
Classe avec code pour générer des cadres, des panneaux, des jlabel pour les images et des boutons pour le diaporama.
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 package com.dug.plantId; import java.io.*; import java.sql.*; import java.util.ArrayList; import java.util.List; public class DBConnection { private Connection connetion = null; private Statement stmt = null; private ResultSet rs = null; private MyPanels DBConnection; /*** Connexion à MySQL ***/ public DBConnection() { try { this.connetion = DriverManager.getConnection("jdbc:mysql://localhost/dbarbres?" + "user=Dug&password=Java427"); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } } /*** Méthode pour créer une liste importée de la BD ***/ public List<Plant> searchPlant(String nom) { List<Plant> listPlants = new ArrayList<Plant> (); try { stmt = connetion.createStatement(); rs = stmt.executeQuery("SELECT * FROM dbarbres.tblarbre where nomLatin like '%" + nom + "%' or nomCommun like '%" + nom + "%'"); byte[] image = null; while (rs.next()) { listPlants.add(new Plant(rs.getBytes("image1"), rs.getBytes("image2"), rs.getBytes("image3"), rs.getBytes("image4"), rs.getBytes("image5"), rs.getInt("id") + "")); /*** Appel des méthodes de FileOutputStream pour afficher les images de la BD. ***/ image1Generate(); image2Generate(); image3Generate(); image4Generate(); image5Generate(); MyPanels.imagesImportToList(); /*** Appelez une méthode statique pour afficher la première image ***/ MyPanels.displayImage1(); } } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { } return listPlants; } /*** Méthodes pour générer des images de bases de données MySQL dans un dossier local (src) ***/ public void image1Generate() throws SQLException, IOException { int i = rs.getInt("id"); Blob ph = rs.getBlob("image1"); if (ph == null) { return; } int length = (int) ph.length(); int bufferSize = 1024; InputStream in = ph.getBinaryStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream outputStream = new FileOutputStream("src/photos/photo1.jpg"); byte[] buffer = new byte[bufferSize]; while ((length = in .read(buffer)) != -1) { System.out.println("writing " + length + " bytes"); out.write(buffer, 0, length); } out.writeTo(outputStream); in .close(); } public void image2Generate() throws SQLException, IOException { int i = rs.getInt("id"); Blob ph = rs.getBlob("image2"); if (ph == null) { return; } int length = (int) ph.length(); int bufferSize = 1024; InputStream in = ph.getBinaryStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream outputStream = new FileOutputStream("src/photos/photo2.jpg"); byte[] buffer = new byte[bufferSize]; while ((length = in .read(buffer)) != -1) { System.out.println("writing " + length + " bytes"); out.write(buffer, 0, length); } out.writeTo(outputStream); in .close(); } public void WindowsMainPanelsAffichage() { DBConnection affichageImage = new DBConnection(); affichageImage.DBConnection = new MyPanels(); } public void image3Generate() throws SQLException, IOException { int i = rs.getInt("id"); Blob ph = rs.getBlob("image3"); if (ph == null) { return; } int length = (int) ph.length(); int bufferSize = 1024; InputStream in = ph.getBinaryStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream outputStream = new FileOutputStream("src/photos/photo3.jpg"); byte[] buffer = new byte[bufferSize]; while ((length = in .read(buffer)) != -1) { System.out.println("writing " + length + " bytes"); out.write(buffer, 0, length); } out.writeTo(outputStream); in .close(); } public void image4Generate() throws SQLException, IOException { int i = rs.getInt("id"); Blob ph = rs.getBlob("image4"); if (ph == null) { return; } int length = (int) ph.length(); int bufferSize = 1024; InputStream in = ph.getBinaryStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream outputStream = new FileOutputStream("src/photos/photo4.jpg"); byte[] buffer = new byte[bufferSize]; while ((length = in .read(buffer)) != -1) { System.out.println("writing " + length + " bytes"); out.write(buffer, 0, length); } out.writeTo(outputStream); in .close(); } public void image5Generate() throws SQLException, IOException { int i = rs.getInt("id"); Blob ph = rs.getBlob("image5"); if (ph == null) { return; } int length = (int) ph.length(); int bufferSize = 1024; InputStream in = ph.getBinaryStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream outputStream = new FileOutputStream("src/photos/photo5.jpg"); byte[] buffer = new byte[bufferSize]; while ((length = in .read(buffer)) != -1) { System.out.println("writing " + length + " bytes"); out.write(buffer, 0, length); } out.writeTo(outputStream); in .close(); } }
J'ai tout essayé mais je n'y arrive pas, il y aurait peut-être une solution avec ressource intégrée via une URL. Une URL qui peut être obtenue en utilisant Class.getResource() mais je n'ai pas assez de connaissances.
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 package com.dug.plantId; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.List; public class MyPanels extends JPanel implements ActionListener { private JComboBox jComboBoxNomCommun; private JComboBox jComboBoxNomLatin; static ImageIcon[] s; static JLabel jLabelImages; private static JButton b1; private static JButton b2; int i, l1; public MyPanels() { super(); initComposantsPanel1(); } /**** main ****/ public static void main(String args[]) { MyPanels launchWindowsProg = new MyPanels(); launchWindowsProg.generatePanels(); launchWindowsProg.buttonsSlideShow(); } /*** Initialiser les composants de panel1. ***/ private void initComposantsPanel1() { this.setLayout(new GridBagLayout()); JLabel listeAlphabetiqueNomLatin = panel1LabelCell(GridBagConstraints.WEST, 2, 1, "Liste alphabétique nom latin: "); jComboBoxNomLatin = panel1ComboBoxNomLatin(GridBagConstraints.WEST, 3, 1, ""); JLabel listeAlphabetiqueNomCommunTextField = panel1LabelCell(GridBagConstraints.WEST, 2, 2, "Liste alphabétique nom commun: "); jComboBoxNomCommun = panel1ComboBoxNomCommun(GridBagConstraints.WEST, 3, 2, ""); } /*** Constructeurs pour les différents composants ***/ public JLabel panel1LabelCell(int GridBagConstraints, int positionGridx, int positionGridy, String labelLigne) { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.insets = new Insets(0, 300, 3, 20); gridBagConstraints.anchor = GridBagConstraints; gridBagConstraints.gridx = positionGridx; gridBagConstraints.gridy = positionGridy; JLabel jLabel = new JLabel(labelLigne); this.add(jLabel, gridBagConstraints); return jLabel; } /*** Constructeur panel1 (JComboBox) ***/ public JComboBox panel1ComboBoxNomCommun(int GridBagConstraints, int positionGridx, int positionGridy, String text) { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.anchor = GridBagConstraints; gridBagConstraints.ipadx = 10; JComboBox jComboBoxNomCommun = new JComboBox(); jComboBoxNomCommun.setPreferredSize(new Dimension(225, 23)); gridBagConstraints.gridx = positionGridx; gridBagConstraints.gridy = positionGridy; /*** DB connection ***/ Connection con = null; Statement st = null; ResultSet rs = null; /*** Import liste DB vers jComboBoxNomCommun ***/ try { con = DriverManager.getConnection("jdbc:mysql://localhost/dbarbres?" + "user=Dug&password=Java427"); st = con.createStatement(); String s = "select * from tblarbre"; rs = st.executeQuery(s); while (rs.next()) { jComboBoxNomCommun.addItem(rs.getString( "nomCommun")); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "ERROR"); } finally { try { st.close(); rs.close(); con.close(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "ERROR CLOSE"); } } jComboBoxNomCommun.addActionListener(e -> { String typedText = ((JTextField) jComboBoxNomCommun.getEditor().getEditorComponent()).getText(); DBConnection dBConnection = new DBConnection(); List<Plant> findedPlant = dBConnection.searchPlant(typedText); for (Plant plant: findedPlant) { } }); this.add(jComboBoxNomCommun, gridBagConstraints); return jComboBoxNomCommun; } private JComboBox panel1ComboBoxNomLatin(int GridBagConstraints, int positionGridx, int positionGridy, String text) { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.anchor = GridBagConstraints; gridBagConstraints.ipadx = 10; JComboBox jComboBoxNomLatin = new JComboBox(); jComboBoxNomLatin.setPreferredSize(new Dimension(225, 23)); gridBagConstraints.gridx = positionGridx; gridBagConstraints.gridy = positionGridy; this.add(jComboBoxNomLatin, gridBagConstraints); Connection con = null; Statement st = null; ResultSet rs = null; /*** Import liste DB to jComboBoxNomLatin ***/ try { con = DriverManager.getConnection("jdbc:mysql://localhost/dbarbres?" + "user=Dug&password=Java427"); st = con.createStatement(); String s = "select * from tblarbre"; rs = st.executeQuery(s); while (rs.next()) { jComboBoxNomLatin.addItem(rs.getString( "nomLatin")); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "ERROR"); } finally { try { st.close(); rs.close(); con.close(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "ERROR CLOSE"); } } jComboBoxNomLatin.addActionListener(e -> { String typedText = ((JTextField) jComboBoxNomLatin.getEditor().getEditorComponent()).getText(); DBConnection dBConnection = new DBConnection(); List<Plant> findedPlant = dBConnection.searchPlant(typedText); for (Plant plant: findedPlant) { } }); return jComboBoxNomLatin; } /*** Generer les panels ***/ void generatePanels() { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); Border border = BorderFactory.createLineBorder(Color.GRAY, 1); panel1.setBounds(10, 10, 1400, 200); panel2.setBounds(720, 250, 700, 530); panel2.setBorder(border); MyPanels myPanels = new MyPanels(); panel1.add(myPanels); frame.add(panel1, BorderLayout.NORTH); frame.add(panel2, BorderLayout.EAST); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); frame.setSize(1450, 900); frame.setVisible(true); /*** Ajout des buttons pour showview images***/ panel2.add(b1); panel2.add(b2); jLabelImages = new JLabel("", JLabel.CENTER); panel2.add(jLabelImages, BorderLayout.CENTER); }); } /*** Import images dans une liste***/ public static void imagesImportToList() { s = new ImageIcon[5]; s[0] = new ImageIcon("src/photos/photo1.jpg"); s[1] = new ImageIcon("src/photos/photo2.jpg"); s[2] = new ImageIcon("src/photos/photo3.jpg"); s[3] = new ImageIcon("src/photos/photo4.jpg"); s[4] = new ImageIcon("src/photos/photo5.jpg"); } /*** Méthode d'affichage des images du diaporama ***/ public void buttonsSlideShow() { b1 = new JButton("<<"); b2 = new JButton(">>"); b1.addActionListener(e -> { if (e.getSource() == b1) { if (i == 0) { JOptionPane.showMessageDialog(null, " First image"); } else { i = i - 1; jLabelImages.setIcon(s[i]); } } if (e.getSource() == b2) { if (i == s.length - 1) { JOptionPane.showMessageDialog(null, "Last Image"); } else { i = i + 1; jLabelImages.setIcon(s[i]); } } }); b2.addActionListener(e -> { if (e.getSource() == b1) { if (i == 0) { JOptionPane.showMessageDialog(null, "First image"); } else { i = i - 1; jLabelImages.setIcon(s[i]); } } if (e.getSource() == b2) { if (i == s.length - 1) { JOptionPane.showMessageDialog(null, "Last Image"); } else { i = i + 1; jLabelImages.setIcon(s[i]); } } }); } @Override public void actionPerformed(ActionEvent e) {} /*** Afficher l'image1 après la séléction dans la liste JCombobox ***/ static void displayImage1() { String displayImage1 = "src/photos/photo1.jpg"; ImageIcon image1 = new ImageIcon(displayImage1); jLabelImages.setIcon(image1); } }
Merci d'avance pour votre éclairage.
Partager