IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Problème jComboBox.removeAllItem() avec actionListener() génère erreur de type Null pointeurException


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    etudient_master
    Inscrit en
    Novembre 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : etudient_master

    Informations forums :
    Inscription : Novembre 2011
    Messages : 22
    Par défaut Problème jComboBox.removeAllItem() avec actionListener() génère erreur de type Null pointeurException
    Bonjour ,
    le but de mon programme est de créer deux combobox relier connecté à bdd ,quand je clique sur le client1 de combobox1 affiche une liste relier au client1, aussi pour le client2 une liste relier au client 2 ainsi de suite mais j'ai recontré un problème affichage doublé au niveau de client2 affiche les coordonné de client 1 et client 2 j'ai pensé à removeAllItem() mais j'ai rencontré un problème de nullpointer sur comboBox2? comment résoudre ce problème ?

    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
     
     
    package admin;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.DefaultComboBoxModel;
     
     
    public class Interface_admin extends javax.swing.JFrame {
     
     
        ResultSet rs,rs1,rs2,rs3,rs4,rs5;
         Connection con=null;
     
         String client;
         String liste ;
     
        public Interface_admin() {
           initComponents();
     
            try{
     
            Class.forName("org.sqlite.JDBC");
            con=DriverManager.getConnection("jdbc:sqlite:"+"C:\\work\\administration.db");
            System.out.println("bdd est connecter ");
     
            Statement stm=con.createStatement();
            String sql1 ="select * from client ";
     
             rs=stm.executeQuery(sql1);
     
     
            try {
     
                while(rs.next()){
                     int id=rs.getInt(1);
                     String link=rs.getString(2);
     
                    jComboBox1.addItem(link); 
     
                    }
     
     
               jComboBox1.addActionListener(new actionListener()); 
               client=jComboBox1.getSelectedItem().toString();    
     
               jButton1.addActionListener(new actionl());
            } catch (SQLException ex) {
                System.out.println("impossible de charger les items de BDD "+ex);
        } 
     
     
     
     
          }
     
            catch(Exception ex){
     
            System.out.println(ex);
     
            }  
        }  
     
     
     
     
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
     
     
     
     
     
        }                                        
     
    public class actionListener2 implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent ae) {
     
                liste=jComboBox2.getSelectedItem().toString();
                        System.out.println(liste); 
     
     
            }
     
     
     
     
     
     
     
    }
     
     
     
     
     
     
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
     
     
     
     
     
        }                                          
     
     
     
        public class actionListener implements ActionListener{
     
    public void actionPerformed(ActionEvent ae){
     
    client=jComboBox1.getSelectedItem().toString();
     
                 System.out.println(client);
     
     
        switch (client) {
            case "client1":
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=1";
                    rs1=stm.executeQuery(sql2);
     
                    while(rs1.next()){
     
     
                        jComboBox2.addItem( rs1.getString(1));
     
     
                    }
     
                }
                catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
              jComboBox2.addActionListener(new actionListener2());
                break;
            case "client2":
              jComboBox2.removeAllItems();  
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=2";
                    rs2=stm.executeQuery(sql2);
     
                    while(rs2.next()){
     
     
                        jComboBox2.addItem( rs2.getString(1));
     
                    }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
               jComboBox2.addActionListener(new actionListener2());
                break;
            case "client3":
               jComboBox2.removeAllItems();     
                try {
                            Statement stm=con.createStatement();
                            String sql2="select nom from client where IDb=3";
                            rs3=stm.executeQuery(sql2);
     
                            while(rs3.next()){
     
     
                               jComboBox2.addItem( rs3.getString(1));
     
                            }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
             jComboBox2.addActionListener(new actionListener2());
                break;
            case "client4":
      jComboBox2.removeAllItems();  
                try{
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=4";
                    rs4=stm.executeQuery(sql2);
     
                    while(rs4.next()){
     
     
                      jComboBox2.addItem( rs4.getString(1));
     
                    }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
     
                jComboBox2.addActionListener(new actionListener2());
     
                break;}
                finally{
        try {
            rs4.close();
     
        } catch (SQLException ex) {
            Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
        }
                        }  
        }
     
     
     
     
     
     
     
     
     
     
    }}
        public static void main(String args[]) {
     
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
     
     
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Interface_admin().setVisible(true);
                }
            });
        }
     
     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField3;
     
    }

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Billets dans le blog
    2
    Par défaut
    Salut,

    Il où le removeAllItem() dans ton code ?

    Si tu appelles cette méthode, ça vide le modèle de la combo, et ça provoque éventuellement un changement de sélection, puisqu'il n'y a plus rien à sélectionner. Du coup les ActionListener qui sont affectés au combo, qui sont du genre jComboBox2.getSelectedItem().toString(); peuvent effectivement provoquer des NullPointerException, puisqu'il n'y a plus rien à sélectionner, donc getSelectedItem() retourne null. Il te suffit de tester ce cas pour ajuster la réaction (et par ailleurs rendre la sélection correcte, la variable liste dans le cas de jComboBox2).
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

  3. #3
    Membre averti
    Femme Profil pro
    etudient_master
    Inscrit en
    Novembre 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : etudient_master

    Informations forums :
    Inscription : Novembre 2011
    Messages : 22
    Par défaut Réponse pour l'utilisation de la méthode removeAllItems()
    Bonjour ,

    voici mon code avec la méthode removeAllItems() j'ai besoin de vider combobox2 lorsque je sélectionne le deuxième choix ,j'ai essayé de les placés après actionlistener() de jcombobox2 mais sa marche pas j'ai l'impression que le code exécute le "remove" avants le "listener" pour donner l'erreur nullpointerexception.

    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
     
     
    package admin;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.DefaultComboBoxModel;
     
     
    public class Interface_admin extends javax.swing.JFrame {
     
     
        ResultSet rs,rs1,rs2,rs3,rs4,rs5;
         Connection con=null;
     
         String client;
         String liste ;
     
        public Interface_admin() {
           initComponents();
     
            try{
     
            Class.forName("org.sqlite.JDBC");
            con=DriverManager.getConnection("jdbc:sqlite:"+"C:\\work\\administration.db");
            System.out.println("bdd est connecter ");
     
            Statement stm=con.createStatement();
            String sql1 ="select * from client ";
     
             rs=stm.executeQuery(sql1);
     
     
            try {
     
                while(rs.next()){
                     int id=rs.getInt(1);
                     String link=rs.getString(2);
     
                    jComboBox1.addItem(link); 
     
                    }
     
     
               jComboBox1.addActionListener(new actionListener()); 
               client=jComboBox1.getSelectedItem().toString();    
     
               jButton1.addActionListener(new actionl());
            } catch (SQLException ex) {
                System.out.println("impossible de charger les items de BDD "+ex);
        } 
     
     
          }
     
            catch(Exception ex){
     
            System.out.println(ex);
     
            }  
        }  
     
     
     
     
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
     
     
     
     
     
        }                                        
     
    public class actionListener2 implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent ae) {
     
                liste=jComboBox2.getSelectedItem().toString();
                        System.out.println(liste); 
     
     
            }
     
    }
     
     
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
     
     
     
     
     
        }                                          
     
     
     
        public class actionListener implements ActionListener{
     
    public void actionPerformed(ActionEvent ae){
     
    client=jComboBox1.getSelectedItem().toString();
     
                 System.out.println(client);
     
     
        switch (client) {
            case "client1":
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=1";
                    rs1=stm.executeQuery(sql2);
     
                    while(rs1.next()){
     
     
                        jComboBox2.addItem( rs1.getString(1));
     
     
                    }
     
                }
                catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
              jComboBox2.addActionListener(new actionListener2());
              jComboBox2.removeAllItems();
                break;
            case "client2":
     
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=2";
                    rs2=stm.executeQuery(sql2);
     
                    while(rs2.next()){
     
     
                        jComboBox2.addItem( rs2.getString(1));
     
                    }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
               jComboBox2.addActionListener(new actionListener2());
               jComboBox2.removeAllItems();
                break;
            case "client3":
     
                try {
                            Statement stm=con.createStatement();
                            String sql2="select nom from client where IDb=3";
                            rs3=stm.executeQuery(sql2);
     
                            while(rs3.next()){
     
     
                               jComboBox2.addItem( rs3.getString(1));
     
                            }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
             jComboBox2.addActionListener(new actionListener2());
             jComboBox2.removeAllItems();
                break;
            case "client4":
                try{
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=4";
                    rs4=stm.executeQuery(sql2);
     
                    while(rs4.next()){
     
     
                      jComboBox2.addItem( rs4.getString(1));
     
                    }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
     
                jComboBox2.addActionListener(new actionListener2());
                jComboBox2.removeAllItems();
     
                break;}
                finally{
        try {
            rs4.close();
     
        } catch (SQLException ex) {
            Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
        }
                        }  
        }
     
     
     
     
     
     
     
     
     
     
    }}
        public static void main(String args[]) {
     
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
     
     
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Interface_admin().setVisible(true);
                }
            });
        }
     
     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField3;
     
    }

  4. #4
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Billets dans le blog
    2
    Par défaut
    C'est absurde de faire le removeAllItems après avoir remplie la combo ! Autant ne pas la remplir, ni avant, ni après !

    Pour remplacer la liste de choix dans la combo, la vider d'abord pour supprimer les anciennes valeurs puis la remplir avec les nouvelles.

    Ensuite, ce process n'a rien à voir avec l'enregistrement d'événements d'action sur les combo : l'enregistrement doit être fait à la création du composant combo, sinon ça enregistre un nouvel ActionListener sur le même composant (jComboBox2), à chaque nouvelle action sur jComboBox1.

    Je t'ai dit que tu devais prendre en compte que la sélection serait null lorsque tu vidais la combo (vidage = nouvelle sélection = événement), donc qu'il fallait prendre en compte ce cas.
    Il suffit de faire un truc comme ça (surtout que c'est là qu'il y a la NullPointerException) :

    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
     
    public class actionListener2 implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent ae) {
     
                Object selectedItem = jComboBox2.getSelectedItem();
                if ( selectedItem==null ) {
                       liste = null;
                }
                else {
                       liste = selectedItem.toString();
                }
                System.out.println(liste); 
            }
     
    }
    ;
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

  5. #5
    Membre averti
    Femme Profil pro
    etudient_master
    Inscrit en
    Novembre 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : etudient_master

    Informations forums :
    Inscription : Novembre 2011
    Messages : 22
    Par défaut nullpointerexception
    Merci pour votre aide mais toujours le problème avec" nullpointerexception ".


    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
     
    package admin;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.DefaultComboBoxModel;
     
     
    public class Interface_admin extends javax.swing.JFrame {
     
     
        ResultSet rs,rs1,rs2,rs3,rs4,rs5;
         Connection con=null;
     
         String client;
         String liste ;
     
        public Interface_admin() {
           initComponents();
     
            try{
     
            Class.forName("org.sqlite.JDBC");
            con=DriverManager.getConnection("jdbc:sqlite:"+"C:\\work\\administration.db");
            System.out.println("bdd est connecter ");
     
            Statement stm=con.createStatement();
            String sql1 ="select * from client ";
     
             rs=stm.executeQuery(sql1);
     
     
            try {
     
                while(rs.next()){
                     int id=rs.getInt(1);
                     String link=rs.getString(2);
     
                    jComboBox1.addItem(link); 
     
                    }
     
     
               jComboBox1.addActionListener(new actionListener()); 
               client=jComboBox1.getSelectedItem().toString();    
     
               jButton1.addActionListener(new actionl());
            } catch (SQLException ex) {
                System.out.println("impossible de charger les items de BDD "+ex);
        } 
     
     
          }
     
            catch(Exception ex){
     
            System.out.println(ex);
     
            }  
        }  
     
     
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
        }                                        
     
    public class actionListener2 implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent ae) {
     
                Object selectedItem = jComboBox2.getSelectedItem();
                if ( selectedItem==null ) {
                       liste = null;
                }
                else {
                       liste = selectedItem.toString();
                }
     
                        System.out.println(liste); 
     
     
            }
     
    }
     
     
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
     
        }                                          
     
     
        public class actionListener implements ActionListener{
     
    public void actionPerformed(ActionEvent ae){
     
    client=jComboBox1.getSelectedItem().toString();
     
                 System.out.println(client);
     
     
        switch (client) {
            case "client1":
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=1";
                    jComboBox2.removeAllItems();
     
                    rs1=stm.executeQuery(sql2);
     
                    while(rs1.next()){
     
     
                        jComboBox2.addItem( rs1.getString(1));
     
     
                    }
     
                }
                catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
              jComboBox2.addActionListener(new actionListener2());
     
                break;
            case "client2":
     
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=2";
                      jComboBox2.removeAllItems();
     
                    rs2=stm.executeQuery(sql2);
     
                    while(rs2.next()){
     
     
                        jComboBox2.addItem( rs2.getString(1));
     
                    }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
               jComboBox2.addActionListener(new actionListener2());
                           break;
            case "client3":
     
                try {
                            Statement stm=con.createStatement();
                            String sql2="select nom from client where IDb=3";
                            jComboBox2.removeAllItems();
     
                            rs3=stm.executeQuery(sql2);
     
                            while(rs3.next()){
     
     
                               jComboBox2.addItem( rs3.getString(1));
     
                            }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
             jComboBox2.addActionListener(new actionListener2());
     
                break;
            case "client4":
                try{
                try {
                    Statement stm=con.createStatement();
                    String sql2="select nom from client where IDb=4";
                    jComboBox2.removeAllItems();
     
                    rs4=stm.executeQuery(sql2);
     
                    while(rs4.next()){
     
     
                      jComboBox2.addItem( rs4.getString(1));
     
                    }
     
                } catch (SQLException ex) {
                    Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
                }
     
                jComboBox2.addActionListener(new actionListener2());
     
     
                break;}
                finally{
        try {
            rs4.close();
     
        } catch (SQLException ex) {
            Logger.getLogger(Interface_admin.class.getName()).log(Level.SEVERE, null, ex);
        }
                        }  
        }
     
     
     
     
     
     
     
     
     
     
    }}
        public static void main(String args[]) {
     
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Interface_admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
     
     
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Interface_admin().setVisible(true);
                }
            });
        }
     
     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField3;
     
    }

  6. #6
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Billets dans le blog
    2
    Par défaut
    Est-ce bien toujours la même, à la même ligne ? Tu aurais une stacktrace à montrer peut-être ?

    Dans ton code, tu continues par ailleurs à enregistrer plusieurs écouteurs actionlistener2 sur le même composant...
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

Discussions similaires

  1. Problème affichage JTextField avec ActionListener
    Par phoenix420 dans le forum Composants
    Réponses: 5
    Dernier message: 24/06/2010, 13h29
  2. les problèmes d'excel avec rapport d'erreur
    Par the-geut dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 10/05/2010, 17h52
  3. Problème avec une erreur de type [DCC Erreur]
    Par colorid dans le forum Langage
    Réponses: 5
    Dernier message: 24/09/2007, 20h04
  4. [VB6] Problème de textbox multiligne qui génére une erreur
    Par bb62 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 20/02/2006, 16h21
  5. Problème avec fichier Excel génère erreur
    Par Yanmeunier dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 17/02/2006, 11h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo