Se déplacer d'un JPanel à un autre.
Bonjour à tous,
J'ai une JFrame principale, qui contient un JPanel nommé jPanelPrincipal.
Ce jPanelPrincipal me permet d'afficher des pannel dérivés comme CréationVideo ou ListerVideos.
Dans mon panel CréationVideo, j'ai des champs a remplir, et un bouton pour valider l'insertion en base de ma nouvelle vidéo.
Voilà mon problème :
Quand je valide l'insertion en base (qui fonctionne bien) j'aimerai rediriger mon affichage sur le panel ListerVideos.
Pour cela, j'ai créer une interface que j'implémente dans ma JFrame :
Code:
1 2 3 4 5 6 7
|
public interface IFrameDrawable {
public void redraw(JPanel panel);
} |
Et dans ma JFrame j'ai donc la méthode suivante :
Code:
1 2 3 4 5 6 7 8
|
public void redraw(JPanel panel) {
this.pannelPrincipal.removeAll();
this.pannelPrincipal.add(panel);
this.pannelPrincipal.setVisible(true);
this.pack();
this.repaint();
} |
Ainsi, dans mon panel CréationVideo :
Code:
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
|
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String nom = this.txtFieldNom.getText();
String duree = this.txtFieldDuree.getText();
String prix = this.txtFieldPrix.getText();
String genre_libelle = this.comboGenre.getSelectedItem().toString();
StringBuilder sb = new StringBuilder();
if(nom.isEmpty())
sb.append("Le champs nom est obligatoire \n");
if(duree.isEmpty())
sb.append("Le champs durée est obligatoire \n");
if(prix.isEmpty())
sb.append("Le champs prix est obligatoire \n");
if(sb.length() == 0)
{
try {
Double prix_double = 0.0;
prix_double = Double.parseDouble(prix);
Genre genre = new Genre(genre_libelle);
Video video = new Video(nom, duree, genre, prix_double);
VideoDAO videoDAO = new VideoDAO(session);
videoDAO.insererVidéo(video);
} catch (SQLException ex) {
Logger.getLogger(CreationVideo.class.getName()).log(Level.SEVERE, null, ex);
}
ClientLourd cl = ((ClientLourd)this.getParent());
cl.redraw(new ListeDesVideos(session));
}
else
txtError.setText(sb.toString());
} |
Le problème est que ce code ne fonctionne pas :
Code:
1 2 3
|
ClientLourd cl = ((ClientLourd)this.getParent());
cl.redraw(new ListeDesVideos(session)); |
Code:
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
| Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to IHM.ClientLourd
at IHM.CreationGenre.jButton1ActionPerformed(CreationGenre.java:105)
at IHM.CreationGenre.access$000(CreationGenre.java:14)
at IHM.CreationGenre$1.actionPerformed(CreationGenre.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) |
Voilà, je ne sais pas si c'est "basique" mais je n'ai pas trouvé de solution !
Merci beaucoup !