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
|
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
public class ComboBoxEditor extends DefaultCellEditor{
private boolean isPushed;
private ComboBoxListener bListener = new ComboBoxListener();
private Magasin magasin;
private CalendarSpecialPrint date;
private Map<CalendarSpecialPrint, ArrayList<JComboBox>> list;
private int ligne;
public ComboBoxEditor(JCheckBox check, Magasin magasin, CalendarSpecialPrint date, Map<CalendarSpecialPrint, ArrayList<JComboBox>> l) {
//Par défaut, ce type d'objet travaille avec un JCheckBox
super(check);
this.magasin = magasin;
this.date = date;
list = l;
Iterator keys = list.keySet().iterator();
CalendarSpecialPrint key;
while(keys.hasNext()){
key = (CalendarSpecialPrint) keys.next();
for(int i=0; i<list.get(key).size(); i++){
list.get(key).get(i).addActionListener(bListener);
}
}
}
@Override
public Object getCellEditorValue(){
Iterator keys = list.keySet().iterator();
CalendarSpecialPrint key;
while(keys.hasNext()){
key = (CalendarSpecialPrint) keys.next();
if(key.equals(date)){
return list.get(key).get(ligne);
}
}
return "";
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
//On précise le numéro de ligne à notre listener
bListener.setRow(row);
//Idem pour le numéro de colonne
bListener.setColumn(column);
//On passe aussi le tableau en paramètre pour des actions potentielles
bListener.setTable(table);
//On renvoie le bouton
ligne = row;
return (Component) value;
}
//Notre listener pour le comboBox
class ComboBoxListener implements ActionListener{
private int column, row;
private JTable table;
public void setColumn(int col){this.column = col;}
public void setRow(int row){this.row = row;}
public void setTable(JTable table){this.table = table;}
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("YES");
int ind = magasin.getClients().indexOf((Client)table.getValueAt(row, 1));
if(ind != -1){
for(int i=0; i<magasin.getAchats().get(ind).getDates().size(); i++){
if(magasin.getAchats().get(ind).getDates().get(i).equals(date) && magasin.getAchats().get(ind).getProduits().get(i).equals((Produit)((JComboBox)(table.getValueAt(row, 2))).getSelectedItem())){
table.setValueAt(magasin.getAchats().get(ind).getProduits().get(i).getPrix(), row, 3);
table.setValueAt(magasin.getAchats().get(ind).getQuantite().get(i), row, 4);
table.setValueAt(magasin.getAchats().get(ind).getVersement().get(i), row, 5);
table.setValueAt(magasin.getAchats().get(ind).getReste().get(i), row, 6);
table.setValueAt(magasin.getAchats().get(ind).getTotaux().get(i), row, 7);
table.revalidate();
table.repaint();
break;
}
}
}
}
}
} |
Partager