Bonjour, voilà j'ai une table toute simple SQL qui a cette structure
J'aimerais pouvoir permettre à mes utilisateurs de gérer cette table via un onglet dans l'appli Java/SWING que je leur ai mise à disposition.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3hote_id int hote_code varchar hote_libelle varchar
Pour cela , j'ai créé un Jpanel ou j'ai placé un menu déroulant Jcombobox , un bouteau Nouveau , Editer et Supprimer.
Voici mon Jpanel :
et le Model de ma Jcombox
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 package com.fft.tribune; import com.fft.tribune.TableBercy; import com.fft.tribune.tools.Functions; import com.fft.tribune.tools.SQLError; import java.awt.event.ItemEvent; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JOptionPane; import org.apache.log4j.Category; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * BercyPanel.java * * Created on 27 oct. 2009, 10:34:50 */ /** * * @author itrec */ public class BercyPanel extends javax.swing.JPanel { static final Category log = Category.getInstance(BercyPanel.class); private TablesModel tablesmodel = new TablesModel(); private String querycountDel = "SELECT count(*) as nombreInvites FROM V_TP_INVITES WHERE INV_MIDI = CODETABLE OR INV_MIDI = CODETABLE"; private String queryDel = "DELETE FROM TP_HOTE WHERE HOTE_ID = HOTEID"; private String lastIdSelected = ""; /** Creates new form BercyPanel */ public BercyPanel() { initComponents(); jComboBox1.setModel(tablesmodel); jComboBox1.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { evtComboBoxItemStateChanged(evt); } private void evtComboBoxItemStateChanged(ItemEvent evt) { lastIdSelected = tablesmodel.getSelectedId(); log.debug(tablesmodel.getSelectedLibelle()); log.debug(tablesmodel.getSelectedCode()); log.debug(tablesmodel.getSelectedId()); } }); jLabel1.setFont(new java.awt.Font("MS Sans Serif", 0, 18)); log.debug("load des tables"); loadTables(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); nouveau = new javax.swing.JButton(); jComboBox1 = new javax.swing.JComboBox(); editer = new javax.swing.JButton(); supprimer = new javax.swing.JButton(); jLabel1.setText("Gestion Des Tables Bercy"); nouveau.setText("Nouveau"); nouveau.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nouveauActionPerformed(evt); } }); nouveau.addAncestorListener(new javax.swing.event.AncestorListener() { public void ancestorMoved(javax.swing.event.AncestorEvent evt) { } public void ancestorAdded(javax.swing.event.AncestorEvent evt) { nouveauAncestorAdded(evt); } public void ancestorRemoved(javax.swing.event.AncestorEvent evt) { } }); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); editer.setText("Editer"); editer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editerActionPerformed(evt); } }); supprimer.setText("Supprimer"); supprimer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { supprimerActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(120, 120, 120) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup() .addGap(53, 53, 53) .addComponent(nouveau) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(editer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(supprimer))) .addContainerGap(26, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(24, 24, 24) .addComponent(jLabel1) .addGap(45, 45, 45) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(nouveau) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(editer) .addComponent(supprimer)) .addContainerGap(39, Short.MAX_VALUE)) ); }// </editor-fold> public void loadTables() { // Evenement ComboBox log.debug("loadTables()"); if ( tablesmodel.load() == false ) { new SQLError(); } } private void nouveauActionPerformed(java.awt.event.ActionEvent evt) { log.debug("new table"); TableBercy newTable = new TableBercy(null, true, this); // newTable.init(item.getId()); newTable.setVisible(true); } private void nouveauAncestorAdded(javax.swing.event.AncestorEvent evt) { // TODO add your handling code here: } private void editerActionPerformed(java.awt.event.ActionEvent evt) { log.debug("lastidselected " + lastIdSelected); TableBercy newTable = new TableBercy(null, true , lastIdSelected , this ); newTable.setVisible(true); } private void supprimerActionPerformed(java.awt.event.ActionEvent evt) { String temp = Functions.replace(querycountDel, "CODETABLE", this.tablesmodel.getSelectedId()); log.debug(temp); int count = 0; Connection con = ContextSingleton.getInstance().getConnection(); try{ Statement s = con.createStatement(); ResultSet r = s.executeQuery(temp); r.next(); count = r.getInt("nombreInvites") ; r.close(); }catch(Exception e){ log.debug(e.getMessage()); } //Si il n'y a pas d'invités à la table alors on peut supprimer if(count == 0){ temp = Functions.replace(queryDel, "HOTEID", this.tablesmodel.getSelectedId()); log.debug(temp); try{ Statement s = con.createStatement(); int r = s.executeUpdate(temp); } catch(Exception e){ log.debug(e.getMessage()); } JOptionPane.showMessageDialog(this, "La table "+ this.tablesmodel.getSelectedLibelle() + " a été supprimée", "Application Tribune", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(this, "Il y a des invités à la table " + this.tablesmodel.getSelectedLibelle() + ", voulez devez les réaffecter.", "Application Tribune", JOptionPane.ERROR_MESSAGE); } raffraichir(); } public void raffraichir() { tablesmodel.reload(); jComboBox1.setSelectedIndex(0); } public javax.swing.JComboBox getComboBox(){ return jComboBox1; } // Variables declaration - do not modify private javax.swing.JButton editer; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JButton nouveau; private javax.swing.JButton supprimer; // End of variables declaration }
Lorsque je clique sur Nouveau ou Editer cela lance un JDialog qui contient deux champs:
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 /* * EvtComboModel.java * * Created on 30 novembre 2008, 17:23 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package com.fft.tribune; import javax.swing.JComboBox; import javax.swing.ComboBoxModel; import javax.swing.AbstractListModel; import org.apache.log4j.Logger; import com.fft.tribune.tools.*; import java.util.Vector; /** * * @author itrec */ public class TablesModel extends AbstractListModel implements ComboBoxModel { private static final Logger log = Logger.getLogger(EvtComboModel.class); private static final String LOG_PROPERTIES_FILE = "log4J.properties"; Vector data = new Vector(); Vector items = new Vector(); Object item; int indice = 0; int closed = 0; private String query_ = "select hote_id,hote_code,hote_libelle from tp_hote WHERE HOTE_EVT_ID = '"; /** * Creates a new instance of EvtComboModel */ public TablesModel() { //load(); } public int getSize() { return data.size(); } public Object getElementAt(int val) { indice = val; Vector v = (Vector)data.elementAt(indice); return (Object)v.elementAt(1); } public Object getSelectedItem() { return (item); } public void setSelectedItem(Object obj) { item = obj; } public boolean load() { data = null; data = new Vector(); log.debug("load()"); String query = query_ + ContextSingleton.getInstance().getEventId() + "'"; if (JDBCTool.getInstance().executeQuery(query, data) == false) { return false; } indice = data.size() - 1; return true; } public boolean reload() { log.debug("reload()"); return load(); } public String getSelectedId() { Vector v = (Vector)data.elementAt(indice); return ((String)v.elementAt(0)); } public String getSelectedCode() { Vector v = (Vector)data.elementAt(indice); return ((String)v.elementAt(1)); } public String getSelectedLibelle() { Vector v = (Vector)data.elementAt(indice); return ((String)v.elementAt(2)); } public boolean isSelectedClosed() { Vector v = (Vector)data.elementAt(indice); String s = (String)v.elementAt(3); if (s.equalsIgnoreCase("N")) return false; return true; } public int getLastClosed() { int last = -1; for (int i=0; i<data.size(); i++) { Vector v = (Vector)data.elementAt(i); String s = (String)v.elementAt(3); if (s.equalsIgnoreCase("N")) last = i; } return last; } }
Et quand je clique sur Supprimer , ca supprime l'option active du menu déroulant.
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * NewJDialog.java * * Created on 26 oct. 2009, 18:52:26 */ package com.fft.tribune; import com.fft.tribune.tools.Functions; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JDialog; import javax.swing.JOptionPane; import org.apache.log4j.Category; /** * * @author itrec */ public class TableBercy extends javax.swing.JDialog { static final Category log = Category.getInstance(TableBercy.class); private String queryInsertt = "INSERT INTO TP_HOTE "+ "SELECT max(HOTE_ID)+1,'VALUECODE','VALUELIBELLE','1','EVENTID'"+ " FROM TP_HOTE"; private String queryEdit = "UPDATE TP_HOTE "+ "SET HOTE_CODE = 'VALUECODE' , HOTE_LIBELLE = 'VALUELIBELLE' "+ "WHERE HOTE_ID= HOTEID"; private String queryCheckTable = "SELECT COUNT(*) as nombre FROM TP_HOTE "+ "WHERE ((HOTE_CODE = 'CODETABLE' OR HOTE_LIBELLE = 'LIBELLETABLE') AND HOTE_EVT_ID = "+ ContextSingleton.getInstance().getEventId() + ")"; private String query_table = "select hote_id,hote_code,hote_libelle from tp_hote WHERE HOTE_ID = '"; private String libelle = ""; private String code = ""; private String mode = "add"; private String id = ""; /** A return status code - returned if Cancel button has been pressed */ public static final int RET_CANCEL = 0; /** A return status code - returned if OK button has been pressed */ public static final int RET_OK = 1; private int returnStatus = RET_CANCEL; private BercyPanel panel = null; /** Creates new form NewJDialog */ public TableBercy(java.awt.Frame parent, boolean modal, BercyPanel panel_) { super(parent, modal); panel = panel_; initComponents(); mode = "add"; } /** Creates new form NewJDialog */ public TableBercy(java.awt.Frame parent, boolean modal, String id_ , BercyPanel panel_) { super(parent, modal); log.debug("String id_"+ id_); id = id_; mode = "edit"; initComponents(); panel = panel_; String query = query_table + id + "'" ; log.debug(query); Connection con = ContextSingleton.getInstance().getConnection(); try{ Statement s = con.createStatement(); ResultSet r = s.executeQuery(query); r.next(); libelle = r.getString("HOTE_LIBELLE") ; log.debug("libellule"+libelle); code = r.getString("HOTE_CODE") ; log.debug("codation"+code); r.close(); }catch(Exception e){ log.debug(e.getMessage()); } nomtable.setText(libelle); codetable.setText(code); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { textField1 = new java.awt.TextField(); nomtable = new java.awt.TextField(); label1 = new java.awt.Label(); jButton1 = new javax.swing.JButton(); label2 = new java.awt.Label(); codetable = new java.awt.TextField(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); textField1.setText("textField1"); label1.setText("Nom De La Table"); jButton1.setLabel("OK"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okActionPerformed(evt); } }); label2.setText("Code De La Table"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(51, 51, 51) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(codetable, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(36, 36, 36) .addComponent(nomtable, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(34, 34, 34) .addComponent(jButton1) .addContainerGap(32, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(nomtable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(33, 33, 33) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(codetable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(25, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void okActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String temp = ""; this.code = codetable.getText(); this.libelle = nomtable.getText(); int nombre = 0; if(code.length() >5){ JOptionPane.showMessageDialog(this, "Le code de la table doit être constitué au maximum de 5 caractères.", "Application Tribune", JOptionPane.INFORMATION_MESSAGE); }else{ if(mode.equals("add")){ //On regarde si la table n'existe pas pour l'évenement donné temp = Functions.replace(queryCheckTable, "CODETABLE", code); temp = Functions.replace(temp, "LIBELLETABLE", libelle); log.debug(temp); Connection con = ContextSingleton.getInstance().getConnection(); try{ Statement s = con.createStatement(); ResultSet r = s.executeQuery(temp); r.next(); nombre = r.getInt("nombre") ; r.close(); }catch(Exception e){ log.debug(e.getMessage()); } if(nombre==0){ temp = Functions.replace(queryInsertt, "VALUECODE", this.code); temp = Functions.replace(temp, "VALUELIBELLE", this.libelle); temp = Functions.replace(temp, "EVENTID", ContextSingleton.getInstance().getEventId()); } }else{ //mode edit temp = Functions.replace(queryEdit, "VALUECODE", this.code); temp = Functions.replace(temp, "VALUELIBELLE", this.libelle); temp = Functions.replace(temp, "HOTEID", this.id); } log.debug(temp); if(nombre==0){ Connection c = ContextSingleton.getInstance().getConnection(); try { Statement stmt = c.createStatement(); int resultat = stmt.executeUpdate(temp); } catch(Exception e){ System.out.println("exception due a la requete" + e.getMessage()); } doClose(RET_OK); }else{ //Il existe déjà une table portant le meme nom ou meme code JOptionPane.showMessageDialog(this, "La table "+ libelle + " ou le code table " + code + " existe déjà", "Application Tribune", JOptionPane.INFORMATION_MESSAGE); } } this.panel.raffraichir(); } private void doClose(int retStatus) { returnStatus = retStatus; setVisible(false); dispose(); } /** * @param args the command line arguments */ // Variables declaration - do not modify private java.awt.TextField codetable; private javax.swing.JButton jButton1; private java.awt.Label label1; private java.awt.Label label2; private java.awt.TextField nomtable; private java.awt.TextField textField1; // End of variables declaration }
Actuellement , j'ai un problème lorsque j'ajoute ou édite ou supprimer un codee , ca concerne le refresh de mon menu deroulant.
J'appelle pourtant la fonction :
tablesmodel.reload();
Mais je suis obligé de cliquer sur le menu déroulant pour que ca se fasse , pourquoi?
Partager