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
| public class TableEventComponentFlux extends DefaultCellEditor {
/**
*
*/
private static final long serialVersionUID = 1L;
protected JCheckBox checx;
private checkBoxListener bListener = new checkBoxListener();
/**
* Constructeur avec une checkBox
* @param checkBox
* @param count
*/
public TableEventComponentFlux(JCheckBox checkBox) {
//Par défaut, ce type d'objet travaille avec un JCheckBox
super(checkBox);
//On crée à nouveau notre bouton
checx = new JCheckBox();
checx.setOpaque(true);
//On lui attribue un listener
checx.addActionListener(bListener);
checx.setHorizontalAlignment(SwingConstants.CENTER);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
//On définit 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 pour des actions potentielles
bListener.setTable(table);
return checx;
}
/**
* Notre listener pour le bouton
* @author CHerby
*
*/
public static class checkBoxListener implements ActionListener{
private static JTable table;
private int row;
private int column;
private JCheckBox checx;
public void setColumn(int col){this.column = col;}
public void setRow(int row){this.row = row;}
public void setTable(JTable table){checkBoxListener.table = table;}
public JCheckBox getChexc(){return this.checx;}
public void actionPerformed(ActionEvent event) {
if(((JCheckBox) event.getSource()).isSelected())
System.out.println("chexc cocher ligne "+row+ " column "+column);
else
System.out.println("chexc décocher ligne "+row+ " column "+column);
}
}
} |
Partager