Bonjour à la communauté.
J'utilise une JTable pour afficher le résultat d'une requête.
J'étais confronté à un problème d'édition des cellules; j'ai regardé dans quelques tuto(et grâce à vos suggestion) et le problème est résolu.
Maintenant je désire caché certaines colonne de ma table mais j'y arrive pas.
j'ai essayé
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
setMinWidth(0) 
setMaxWidth(0)
mais ça ne marche
Voici mon code:
Pour la création de la table j'utilise la classe FrameListeUser
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
 
public  class FrameListeUser extends javax.swing.JFrame {
    //TableModel ft;
    /** Creates new form FrameListeUser */
    public FrameListeUser() {
        initComponents();
        UIManager.put("Table.alternateRowColor", new Color(204, 204, 205));
 
        getUsers();
        JTableUser.getTableHeader().setReorderingAllowed(false);
 
 
    }
    public void getUsers() {
        Statement STMT;
         int i = 0;
         int j = 0;
        try {
            STMT = Global.MyConnection.createStatement();
            ResultSet rs;
            rs = STMT.executeQuery("select  numeroutilisateur, nom, prenom, telephone, adresse "
                    + "FROM tsutilisateurs WHERE nom LIKE '" + TextSearch.getText() +  "%'  ORDER BY nom ASC;");
 
            while (rs.next()) {
                j++; //récupère le nombre totale de ligne de la requete
            }
 
            Object vals[][] = new Object[j][5];            
            rs = STMT.executeQuery("select  numeroutilisateur, nom, prenom, telephone, adresse "
                    + "FROM tsutilisateurs WHERE nom LIKE '" + TextSearch.getText() +  "%' ORDER BY nom ASC;");           
 
            while (rs.next()) {
                vals[i][0] = rs.getString(1);
                vals[i][1] = rs.getString(2);
                vals[i][2] = rs.getString(3);
                vals[i][3] = rs.getString(4);
                vals[i][4] = rs.getString(5);
                i++;
            }
 
            String[] head = new String[]{"Numéro", "Nom", "Prénom(s)", "Téléphone", "Adresse"};
            DefaultTableModel jTM=new UneditableTableModel(vals,head);            
            JTableUser.setModel(jTM);
 
 
        } catch (Exception ex) {
            //Logger.getLogger(Consultation.class.getName()).log(Level.SEVERE, null, ex);
        }
 
    }
 
    /** 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() {
 
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JTableUser = new javax.swing.JTable();
        TextSearch = new javax.swing.JTextField();
        jLabelIcon = new javax.swing.JLabel();
        jCheckBoxCpteDesable = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        BNew = new javax.swing.JButton();
        BModif = new javax.swing.JButton();
        BDel = new javax.swing.JButton();
        BActualiser = new javax.swing.JButton();
        BFermer = new javax.swing.JButton();
        BDesactiver = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Liste des utilisateurs");
        setAlwaysOnTop(true);
        setBackground(java.awt.SystemColor.activeCaption);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
 
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Liste des utilisateurs", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.blue)); // NOI18N
 
        JTableUser.setAutoCreateRowSorter(true);
        JTableUser.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
 
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "null"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };
 
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        JTableUser.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(JTableUser);
        JTableUser.getColumnModel().getColumn(0).setCellEditor(JTableUser.getCellEditor());
 
        TextSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TextSearchKeyReleased(evt);
            }
        });
 
        jLabelIcon.setText("chercher...");
 
        jCheckBoxCpteDesable.setText("Compte désactivé");
 
        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1013, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabelIcon)
                        .addGap(22, 22, 22)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBoxCpteDesable)
                            .addComponent(TextSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 937, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jCheckBoxCpteDesable)
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TextSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelIcon))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                .addContainerGap())
        );
 
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Actions", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.blue)); // NOI18N
 
        BNew.setText("Nouveau");
        BNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BNewActionPerformed(evt);
            }
        });
 
        BModif.setText("Modifier");
        BModif.setToolTipText("Modifier la ligne selectionnée");
        BModif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BModifActionPerformed(evt);
            }
        });
 
        BDel.setText("Supprimer");
        BDel.setToolTipText("Supprimer la ligne selectionnée");
        BDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDelActionPerformed(evt);
            }
        });
 
        BActualiser.setText("Actualiser");
        BActualiser.setToolTipText("Rafraîchir la liste");
        BActualiser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BActualiserActionPerformed(evt);
            }
        });
 
        BFermer.setText("Fermer");
        BFermer.setToolTipText("Fermer la fenêtre");
        BFermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BFermerActionPerformed(evt);
            }
        });
 
        BDesactiver.setText("Désactiver");
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(259, Short.MAX_VALUE)
                .addComponent(BNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BModif)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BDesactiver)
                .addGap(26, 26, 26)
                .addComponent(BDel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BActualiser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BFermer)
                .addGap(256, 256, 256))
        );
 
        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BModif, BNew});
 
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(BDel)
                .addComponent(BActualiser)
                .addComponent(BFermer)
                .addComponent(BNew)
                .addComponent(BModif)
                .addComponent(BDesactiver))
        );
 
        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {BModif, BNew});
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
 
        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel1, jPanel2});
 
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>                        
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton BActualiser;
    private javax.swing.JButton BDel;
    private javax.swing.JButton BDesactiver;
    private javax.swing.JButton BFermer;
    private javax.swing.JButton BModif;
    private javax.swing.JButton BNew;
    private javax.swing.JTable JTableUser;
    private javax.swing.JTextField TextSearch;
    private javax.swing.JCheckBox jCheckBoxCpteDesable;
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
 
 
 
}
et pour empêcher l'édition des cellules j'utilise la classe UneditableTableModel
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
public class UneditableTableModel extends DefaultTableModel {
    public UneditableTableModel(Object[][] data, Object[] columnNames) {
        super(data, columnNames);
    }
    public boolean isCellEditable(int row, int column) {
       return false;
    }
}
Merci d'avance pour vos suggestion