Bonjour,

J'ai créé une jTable dynamique en ligne et colonne, mais j'ai un petit souci, dès que je rentre une valeur dans une cellule, ensuite toute la ligne est initialisée avec cette même valeur.
Je pense que mon souci vient soit de getValueAt et/ou setValueAt, mais je n'arrive pas a corriger.

Voici 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
 
public class SmartToolTable extends javax.swing.JDialog 
{
  private TableData tableData = null;
  JTableHeader jTableHeader = null; 
  TableColumnModel tableColumnModel = null; 
  /** Creates new form SmartToolTable */
  public SmartToolTable(java.awt.Frame parent, boolean modal) 
  {
    super(parent, modal);
    initComponents();
    jTableHeader = jtTable.getTableHeader();
    tableColumnModel = jTableHeader.getColumnModel();
 
    tableData = new TableData();
    jtTable.setAutoCreateColumnsFromModel(false);
    jtTable.setModel(tableData);
 
    refresh();
  }
 
  public void refresh()
  {
    // Set row height
    jtTable.setRowHeight(25);
    jtTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
  }
 
  // Class that represent a row
  public class TableRow 
  {
    private ArrayList<String> cells;
 
    public TableRow(int cellCount) 
    {
      super();
      cells = new ArrayList<String>();
      for(int i=0; i<cellCount; i++)
      {
        addCell("");
      }
    }
 
    public String getCell(int cell)
    {
      if (cell<cells.size())
        return (String)cells.get(cell);
      else
        return (null);
    }
    public void setCell(int cell, String value) 
    {
      if (cell<cells.size())
        cells.set(cell, value);
    }
 
    public void addCell(String value)
    {
      cells.add(value);
    }
  }
 
  static class ColumnData
  {
    public String colTitle;
    public int colWidth;
    public int colAlignment;
    public boolean colEditable;
 
    public ColumnData(String title, int width, int alignment, boolean editable)
    {
      this.colTitle = title;
      this.colWidth = width;
      this.colAlignment = alignment;
      this.colEditable = editable;
    }
  }
 
  class TableData extends AbstractTableModel
  {
    private static final long serialVersionUID = 1L;
    private ArrayList<ColumnData> columns;
    private ArrayList<TableRow> rows;
 
    public TableData()
    {
      rows = new ArrayList<TableRow>();
      columns = new ArrayList<ColumnData>();
      clear();
    }
 
    public void defaultColumns()
    {
      addColumn("ToolIndex", 40, javax.swing.JLabel.LEFT, true);
      addColumn("ToolNumber", 40, javax.swing.JLabel.LEFT, true);
      addColumn("Select objects", 100, javax.swing.JLabel.LEFT, true);
      addColumn("DIA (mm)", 40, javax.swing.JLabel.LEFT, true);
      addColumn("Predrill (mm)", 40, javax.swing.JLabel.LEFT, true);
    }
 
    public void addColumn(String colName, int colWidth, int colAlignement, boolean colEditable)
    {
      ColumnData cd = new ColumnData(colName, colWidth, colAlignement, colEditable);
      columns.add(cd);
      TableColumn tc = new TableColumn();  
      tc.setHeaderValue(colName);  
      tc.setWidth(colWidth);
      DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
      renderer.setHorizontalAlignment(colAlignement);
      tc.setCellRenderer(renderer);
      tableColumnModel.addColumn(tc);  
      jtTable.updateUI();  
    }
 
    public int getColCount()
    {
      return(columns.size());
    }
 
    public void clear()
    {
      rows.clear();
      columns.clear();
      // Delete existing columns
      while(jtTable.getColumnCount()>0) 
      {
        TableColumn colToDelete = jtTable.getColumnModel().getColumn(jtTable.getColumnCount() - 1);
        jtTable.removeColumn(colToDelete);
        jtTable.validate();
      }
      defaultColumns();
    }
 
    public void sortRows(int dri_sortCol)
    {
      //Collections.sort(rows, new ColComparator(sortCol, sortAsc));
    }
 
    public int getRowCount()
    {
      return rows==null ? 0 : rows.size();
    }
 
    public int getColumnCount()
    {
      return columns.size();
    }
 
    public String getColumnName(int colIndex)
    {
      ColumnData cd = (ColumnData)columns.get(colIndex);
      String str = cd.colTitle;
      return str;
    }
 
    public boolean isCellEditable(int nRow, int nCol)
    {
      ColumnData cd = (ColumnData)columns.get(nCol);
      return(cd.colEditable);
    }
 
    public void addRow()
    {
      TableRow row = new TableRow(getColCount());
      rows.add(row);
      fireTableDataChanged();
    }
 
    public void remove(int row)
    {
      if ((row>-1) && (row<rows.size()))
      {
        rows.remove(row);
        fireTableDataChanged();
      }
    } 
 
    public Object getValueAt(int nRow, int nCol) 
    {
      if (nRow < 0 || nRow>=getRowCount()) 
      {
        return "";
      }
      TableRow row = rows.get(nRow);
      System.out.println("nRow=" + nRow + " nCol=" + nCol);
      return row.getCell(nCol);
    }
 
    public void setValueAt(Object value, int nRow, int nCol) 
    {
      if (nRow < 0 || nRow>=getRowCount()) return;
      TableRow row = rows.get(nRow);
      String svalue = value.toString();
      row.setCell(nCol, svalue);
      this.fireTableCellUpdated(nRow, nCol);
    }
 
  }
PS, ce n'est qu'une partie de mon code

Merci

Denis