Bonjour,
Je pense que mon problème est dû au fait que j'utilise l'architecture MVC.
Je vais laisser le code entier plus bas.
J'ai une appli(JFrame) avec 4 JPanels
Voici comment ils se composent:
*- PanelGauche est un panel qui est tjs visible avec un bouton
*- PanelPrincipal est un panel qui est tjs visible qui continet 2 autres panel:
** - PanelSous1
** - PanelSous2
J'utilise un CardLayout pour le PanelPrincipal
Quand je clique sur le bouton "test", je voudrais pouvoir afficher soit le PanelSous1, soit le PanelSous2.
Voici un bout du code que j'utilise (ici juste des remove pour le test):
mise en place du PanelPrincipal:
et le code du bouton
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 private JPanel getPanelPrincipal() { if (PanelPrincipal == null) { PanelPrincipal = new JPanel(); PanelPrincipal.setLayout(new CardLayout()); PanelPrincipal.setBounds(new Rectangle(138, 16, 315, 250)); PanelPrincipal.setBackground(new Color(238, 0, 0)); PanelPrincipal.add(getPanelSous1(), getPanelSous1().getName()); //PanelPrincipal.remove(getPanelss()); PanelPrincipal.add(getPanelSous2(), getPanelSous2().getName()); } return PanelPrincipal; }
Ceci fonctionne quand je clique sur le bouton mes sous panels disparaissent
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 private JButton getBtnTest() { if (btnTest == null) { btnTest = new JButton(); btnTest.setBounds(new Rectangle(19, 38, 59, 26)); btnTest.setText("Test"); btnTest.setActionCommand("test"); btnTest.addActionListener(controleur); btnTest.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed() PanelPrincipal.remove(getPanelss()); PanelPrincipal.remove(getPanelss2()); } }); } return btnTest; }
Maintenant que j'utilise une architecture MVC, si je remets le code du bouton dans une méthode [de ma classe JFrame ]:
et que via le controleur j'appelle cette méthode:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 public void changerPanel(){ //this.PanelPrincipal.remove(getPanelss()); //PanelPrincipal.validate(); //PanelPrincipal.getLayout(); PanelPrincipal.remove(getPanelSous1()); PanelPrincipal.remove(getPanelSous2()); }
J'obtiens une erreur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("test")){ this.vue.changerPanel(); } }
Je suppose que je n'arrive pas à capter le bon container ou quelque chose comme ça dans mon controlleur.
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 dbbon.controleur.DBBonControleur.actionPerformed(DBBonControleur.java:23) 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)
Merci de votre aide.
Voici le code mon appli:
Mon Main qui utilise le modèle
Ma GUI
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package dbbon.tests; import dbbon.gui.frame.EcranPrincipal; import dbbon.produit_catalogue.PCA_SQL; public class AppliTest { public static void main(String[] args) { PCA_SQL pca_sql = new PCA_SQL(); EcranPrincipal ecranPrincipal = new EcranPrincipal(pca_sql); ecranPrincipal.setVisible(true); } }
et mon controleur
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 package dbbon.gui.frame; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JPanel; import javax.swing.JFrame; import java.util.EventListener; import dbbon.controleur.DBBonControleur; import dbbon.gui.panels.PanelTest; import dbbon.produit_catalogue.PCA_SQL; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.JMenuBar; import javax.swing.JMenu; import java.awt.Rectangle; import javax.swing.JMenuItem; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.BorderFactory; import javax.swing.border.TitledBorder; import java.awt.Font; import java.awt.Color; import java.util.ArrayList; import javax.swing.JScrollPane; import javax.swing.JTable; import java.awt.Point; import javax.swing.SwingConstants; import javax.swing.JButton; import java.awt.SystemColor; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.CardLayout; public class EcranPrincipal extends JFrame { private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); // @jve:decl-index=0: private static final long serialVersionUID = 1L; private DBBonControleur controleur; private JPanel jContentPane = null; private JPanel PanelGauche = null; private JButton btnTest = null; private JPanel PanelPrincipal = null; private JPanel PanelSous1 = null; private JPanel PanelSous2 = null; /** * This is the default constructor */ public EcranPrincipal(PCA_SQL modele) { super(); controleur= new DBBonControleur(this,modele); initialize(); this.setLocation((screen.width - this.getSize().width)/2,(screen.height - this.getSize().height)/2); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(493, 320); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.setBackground(new Color(238, 255, 238)); jContentPane.add(getPanelGauche(), null); jContentPane.add(getPanelPrincipal(), null); } return jContentPane; } /** * This method initializes PanelGauche * * @return javax.swing.JPanel */ private JPanel getPanelGauche() { if (PanelGauche == null) { PanelGauche = new JPanel(); PanelGauche.setLayout(null); PanelGauche.setBounds(new Rectangle(11, 15, 82, 257)); PanelGauche.setBackground(new Color(0, 238, 238)); PanelGauche.add(getBtnTest(), null); } return PanelGauche; } /** * This method initializes btnTest * * @return javax.swing.JButton */ private JButton getBtnTest() { if (btnTest == null) { btnTest = new JButton(); btnTest.setBounds(new Rectangle(19, 38, 59, 26)); btnTest.setText("Test"); btnTest.setActionCommand("test"); btnTest.addActionListener(controleur); /* btnTest.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed() //PanelPrincipal.remove(getPanelss()); //PanelPrincipal.remove(getPanelss2()); } }); */ } return btnTest; } /** * This method initializes PanelPrincipal * * @return javax.swing.JPanel */ private JPanel getPanelPrincipal() { if (PanelPrincipal == null) { PanelPrincipal = new JPanel(); PanelPrincipal.setLayout(new CardLayout()); PanelPrincipal.setBounds(new Rectangle(138, 16, 315, 250)); PanelPrincipal.setBackground(new Color(238, 0, 0)); PanelPrincipal.add(getPanelSous1(), getPanelSous1().getName()); //PanelPrincipal.remove(getPanelss()); PanelPrincipal.add(getPanelSous2(), getPanelSous2().getName()); } return PanelPrincipal; } public void changerPanel(){ //this.PanelPrincipal.remove(getPanelss()); //PanelPrincipal.validate(); //PanelPrincipal.getLayout(); PanelPrincipal.remove(getPanelSous1()); PanelPrincipal.remove(getPanelSous2()); } /** * This method initializes PanelSous1 * * @return javax.swing.JPanel */ private JPanel getPanelSous1() { if (PanelSous1 == null) { PanelSous1 = new JPanel(); PanelSous1.setLayout(null); PanelSous1.setName("jPanel"); } return PanelSous1; } /** * This method initializes PanelSous2 * * @return javax.swing.JPanel */ private JPanel getPanelSous2() { if (PanelSous2 == null) { PanelSous2 = new JPanel(); PanelSous2.setLayout(null); PanelSous2.setName("Panelss2"); PanelSous2.setBackground(new Color(149, 153, 153)); } return PanelSous2; } } // @jve:decl-index=0:visual-constraint="10,10"
Merci
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 package dbbon.controleur; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import dbbon.gui.frame.EcranPrincipal; import dbbon.gui.panels.PanelTest; import dbbon.produit_catalogue.PCA_SQL; public class DBBonControleur implements ActionListener{ private EcranPrincipal vue; private PCA_SQL modele; /** * Crée un controleur pour la fenêtre principale * @param vue La fenêtre principale. * @param modele Le modèle du MVC. */ public DBBonControleur(EcranPrincipal vue, PCA_SQL modele) { // TODO Auto-generated constructor stub } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("test")){ this.vue.changerPanel(); } } }
PS: Est-ce mieux de faire un remove ou une sorte de setVisible?
Partager