Probleme de suppression dans une JTable
Bonjour,
J'ai réalisé une application CRUD ,tout fonctionne sauf la suprression en effet si je selectionne plusieurs lignes que je fais un clic droit delete cela fonctionne, si je selectionne une ligne à l'indice 4 par exemple c'est la ligne à l'indice 2 qui est supprimée
Je voudrais pouvoir effacer la ligne que j'ai selectionné n'importe où sur mon tableau...
j'ai un tableau de deux colonnes et des lignes dynamiques........
Merci de l'attention que vous porterez à mon pb si vous savez ce qui cloche faites moi signe merci encore
voici le code du Delete et ma méthode removePerson
Code:
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
| ============================================================*==========
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package crudapplication.actions;
import crudapplication.PersonsModel;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
/**
*
* @author FRWD9529
*/
public class DelAction extends AbstractAction {
private int[] selectedRows = new int[0];
private PersonsModel model = null;
public DelAction(PersonsModel model) {
super("Delete
");
this.model = model;
}
public void setSelectedRows(int[] selectedRows) {
this.selectedRows = selectedRows;
setEnabled((this.selectedRows.length > 0));
}
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "Voulez-vous vraiment confirmer votre choix ?",
"Confirmation de la suppression", JOptionPane.YES_NO_OPTION);
switch (result) {
case JOptionPane.YES_OPTION:
model.removePerson(selectedRows);
break;
case JOptionPane.NO_OPTION:
break;
}
}
} |
======================================================================
et le code ou se trouve la méthode removePerson...
Code:
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;
}
}
} |