Bonsoir tout le monde,
J'ai commencé une petite appli avec swing et j'ai quelques erreurs que je ne comprend pas...
En fait, j'ai fait une frame avec un Panel (SelectionPanel) qui est définis dès le départ comme container root. J'aimerais pouvoir changer ce container sur clique sur un bouton (contenu dans SelectionPanel), donc j'ai créer un ActionListener pour SelectionPanel (SelectionListner) et quand il y'a clique sur le bouton, j'appel une fonction de la classe de mon Frame (MainFram) qui change le container pour celui voulu (j'envois le titre du container et puis en fonction du tite je choisis lequel sera sélectionné, c'est pas terrible mais c'est provisoire).
Bref, tout ça pour dire qu'il me fait cette erreur :
Et je ne comprend pas pourquoi, pourtant tout les objet utilisé dans le code sont préalablement instanciés...
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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at SelectionListener.actionPerformed(SelectionListener.java:21) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3126) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1778) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
J'ai cru que c'est parceque je ne pouvais pas interarir directement avec mon frame et que seul la classe du main le pouvait... Alors j'ai bindé les fonction du frame sur le Main et j'ai appeler les fonctions en utilisant l'objet de mon main, mais même résultat...
A mon avis je fais une grave erreur quelque part mais je ne sais pas où...
Voici les parties importantes de mon code :
SelectionPanel :
SelectionListener :
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 import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JPanel; public class SelectionPanel extends JPanel{ private JButton displayButton; private SelectionListener mControler; private Main mApp; public SelectionPanel(Main app) { //we create the controler mControler = new SelectionListener(this, mApp); //create the button and add the controler displayButton = new JButton("Affichage"); displayButton.addActionListener(mControler); this.add(displayButton); } }
Les fonctions bindés dans le main :
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 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; public class SelectionListener implements ActionListener{ private SelectionPanel mView; private Main mApp; public SelectionListener (SelectionPanel view, Main app) { mView = view; mApp = app; } public void actionPerformed(ActionEvent arg0) { // FOR THE MOMENT WE DO LIKE THAT BUT IT IS PROVISORY String textButton=((JButton) arg0.getSource()).getText(); if(textButton == "Affichage"){ mApp.onSetActivity(mApp.onGetActivity(textButton)); System.out.println("Ouverture de l'activité affichage"); } } }
Les fonctions du frame :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public void onSetActivity(JPanel activity) { frame.setActivity(activity); } public JPanel onGetActivity(String activityName) { return frame.getActivity(activityName); }
L'activité (activity) correspond au panel qui sera affiché.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 //Set the panel (activity) which will be on screen public void setActivity(JPanel wantedActivity){ this.setContentPane(wantedActivity); } public JPanel getActivity(String activityName) { if(activityName == "Affichage"){ return mDisplayPanel; } return mSelectionPanel; }
Voilà, je pense que vous avez tous les éléments pour régler l'affaire.
Ca se trouve je fais n'importe quoi mais j'avais plutôt l'impression que ce que je fais est censé... En même temps j'ai pas une grande expérience du Java.
Merci de votre aide,
A+
dede
Partager