Bonjour à tous,
Alors voila mon problème :
J'ai :
- une Jlist qui contient des éléments
- un bouton supprimer
- un textField
- un bouton ajouter
Je voudrai pouvoir retirer des éléments de la JList à l'aide du bouton retirer et ajouter des éléments toujours dans la JList à l'aide du TextField et du bouton ajouter.
Et voici mon code qui ne marche pas :
Les lignes sources d'erreurs sont :
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 public class Assistant extends JFrame{ private JPanel p_methodes_liste; private JPanel p_methodes_boutons; private JPanel p_attributs_liste; private JPanel p_attributs_boutons; private JPanel p_global; private JLabel titre_methodes; private JLabel titre_attributs; private JList methodes; private JList attributs; private JTextField saisie_methodes; private JTextField saisie_attributs; private JButton retirer_methodes; private JButton retirer_attributs; private JButton ajouter_methodes; private JButton ajouter_attributs; private String nomsCouleurs[] = { "Noir", "Bleu", "Cyan" }; public Assistant(CellulePerso cellule){ this.setTitle("Assistant"); Container c = this.getContentPane(); titre_methodes = new JLabel("Liste des méthodes :"); titre_attributs = new JLabel("Liste des attributs :"); methodes = new JList(nomsCouleurs); attributs = new JList(); saisie_methodes = new JTextField(); saisie_attributs = new JTextField(); retirer_methodes = new JButton("Retirer cette méthode"); retirer_attributs = new JButton("Retirer cet attribut"); ajouter_methodes = new JButton("Ajouter cette méthode"); ajouter_attributs = new JButton("Ajouter cet attribut"); p_methodes_liste = new JPanel(); p_methodes_liste.setLayout(new GridLayout(2,1)); p_methodes_liste.add(titre_methodes); p_methodes_liste.add(new JScrollPane(methodes)); p_methodes_boutons = new JPanel(); p_methodes_boutons.setLayout(new GridLayout(3,1)); p_methodes_boutons.add(retirer_methodes); p_methodes_boutons.add(saisie_methodes); p_methodes_boutons.add(ajouter_methodes); p_attributs_liste = new JPanel(); p_attributs_liste.setLayout(new GridLayout(2,1)); p_attributs_liste.add(titre_attributs); p_attributs_liste.add(new JScrollPane(attributs)); p_attributs_boutons = new JPanel(); p_attributs_boutons.setLayout(new GridLayout(3,1)); p_attributs_boutons.add(retirer_attributs); p_attributs_boutons.add(saisie_attributs); p_attributs_boutons.add(ajouter_attributs); p_global = new JPanel(); p_global.setLayout(new GridLayout(2,2)); p_global.add(p_methodes_liste); p_global.add(p_methodes_boutons); p_global.add(p_attributs_liste); p_global.add(p_attributs_boutons); c.add(p_global); ajouter_methodes.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ final DefaultListModel model = (DefaultListModel)methodes.getModel(); String s = saisie_methodes.getText(); model.addElement(s); SwingUtilities.invokeLater(new Runnable() { public void run() { methodes.ensureIndexIsVisible(model.getSize()-1); } }); } } ); retirer_methodes.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ int[] selected = methodes.getSelectedIndices(); DefaultListModel model = (DefaultListModel)methodes.getModel(); for(int i=0; i < selected.length; ++i) { model.removeElementAt(selected[i] - i); } } } ); this.pack(); this.setVisible(true); } }
final DefaultListModel model = (DefaultListModel)methodes.getModel();
et
DefaultListModel model = (DefaultListModel)methodes.getModel();
Visiblement il aime ma façon de caster...
Si vous aviez une idée SVP
Partager