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
|
// ui de ton modèle
public TableDemo() {
...
JTable table = new JTable(new MyTableModel());
...
}
//model défini pour ta JTable
// il faut que tu complete les tableau representant les nom de colonne et celui //contenant les objets
//l'affichage se fera alors tout seul
class MyTableModel extends AbstractTableModel {
private String[] columnNames = ...//nom de tes colonnes...
private Object[][] data = ...//objet contenu dans ta tables...
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
//ici faire les test nécéssaire et mettre le i++ si le test est positif
data[row][col] = value;
fireTableCellUpdated(row, col);
}
...
} |
Partager