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 :

itemstatechanges sur plusieurs objets


Sujet :

AWT/Swing Java

  1. #1
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut itemstatechanges sur plusieurs objets
    Bonjour, j'ai 2 Jcomobobox dans ma fenêtre et je voudrais savoir laquel est "modifier" pour la fonction itemstatechange

    j'ai essayer ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    String s=e.getItem().toString();
    if (s.equals(annee))
    {
    }
    else if (s.equals(option))
    {
    }
    mes 2 composants s'appelent annee option...

    Merci de votre aide

  2. #2
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut
    Il te suffit de vérifier la source de l'évènement !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    JcomboBox combo1 = ... ;
    JcomboBox combo2 = ... ;
     
    public void itemStateChanged( ItemEvent e )
    {
     if ( e.getSource == combo1 )
     {
       // Action à faire avec le combo 1
     }
     else if ( e.getSource == combo2 )
     {
       // Action à faire avec le combo 2
     }
    }

  3. #3
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    j'ai fait ceci ;

    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
     
    public void itemStateChanged(ItemEvent e) 
    	{	
    		if (e.getSource()==annee)
    		{
    			tEtud.vider();
    			String anneeSelect = annee.getSelectedItem().toString();
    			String optionSelect = rempliOption(anneeSelect);
    			chargeTable(anneeSelect,optionSelect);
    			System.out.println("annee");
    		}
    		else if (e.getSource()==option)
    		{
    			String anneeSelect = annee.getSelectedItem().toString();
    			String optionSelect = ((ContenuCBX)option.getSelectedItem()).getCode();
    			chargeTable(anneeSelect,optionSelect);
    			System.out.println("test");
    		}
    	}
    mais cela me génere plein d'erreurs et je ne comprend pas pourquoi, cependant il reconnait bien chaque cbx

    Merci de votre aide

  4. #4
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut
    Citation Envoyé par franfr57
    cela me génere plein d'erreurs et je ne comprend pas pourquoi, cependant il reconnait bien chaque cbx
    Quel genre d'erreur ? A la compilation ou à l'exécution ? Tu peux nous donner les messages ?

  5. #5
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    alors c'est à l'éxécution, lorsque je clique sur ma jcombobox année voici les erreurs :

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at PanneauEtudiant.itemStateChanged(PanneauEtudiant.java:135)
    at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
    at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.JComboBox.intervalRemoved(Unknown Source)
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
    at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
    at javax.swing.JComboBox.removeAllItems(Unknown Source)
    at PanneauEtudiant.rempliOption(PanneauEtudiant.java:33)
    at PanneauEtudiant.itemStateChanged(PanneauEtudiant.java:128)
    at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
    at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    test
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at PanneauEtudiant.itemStateChanged(PanneauEtudiant.java:135)
    at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
    at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.JComboBox.intervalRemoved(Unknown Source)
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
    at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
    at javax.swing.JComboBox.removeAllItems(Unknown Source)annee

    at PanneauEtudiant.rempliOption(PanneauEtudiant.java:33)
    at PanneauEtudiant.itemStateChanged(PanneauEtudiant.java:128)
    at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
    at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  6. #6
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    	if (e.getSource()==annee)
    		{
    			tEtud.vider();
    			String anneeSelect = annee.getSelectedItem().toString();
    			String optionSelect = rempliOption(anneeSelect);
    			chargeTable(anneeSelect,optionSelect);
    			System.out.println("annee");
    		}
    Tu as un NullPointerException à la ligne 135 dans PanneauEtudiant.java. Puisque c'est apparu quand tu a cliqué sur le combo annee, je parrie que l'erreur est dans une des lignes ci dessus.
    annee.getSelectedItem() renvoie-t-il bien un objet différent de null ?
    Essaie avec ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    	if (e.getSource()==annee)
    		{
    			tEtud.vider();
                            Object selection = annee.getSelectedItem() ;
    			String anneeSelect = "" ;
                            if ( selection != null ) {
                                anneeSelect = selection .toString();
                            }
    			String optionSelect = rempliOption(anneeSelect);
    			chargeTable(anneeSelect,optionSelect);
    			System.out.println("annee");
    		}

  7. #7
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    J'ai trouver la ligne qui pose problème, elle se trouve dans ma fonction qui rempli la liste d'options :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    	private String rempliOption(String annee)
    	{
    		option.removeAllItems();
    		String sql="select DISTINCT options.code_option, lib_option from options,etudiant WHERE etudiant.code_option=options.code_option AND annee = '"+annee+"' ORDER BY options.lib_option";
    		ConnectionBD.remplirListeObjet(option, ConnectionBD.requeteAvecResultat(sql),2);
    		return ((ContenuCBX)option.getSelectedItem()).getCode();
    	}
    c'est la lige : option.removeAllItems();
    Je enlève tous les items pour en remettre en fonction de l'année choisi

    Cependant la ligne 135 est la suivante :
    String optionSelect = ((ContenuCBX)option.getSelectedItem()).getCode();
    je ne comprend pas alors que le cbx option a changé je veux récupérer juste le code lui correspondant...

    Merci de votre aide

  8. #8
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut
    La voilà sûrement l'erreur : tu fais option.removeAllItems();, ce qui "vide" le combobox option. Ensuite, je devine que tu remplis option avec le résultat d'une requête SQL. Certes tu le remplis, mais tu ne sélectionne aucun élément du combo ! Après, tu fais String optionSelect = ((ContenuCBX)option.getSelectedItem()).getCode();. Comme option n'a pas d'objet sélectionné, option.getSelectedItem() renvoie null, et appeler une méthode sur un objet null donne un NullPointerException !

    Avant le return tu devrais faire un truc du genre option.setSelectedIndex(0) (après avoir vérifié qu'il y a au moins un élément dans ton combo, bien sûr ).
    Si le combo est vide, tu renvoie null directement.

    ++

  9. #9
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    j'ai essayer mais l'erreur surivent toujours, apparement deux choses sont liés :
    -lorque je met en commentaire option.removeAllItems(); de ma fonction rempliOption je n'ai plus l'erreur, mais dans ma combobox les objet s'ajoute et s'ajoute par dessus
    - lorsque je met en commentaire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    else if (e.getSource()==option)
    		{
    			tEtud.vider();
    			String anneeSelect = annee.getSelectedItem().toString();
    			String optionSelect = ((ContenuCBX)option.getSelectedItem()).getCode();
    			chargeTable(anneeSelect,optionSelect);
    		}
    de la fonction itemstatechanged je n'ai plus l'erreur non plus, mes combobox se recharge parfaitement bien et pour chaque il y a un objet selectionné mais ma table ne se recharge plus en fonction du second combobox vu que c'est en commentaire

    VOila je suis certain que c'est 2 choses sont liés mais je ne vois pas comment résoudre ce problème bizarre

    Merci de votre aide

  10. #10
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    je ne comprend pas pourriez vous m'aider s'il vous plait...
    je cherche toujours sans résultat snif

  11. #11
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut
    Il faut que ru récupères l'objet sélectionné AVANT de vider ton combo ! Autrement dit, le removeAll se fait APRES le getSelectedItem.

  12. #12
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    non je veux récupérer l'objet du nouveau combobox qui se séléctionne automatiquement vu que quand je met en commentaire l'une des 2 choses cela fonctionne parfaitement comment explique tu ça?

  13. #13
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut
    Citation Envoyé par franfr57
    non je veux récupérer l'objet du nouveau combobox qui se séléctionne automatiquement vu que quand je met en commentaire l'une des 2 choses cela fonctionne parfaitement comment explique tu ça?
    Donc c'est bien ce que j'ai dit plus haut, remplir un combobox ne veut pas dire sélectionner un objet dans la liste. Après l'avoir rempli, tu dois faire option.setSelectedIndex(0). Mais avant de faire ça, vérifie qu'il existe au moins 1 objet dans le combo, sinon ça plante !

  14. #14
    Membre régulier
    Inscrit en
    Janvier 2006
    Messages
    716
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 716
    Points : 112
    Points
    112
    Par défaut
    je l'ai déja fait, mais j'"obtient toujours le même résultat....

Discussions similaires

  1. Realité augmentée : 1 seul shader sur plusieurs objets
    Par visualight dans le forum ActionScript 3
    Réponses: 0
    Dernier message: 17/11/2010, 14h39
  2. [PPT-2007] Utiliser du code VBA sur plusieurs objets et pas qu'un seul
    Par beegees dans le forum VBA PowerPoint
    Réponses: 1
    Dernier message: 25/03/2010, 09h55
  3. Réponses: 1
    Dernier message: 06/03/2010, 08h19
  4. Réponses: 12
    Dernier message: 09/09/2008, 14h48
  5. Requêtes : recherche de maxi sur plusieur Objet
    Par pertuis dans le forum Langage SQL
    Réponses: 6
    Dernier message: 08/03/2004, 15h28

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