Bonjour,
je débute en Java et je suis en train de développer une petite appli graphique.
J'ai différents écrans et pour chacun j'ai créé une classe. J'ai en plus d'autres classes dont la classe activities
j'ai un écran qui permet de choisir notre activité :
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 import javax.swing.JOptionPane;import java.io.Serializable; public class activites implements Serializable { private String code,libelle; public activites() { code =""; libelle=""; } public activites(String code,String libelle) { this.code = code; this.libelle= libelle; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getLibelle() { return libelle; } public void setLibelle(String libelle) { this.libelle = libelle; } public void afficheActivites() { JOptionPane.showMessageDialog(null,"Libellé : " + libelle); } }
Je récupère cette données pour l'utiliser ensuite dans une requête sur un autre écran...
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 import java.awt.FlowLayout; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class ecr_choix_act implements ActionListener { private JFrame fen = new JFrame(); public JComboBox liste_activite = new JComboBox(); public activites uneActivite = new activites(); public ecr_choix_act() throws SQLException { Connection con = null; ResultSet resultats = null; con = DriverManager.getConnection("jdbc:mysql://localhost/batonnage?zeroDateTimeBehavior=convertToNull","root",""); Statement st = con.createStatement(); resultats = st.executeQuery("SELECT libelle from canaux"); while(resultats.next()) { liste_activite.addItem(resultats.getString("libelle")); } JLabel lbl_titre = new JLabel("Choix de l'activité :"); JButton btn = new JButton("Valider"); btn.addActionListener(this); fen.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,10,5)); fen.getContentPane().add(lbl_titre); fen.getContentPane().add(liste_activite); fen.getContentPane().add(btn); fen.setSize(300, 200); fen.setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub -->erreur est à la ligne suivante ecran.uneActivite = (activites)(liste_activite.getSelectedItem()); if(liste_activite.getSelectedIndex()==0) { fen.setVisible(false); try { ecr_batonnage e = new ecr_batonnage(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { fen.setVisible(false); try { ecr_choix_btq e = new ecr_choix_btq(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Voici l'erreur donnée par eclipse :
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to activites
at ecr_choix_act.actionPerformed(ecr_choix_act.java:41)
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.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.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)
Mais je ne vois pas comment résoudre le problème....
Merci de votre aide !
Partager