Salut a tous,
Je suis un gros newbie des JTable et je voudrai savoir pourquoi ma methode d'ajout de ligne dans un DefaultTableModel ne fonctionne pas !!
Je vous laisse un bout de code :
Je me retrouve avec l'erreur suivante :
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 //initialisation de ma fenetre graphique public void init() { Image icon = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("images/myotis.png")); setIconImage(icon); initComponents(); // Centrage de la fenetre Toolkit tk = Toolkit.getDefaultToolkit() ; Dimension d = tk.getScreenSize() ; // resolution de l'ecran this.hEcran = (int)(d.getHeight()); // hauteur de l'ecran this.lEcran = (int)(d.getWidth()); // largeur de l'ecran setLocation((lEcran-this.getWidth())/2,(hEcran-this.getHeight())/2); initJTable(); Object[] o = new Object[5]; for(int i=0 ; i<5 ; i++) o[i] = ""+i ; tm.addLigne(o); tm.addLigne(o); detection.setModel(tm ); this.validate(); this.setVisible(true); } // Methode d'initialisation de la JTable des voies private void initJTable() { this.tm = new TableModele(); DefaultTableCellRenderer dr = new DefaultTableCellRenderer(); dr.setHorizontalAlignment(SwingConstants.CENTER); this.detection.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); this.detection.setModel(tm); for(int i=0;i<5;i++) { TableColumn col = this.detection.getColumnModel().getColumn(i); col.setCellRenderer(dr); } } // Classe representant les elements du tableau public class TableModele extends DefaultTableModel { private Object[][] data ; private Object[] colonnesNom ; private boolean ALLOW_ROW_SELECTION = true; private int row_count = 1 ; public TableModele() { this.colonnesNom = new Object[5]; this.data = new Object[row_count][5]; init(); } public void init() { // Initialisation des titres this.colonnesNom[0] = new String("Heure"); this.colonnesNom[1] = new String("Ratio Detect."); this.colonnesNom[2] = new String("Ratio Max."); this.colonnesNom[3] = new String("Max Ampl."); this.colonnesNom[4] = new String("Count"); // Initialisation des cellules detection.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (ALLOW_ROW_SELECTION) { // true by default ListSelectionModel rowSM = detection.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel)e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("Lignes selectionnables"); } else { int selectedRow = lsm.getMinSelectionIndex(); System.out.println("ligne " + selectedRow + " est selectionnee"); } } }); } else { detection.setRowSelectionAllowed(false); } super.setDataVector(this.data,this.colonnesNom); } public void addLigne(Object[] o) { super.addRow(o); } public Object getValueAt(int ligne, int col){return data[ligne][col];} public String getColumnName(int col){return (String)colonnesNom[col];} public void setValueAt(Object value, int ligne, int col) { this.data[ligne][col] = value ; fireTableCellUpdated(ligne, col); } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { //Note that the data/cell address is constant, //no matter where the cell appears onscreen. return false ; } }
Merci pour votre aide car je galère depuis hier matin sur ce truc !!Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at leesa.FenetreDetectionHDD$TableModele.getValueAt(FenetreDetectionHDD.java:141)
at javax.swing.JTable.getValueAt(JTable.java:2638)
at javax.swing.JTable.prepareRenderer(JTable.java:5652)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
at javax.swing.JComponent.paintComponent(JComponent.java:763)
at javax.swing.JComponent.paint(JComponent.java:1027)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:285)
at javax.swing.RepaintManager.paint(RepaintManager.java:1128)
at javax.swing.JComponent.paint(JComponent.java:1013)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1797)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Partager