Salut,
Je voudrais ajouter une ligne à ma JTable, lorsque l'utilisateur clique sur le bouton cmd1:
Voici le code associé au bouton cmd1où table est ma JTable)
quand je clique sur le bouton cmd1 j'ai ça à l'écran:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 public void actionPerformed(ActionEvent e) { DefaultTableModel table_defaut = (DefaultTableModel)table.getModel(); Object [] nouv_ligne = null; table_defaut.addRow(nouv_ligne); }
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: jtable.Tableau$1
at jtable.Tableau.actionPerformed(Tableau.java:54)
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.pumpOneEventForHierarchy(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)
Merci de m'aider, au cas où se serait necessaire, voilà tout le code:
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 package jtable; import javax.swing.table.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Tableau extends JPanel implements ActionListener { JTable table; // ma JTable public Tableau() { setLayout(new FlowLayout()); // Le model de données TableModel datamodel = new AbstractTableModel() { public int getRowCount(){return 3;} public int getColumnCount(){return 4;} public Object getValueAt(int ligne, int col) { return null;} public boolean isCellEditable(int ligne, int col){return true;} public String getColumnName(int col) { String val = null; switch(col){ case 0: val = "Nom";break; case 1: val = "Prénom";break; case 2: val = "Age";break; case 3: val = "Genre";break; } return val; } }; table = new JTable(datamodel); JScrollPane bardefil = new JScrollPane(table); // le bouton sur lequel il faut cliquer pour ajouter la ligne JButton cmd1 = new JButton("Ajouter une Ligne"); cmd1.addActionListener(this); add(bardefil); add(cmd1); } public void actionPerformed(ActionEvent e) { DefaultTableModel table_defaut = (DefaultTableModel)table.getModel(); Object [] nouv_ligne = null; table_defaut.addRow(nouv_ligne); } public static void main(String [] arg) { JFrame f = new JFrame("Les JTables"); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); Tableau panel = new Tableau(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); c.add(panel); f.pack(); f.setVisible(true); } }
Partager