I have a project where I have a JTable and :
I use Netbeans IDE, if relevant.
The JTable code :
public class ModelTabelAbonati extends AbstractTableModel {
Abonat[] tabelAbonati = new Abonat[0];
public void Adauga (String nume, String prenume, String cnp, Integer telefon){
tabelAbonati= Arrays.copyOf(tabelAbonati, tabelAbonati.length+1);
tabelAbonati[tabelAbonati.length-1]=new Abonat (nume,prenume, cnp, telefon);
fireTableRowsInserted(tabelAbonati.length-1, tabelAbonati.length-1);
}
public void Adauga(String nume, String prenume, String cnp, int telefon){
tabelAbonati= Arrays.copyOf(tabelAbonati, tabelAbonati.length+1);
tabelAbonati[tabelAbonati.length-1]=new Abonat (nume,prenume, cnp, telefon);
fireTableRowsInserted(tabelAbonati.length-1, tabelAbonati.length-1);
}
@Override
public String getColumnName(int column){
return new String[]{"Nr. ","Nume ","Prenume ","CNP ","Tel. Fix ","Tel. Mobil"}[column];
}
@Override
public int getRowCount() {
return tabelAbonati.length;
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Abonat a= tabelAbonati[rowIndex];
switch (columnIndex){
case 0: return rowIndex+1;
case 1: return a.getNume();
case 2: return a.getPrenume();
case 3: return a.getCnp();
case 4: return a.getTelefon().getTelFix();
case 5: return a.getTelefon().getTelMobil();
default: return "ERROR";
}
}
}
Add this code to ModelTabelAbonati to allow edit cell:
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
Add this table.setRowSelectionAllowed(true)
under table = new Jtable()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.