AbstractTableModel
package mesClass;
import javax.swing.table.AbstractTableModel;
public abstract class classTableau extends AbstractTableModel{
Object [][] ligne;
String [] colonne;
public classTableau() {
}
public classTableau(Object[][] ligne, String[] colonne) {
this.ligne = ligne;
this.colonne = colonne;
}
@Override
public int getRowCount() {
return ligne.length;
}
@Override
public int getColumnCount() {
return colonne.length;
}
@Override
public Object getValueAt(int i, int j) {
return ligne [i] [j];
}
@Override
public String getColumnName(int col) {
return this.colonne[col];
}
public void setLigne(Object[][] ligne) {
this.ligne = ligne;
}
}
Remplir le tableau à partir de la BDD:
public ArrayList<Object[]> recap = new ArrayList<>();
public void raffraichir5() {//remplir le tableau récapitulatif;
int nb = 0;
recap.clear();
Connexion2 cnx = new Connexion2();
cnx.connectdb();
if (cnx.connect != null) {
try {
String sql = "select * FROM change_request ";
Statement stm = cnx.connect.createStatement();
ResultSet rs = stm.executeQuery(sql);
while (rs.next()) {
Object[] cel = new Object[17];
cel[0] = rs.getString(1);
cel[1] = rs.getString(2);
cel[2] = rs.getString(3);
cel[3] = rs.getString(4);
cel[4] = rs.getString(5);
cel[5] = rs.getString(6);
cel[6] = rs.getString(7);
cel[7] = rs.getString(8);
cel[8] = rs.getString(9);
cel[9] = rs.getString(10);
cel[10] = rs.getString(11);
cel[11] = rs.getString(12);
cel[12] = rs.getString(13);
cel[13] = rs.getString(14);
cel[14] = rs.getString(15);
cel[15] = rs.getString(16);
cel[16] = rs.getString(17);
recap.add(cel);
nb++;
}
} catch (Exception e) {
JOptionPane.showConfirmDialog(this, e.getMessage());
}
}
Object[][] ligne = new Object[nb][17];
int j = 0;
for (Object[] cel : recap) {
ligne[j][0] = cel[0];
ligne[j][1] = cel[1];
ligne[j][2] = cel[2];
ligne[j][3] = cel[3];
ligne[j][4] = cel[4];
ligne[j][5] = cel[5];
ligne[j][6] = cel[6];
ligne[j][7] = cel[7];
ligne[j][8] = cel[8];
ligne[j][9] = cel[9];
ligne[j][10] = cel[10];
ligne[j][11] = cel[11];
ligne[j][12] = cel[12];
ligne[j][13] = cel[13];
ligne[j][14] = cel[14];
ligne[j][15] = cel[15];
ligne[j][16] = cel[16];
j++;
}
String[] colonne = {"REFERENCE", "Site", "type de Travaux", "Debut planifié", "Duree", "Début_effectif", "FIN travaux", "RISK_Severity", "RISK", "Impact_Severity", "Impact", "Duree_impact", "Tâches", "Motivations", "Commentaire", "OBSERVATION", "Status"};
tab_recap.setModel(new classTableau(ligne, colonne) {});
this.tab_recap.repaint();
int colIndex = 0;
int colWidth = 100;
TableColumn col = tab_recap.getColumnModel().getColumn(colIndex);
col.setPreferredWidth(colWidth);
TableColumn column = null;
for (int i = 1; i < 15; i++) {
column = tab_recap.getColumnModel().getColumn(i);
column.setPreferredWidth(200);
}
}