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
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.CellEditorListener;
import java.util.EventObject;
import javax.swing.table.TableCellEditor;
public class Editeur extends javax.swing.AbstractCellEditor implements TableCellEditor {
// le bouton qui est le composant affiche pour l'edition de couleur
// il commence par afficher le dialogue choix de couleur
JButton b = new JButton();
// variables auxiliaires
JTable laTable;
int i,j;
public Editeur() {
// methode repondant au clic sur le bouton
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
b_actionPerformed(e);
}
});
}
// la methode qui renvoie le composant editeur
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected,
int row, int col) {
laTable = table;
i = row; j = col;
return b;
}
// methode appelee quand le bouton est clique
void b_actionPerformed(ActionEvent e) {
int r = laTable.getSelectedRow();
int c = laTable.getSelectedColumn();
if (laTable.getValueAt(r,c) != null && ((String)laTable.getValueAt(r,c)).length() == 0)
{
String reponse;
String message = "";
reponse = JOptionPane.showInputDialog("Entrez un mot", message);
this.fireEditingStopped();
laTable.getModel().setValueAt(reponse,r,c);
}
else
{
String[] choix = {"modifier", "supprimer", "annuler"};
int reponse = JOptionPane.showOptionDialog(null,
"Que voulez-vous faire ?",
"",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choix,
choix[2]);
if (reponse == JOptionPane.YES_OPTION)
{
String rep;
String mes = "";
rep = JOptionPane.showInputDialog("Entrez un mot", mes);
this.fireEditingStopped();
laTable.getModel().setValueAt(rep,r,c);
}
else if (reponse == JOptionPane.NO_OPTION)
{
laTable.getModel().setValueAt("",r,c);
}
else;
}
}
public Object getCellEditorValue() {
return null ;
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean isCellEditable(int rowIndex, int columnIndex){
return columnIndex == 1 || columnIndex == 2 || columnIndex == 3 || columnIndex == 4 || columnIndex == 5 || columnIndex == 6 || columnIndex == 7;
}
/*public boolean shouldSelectCell(EventObject anEvent) {
return true;
}*/
/*public boolean stopCellEditing() {
return true ; // pour ne pas pouvoir recommencer une edition
//return false ; // pour pouvoir recommencer une edition
}*/
/*public void cancelCellEditing() {
}*/
/*public void addCellEditorListener(CellEditorListener l) {
}
public void removeCellEditorListener(CellEditorListener l) {
}*/
} |