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
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package crudapplication;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
*
* @author Administrateur
*/
public class PersonsModel extends AbstractTableModel {
private static String[] columns = {"FirstName", "LastName"};
private List<Person> persons = new ArrayList<Person>();
public PersonsModel() {
}
public void addPerson(Person person) {
persons.add(person);
int rowIndex = persons.indexOf(person);
this.fireTableRowsInserted(rowIndex, rowIndex);
}
public Person getPerson(int indexRowSelected) {
return persons.get(indexRowSelected);
}
public List<Person> getAll() {
return this.persons;
}
======================================================================
public void removePerson(int indexRowSelected) {
persons.remove(indexRowSelected);
this.fireTableRowsDeleted(indexRowSelected, indexRowSelected);
}
public void removePerson(int[] selectedRows) {
List<Person> toDelete = new ArrayList<Person>(selectedRows.length);
for (int i = 0; i < selectedRows.length; i++) {
toDelete.add(persons.get(i));
}
for (Person person : toDelete) {
int index = persons.indexOf(person);
persons.remove(person);
this.fireTableRowsDeleted(index, index);
}
}
======================================================================
public void refreshPerson(Person person) {
int rowIndex = persons.indexOf(person);
fireTableRowsUpdated(rowIndex, rowIndex);
}
@Override
public String getColumnName(int column) {
try {
return columns[column];
} catch (ArrayIndexOutOfBoundsException e) {
return "N/A";
}
}
public int getRowCount() {
// throw new UnsupportedOperationException("Not supported yet.");
return persons.size();
}
public int getColumnCount() {
// throw new UnsupportedOperationException("Not supported yet.");
return 2;
}
public Object getValueAt(int rowIndex, int columnIndex) {
//throw new UnsupportedOperationException("Not supported yet.");
Object valeur;
Person person = persons.get(rowIndex);
switch (columnIndex) {
case 0:
valeur = person.getFirstName();
break;
case 1:
valeur = person.getLastName();
break;
default:
valeur = null;
}
return valeur;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Person person = this.persons.get(rowIndex);
switch (columnIndex) {
case 0:
person.setFirstName(aValue.toString());
break;
case 1:
person.setLastName(aValue.toString());
break;
}
}
} |
Partager