Comment récuperer le choix d'un utilisateur quand il choisit un item par exemple

Signaler
Messages postés
6
Date d'inscription
dimanche 23 novembre 2008
Statut
Membre
Dernière intervention
6 janvier 2009
-
Messages postés
26
Date d'inscription
vendredi 4 mars 2005
Statut
Membre
Dernière intervention
24 janvier 2009
-
bsr j'ai un programme avec un Jtable connecté  la base de donnée et qui affiche dans le Jtable les informations de chaque etudiant 
et en bas il un Jlist et un Jtextfield et un button
je veux choisir un choix dans un Jlist par exemple age (18ans et saisir la valeur de l'age dans le JtextField et appuyer sur le button OK Jtable s'affiche avec les étudiants qui ont 18 ans comment faire svp aidez moi
j'ai fais événements mais ils sont faux voila le code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * NewJFrame123.java
 *
 * Created on 5 janv. 2009, 20:35:28
 */


package enseignant;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.awt.event.*;


import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;




public class qqqqq extends javax.swing.JFrame  implements ActionListener, ListSelectionListener {
 Statement stm=null;
 ResultSet rs = null;
 ConnBD conn=new ConnBD();
 Connection con=conn.getConnect();
 
    /** Creates new form NewJFrame123 */
    public qqqqq() {
        initComponents();
       
       
     Vector <Object> NomCol=new Vector<Object>();
  NomCol.add("matricule");
  NomCol.add("nom");
  NomCol.add("prénom");
  NomCol.add("tel");
  NomCol.add("age");
 Vector <Object> ob=new Vector<Object>();
 
 try {
    stm=con.createStatement();
       rs=stm.executeQuery("Select * from enseignant ");
  while(rs.next()){
   Vector <Object> ob1=new Vector<Object>();
   ob1.add(rs.getString("matricule"));
   ob1.add(rs.getString(2));
   ob1.add(rs.getString(3));
   ob1.add(rs.getInt(4));
   ob1.add(rs.getInt(5));
   ob.add(ob1);
  } 
 } catch (SQLException e)
 {
 System.out.println("Erreur d'affichage ");
 }
 
 
 jTable1.setModel(new javax.swing.table.DefaultTableModel(ob,NomCol));
 
 
   
    }


    /** 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() {


        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();jList1.addListSelectionListener(this);
        jButton1 = new javax.swing.JButton(); jButton1.addActionListener(this);
        jTextField1 = new javax.swing.JTextField();jTextField1.addActionListener(this);


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "matricule", "nom", "prenom", "tel", "age"
            }
        ));
        jScrollPane1.setViewportView(jTable1);


        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "age","matricule","telephone" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList1);


        jButton1.setText("OK");


        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });


        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(138, 138, 138)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 648, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(138, 138, 138))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(102, 102, 102))))
        );


        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(250, 250, 250)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(132, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );


        pack();
    }// </editor-fold>
   
   
   
            public void actionPerformed(ActionEvent e){
                
             
             if(e.getActionCommand().equals("matricule"))
             {
              String var4 =(String) jList1.getSelectedValue() ;
              System.out.println(var4 );
             
             }
              
              else if (e.getActionCommand().equals("age")){
               Integer var1=(Integer) jList1.getSelectedValue() ;
               System.out.println(var1);
              
              
              }
               
               else if  (e.getActionCommand().equals("tel")){
                Integer var2=(Integer) jList1.getSelectedValue() ;
                System.out.println(var2);
               
               
               }
               else {
                //System.out.println("selectionner un choix dans la liste");
                        
               
              }
             


              
             
           
             
             if(e.getSource()==jTextField1){
                 String M=jTextField1.getText();
             }
             
             
             if(e.getSource()==jButton1){
                  
                 
              
              
              
              
              
              
              
              
              
              
              try{
                   
                   Vector <Object> NomCol=new Vector<Object>();
                NomCol.add("matricule");
                NomCol.add("nom");
                NomCol.add("prénom");
                NomCol.add("tel");
                NomCol.add("age");
               Vector <Object> ob=new Vector<Object>();
          
                stm=con.createStatement();
               PreparedStatement pt=con.prepareStatement("select * from enseignant where ? <=age<=?");
               pt.setInt(1,18);
               pt.setInt(2,22);
               rs=stm.executeQuery("select * from enseignant");
               while(rs.next()){
                   Vector <Object> ob1=new Vector<Object>();
                   ob1.add(rs.getString("matricule"));
                   ob1.add(rs.getString(2));
                   ob1.add(rs.getString(3));
                   ob1.add(rs.getInt(4));
                   ob1.add(rs.getInt(5));
                   ob.add(ob1);
                  } 
                jTable1.setModel(new javax.swing.table.DefaultTableModel(ob,NomCol));
               
              }
               catch(SQLException ex){
               
               }


           
                  
              
             }
             
             
             
       
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
            }
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   


    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new qqqqq().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
 @Override
 public void valueChanged(ListSelectionEvent arg0) {
  // TODO Auto-generated method stub
  
 }


}

1 réponse

Messages postés
26
Date d'inscription
vendredi 4 mars 2005
Statut
Membre
Dernière intervention
24 janvier 2009

bjr tt

tu peux executer une requete : select * from etudiant where  age>18

Ensuite tu met le resultat dans matrice (par exemple tab[5][100]) en utilisant un boucle while(resultset.next){........remplir la matrice........}

enfin:

JTable jtable1=new JTable(tab);