String valeur jcombobox.getSelectedItem().toString();
String[] entetes = new String[]{"colonne 1", "colonne 2"}; Object[][] data = new Object[0][entetes.length]; JTable table = new JTable(new DefaultTableModel(data, entetes));
((DefaultTableModel)(table.getModel())).addRow(new String[]{"s1", "s2"});
String valeur = monCombo.getSelectedItem().toString();
/* La classe qui définit la fenetre principale */ public class Fenetre { public Fenetre(){ // Quelque part tu créés ta table String[] entetes = new String[]{"id", "nom","prenom","matricule"}; Object[][] personne = new Object[0][entetes.length]; JTable table_entree = new JTable(new DefaultTableModel(personne, entetes)); private void monComboActionPerformed(java.awt.event.ActionEvent evt) { String valeur =(String)monCombo.getSelectedItem(); String sql="select * from personne where matricule=?"; try{ pst=conn.prepareStatement(sql); pst.setString(1,valeur); rs=pst.executeQuery(); if(rs.next()){ ((DefaultTableModel)(table_entree.getModel())).addRow(new String[]{"id", "nom","prenom","matricule"}); } } catch(Exception e){ e.getMessage(); } } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprivate void monComboActionPerformed(java.awt.event.ActionEvent evt) { String valeur =(String)monCombo.getSelectedItem(); String sql="select * from personne where matricule=?"; try{ pst=conn.prepareStatement(sql); pst.setString(1,valeur); rs=pst.executeQuery(); if(rs.next()){ String[] entetes = new String[]{"id", "nom","prenom","matricule"}; Object[][] personne = new Object[0][entetes.length]; JTable table_entree = new JTable(new DefaultTableModel(personne, entetes)); ((DefaultTableModel)(table_entree.getModel())).addRow(new String[]{"id", "nom","prenom","matricule"}); } } catch(Exception e){ e.getMessage(); } }
class ItemAction implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("ActionListener : action sur " + monCombo.getSelectedItem()); String valeur = monCombo.getSelectedItem().toString(); String sql = "select * from personne where matricule=?"; //Object[][] personne = new Object[0][columnNames.length]; try { pst = conn.prepareStatement(sql); pst.setString(1, valeur); rs = pst.executeQuery(); /*if (rs.next()) { //((DefaultTableModel) (table_entree.getModel())).addRow(new String[]{""+rs.getInt("id"), rs.getString("nom"), rs.getString("prenom"), rs.getString("matricule")}); personne[0][0] = rs.getInt("id"); personne[0][1] = rs.getString("nom"); personne[0][2] = rs.getString("prenom"); personne[0][3] = rs.getString("matricule"); } table_entree = new JTable(personne, columnNames);*/ table_entree.setModel(DbUtils.resultSetToTableModel(rs)); } catch (Exception ex) { ex.getMessage(); } } } ------------------------------------------------------------------- combo=> private void populateCombobox() { try { String sql = "SELECT * FROM personne"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()) { String matricule = rs.getString("matricule"); monCombo.addItem(matricule); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } finally { try { rs.close(); // pst.close(); } catch (Exception e) { } } }