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

Java Discussion :

Forcer un ActionListener


Sujet :

Java

  1. #1
    Membre régulier
    Profil pro
    Ingénieur
    Inscrit en
    Janvier 2009
    Messages
    209
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Janvier 2009
    Messages : 209
    Points : 95
    Points
    95
    Par défaut Forcer un ActionListener
    Bonjour ,

    Problématique basique que je viens de me heurter , voila :
    J'ai une JFrame ou j'ai mis tous mes composants
    J'ai une class qui me permet de récupérer le (getSource() ) de ma JFrame principale .
    Sur une des action , j'ouvre une autre petite JFrame mais lorsque je veux créer un evemenent lié a cet JFrame ==> ça plante .

    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
    public class ActionFrame extends AbstractAction implements ActionListener  {
     
    	FramePr a;
             FramePanel b; 
     
     
    	public ActionFenetre(FramePr aa) {
    		// TODO Auto-generated constructor stub
    		this.a=aa; 
    	}
     
     
     
    	public ActionFenetre(FramePanel cc) {
    		// TODO Auto-generated constructor stub
    		this.c=cc;
    	}
    public void actionPerformed(ActionEvent ev) {
     
    	Object sourceEv = ev.getSource() ; 
    	int index = a.ComboBox.getSelectedIndex();
     
    // quand je fait mon choix ==> j'ai une frame qui s'ouvre 
     
    if (sourceEv == a.ComboBox && index==2)
    	{
    			String[] champs = {"a","b","c" } ;
    			String[] btnSM ={"Ajouter","Valider","Annuler"};
    			c = new FrameFenetre(0,champs.length,"TOTO" ,champs , btnSM,index);
     
    	}
    if(sourceEv ==c.jpfSM.btnFrame[0] && index ==1 ) 
       {
     // ça ne marche pas ! 
           Objet o = new Objet("aaa");
          a.panel.add(o) ;
     
      }
    Lorsque je rempli les données nécessaire sur ma nouvelle Frame ==> ça plante !

    Voila la class FramePanel qui fait appel a une autre class

    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
    public class FramePanel extends JFrame {
     
     
     
    	FramePr main = new FramePr(); 
     
    	private int IndexChoice = 0; 
     
    	public FramePanel (int posChamps , int posBtn, String nameDialog , String[] Champs ,String[] btnSM ,int index)
    	{
     
    		jpfSM = new JPanelFrame(nameDialog);
    		JLabel lbl = new JLabel(String.valueOf(index));
    		tk = this.getToolkit(); 
    		dim = tk.getScreenSize(); 
    		int width = dim.width; 
    		int height = dim.height; 
    		jpfSM.addTxt(1,posChamps,Champs);
    		jpfSM.addButton(0, posBtn, btnSM); 
    		p = new JPanel();
    		p.setLayout(new BorderLayout());
    		p.add("North",jpfSM);
    		p.add("South",lbl);
    		add(p);
    		setIndexChoice(Integer.valueOf(lbl.getText()));
     
    		setSize(width/3,height/3);
    		setLocation(width/3, height/3);
    		//setModal(true);
    		for(int i = 0 ; i<3 ; i++)
    		{
    		jpfSM.btnFrame[i].addActionListener(this);
    		}
    		setVisible(true);
     
    	}
    En gros , lorsque je clique sur le Bouton de la nouvelle fenetre , j'ajoute un objet sur la fenetre principale ! .

  2. #2
    Membre expérimenté
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2004
    Messages
    1 184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 184
    Points : 1 745
    Points
    1 745
    Par défaut
    Bonjour,

    ==> ça plante !
    As-tu une exception / stacktrace / message d'erreur ? quelque chose de plus précis que "ça plante" ?

  3. #3
    Membre confirmé

    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2011
    Messages
    181
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Août 2011
    Messages : 181
    Points : 519
    Points
    519
    Billets dans le blog
    1
    Par défaut
    slt, c'est encore moi,
    Pour commencer, il est préférable de mettre a chaque composant son propre actionListener, ça t'évitra de tester a chaque fois, quel est le composant sur le quel cet événement s'est declanché, ça consommera plus de mémoire, mais accélérera ton programme .
    Pour le plantage, laisse moi deux jours, en ce moment, je suis occupé, je suis sur qu'il y a d'autre qui pourrons t'aider, mes excuses .
    A+ .

  4. #4
    Membre régulier
    Profil pro
    Ingénieur
    Inscrit en
    Janvier 2009
    Messages
    209
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Janvier 2009
    Messages : 209
    Points : 95
    Points
    95
    Par défaut
    Citation Envoyé par Mathieu.J Voir le message
    Bonjour,



    As-tu une exception / stacktrace / message d'erreur ? quelque chose de plus précis que "ça plante" ?

    Voila l'erreur :
    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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ActionFenetre.actionPerformed(ActionFenetre.java:69)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.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 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.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(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)

  5. #5
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 074
    Points : 7 978
    Points
    7 978
    Par défaut
    Regarde dans ActionFenetre.java a la ligne 69 c'est la que se trouve l'erreur et ca tu ne nous l'as pas fourni. (Les 2 bouts de code fournis ne permettent meme pas de le compiler)
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  6. #6
    Membre émérite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2008
    Messages
    1 190
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2008
    Messages : 1 190
    Points : 2 657
    Points
    2 657
    Par défaut
    Citation Envoyé par Eausport Voir le message
    Voila l'erreur :
    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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ActionFenetre.actionPerformed(ActionFenetre.java:69)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.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 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.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(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)
    Et si tu nous donnais du coup le code de ActionFenetre on pourrait t'aider. Mais comme l'erreur l'indique un objet à la ligne 69 est null mais tu essayes de l'utiliser quand même. Met des traces pour savoir ce qui est a null.

  7. #7
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 074
    Points : 7 978
    Points
    7 978
    Par défaut
    Y'a de l'echo ici
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  8. #8
    Membre régulier
    Profil pro
    Ingénieur
    Inscrit en
    Janvier 2009
    Messages
    209
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Janvier 2009
    Messages : 209
    Points : 95
    Points
    95
    Par défaut
    Citation Envoyé par wax78 Voir le message
    Y'a de l'echo ici
    Dans ma actionPerformed()
    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
    Object sourceEv = ev.getSource() ; 
    int index = action.ComboSource.getSelectedIndex();
    		if (sourceEv == action.ComboSource && index==1)
    		{
    			String[] champs = {"a","b","c" } ;
    			String[] btnSM ={"Ajouter","Valider","Annuler"};
    			csm = new FramePanel(0,champs.length,"titreFrame",champs , btnSM ,index);
     
     
    			if (sourceEv == csm.jpfSM.btnFrame[0])
    			{
     
     
    				String message = "ça marche ! "; 
    				System.out.println(message);
    			}

  9. #9
    Membre expérimenté
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2004
    Messages
    1 184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 184
    Points : 1 745
    Points
    1 745
    Par défaut
    Tu utilises un élément qui n'est pas initialisée à la ligne 69.
    Tu devrais également préciser quelle est la ligne 69 qui provoque la NullPointerException.

  10. #10
    Membre émérite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2008
    Messages
    1 190
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2008
    Messages : 1 190
    Points : 2 657
    Points
    2 657
    Par défaut
    Citation Envoyé par Eausport Voir le message
    Dans ma actionPerformed()
    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
    Object sourceEv = ev.getSource() ; 
    int index = action.ComboSource.getSelectedIndex();
    		if (sourceEv == action.ComboSource && index==1)
    		{
    			String[] champs = {"a","b","c" } ;
    			String[] btnSM ={"Ajouter","Valider","Annuler"};
    			csm = new FramePanel(0,champs.length,"titreFrame",champs , btnSM ,index);
     
     
    			if (sourceEv == csm.jpfSM.btnFrame[0])
    			{
     
     
    				String message = "ça marche ! "; 
    				System.out.println(message);
    			}
    Ouais mais là on sait pas quelle ligne est la 69, mais on progresse

  11. #11
    Membre régulier
    Profil pro
    Ingénieur
    Inscrit en
    Janvier 2009
    Messages
    209
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Janvier 2009
    Messages : 209
    Points : 95
    Points
    95
    Par défaut
    il me pointe sur cette ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int index = action.ComboSource.getSelectedIndex();
    mais bon , j'ai changé ma configuration et là ça marche .

  12. #12
    Membre émérite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2008
    Messages
    1 190
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2008
    Messages : 1 190
    Points : 2 657
    Points
    2 657
    Par défaut
    Citation Envoyé par Eausport Voir le message
    il me pointe sur cette ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int index = action.ComboSource.getSelectedIndex();
    mais bon , j'ai changé ma configuration et là ça marche .
    Ta configuration?

    Et le problème était clair, soit action ou soit ComboSource était à null.

  13. #13
    Membre confirmé

    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2011
    Messages
    181
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Août 2011
    Messages : 181
    Points : 519
    Points
    519
    Billets dans le blog
    1
    Par défaut
    slt,
    Essaye
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    if (action == null)
           System.out.println ("action a null");
    else
        System.out.println ("action.action.ComboSource a null");
    Avant de mettre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int index = action.ComboSource.getSelectedIndex();
    A+, bonne chance .

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 15/11/2007, 12h31
  2. [stringgrid] forcer l'affichage de la VscrollBar
    Par qi130 dans le forum Composants VCL
    Réponses: 2
    Dernier message: 10/11/2003, 14h03
  3. Forcer la saisie en MAJ dans un Formulaire
    Par lolos dans le forum IHM
    Réponses: 4
    Dernier message: 12/08/2003, 10h57
  4. Forcer jump short ou far
    Par Blustuff dans le forum Assembleur
    Réponses: 6
    Dernier message: 02/03/2003, 02h03
  5. Forcer l'affichage des dizaines...
    Par pataluc dans le forum Langage
    Réponses: 4
    Dernier message: 26/11/2002, 19h38

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