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 121 122
| public class Classtest extends javax.swing.JFrame {
private Controle com=null;
public JCheckBox check=null;
public JCheckBox choix=new JCheckBox() ;
public JButton messag=null;
/** Creates new form Classtest */
public Classtest() {
com=new Controle(this);
forme();
}
public void forme(){
// check.addActionListener(com);
// messag.addActionListener(com);
table.setModel(new tablmodel());
table.setDefaultRenderer(Object.class,new tablerenderer());
table.setDefaultEditor(String.class,new tabeditor(choix));
}
//mon modele
public class tablmodel extends AbstractTableModel {
int col=2;
String[] tab = {"toto","titi"};
String[] tan = {"pat","pepe"};
public int getRowCount() {
return col;
}
public int getColumnCount() {
return col;
}
public Object getValueAt(int rowIndex, int columnIndex) {
Object val=null;
if(rowIndex==0 && columnIndex==0) {
val=tab[rowIndex];
}
if(rowIndex==1 && columnIndex==0) {
val=tab[rowIndex];
}
if(rowIndex==0 && columnIndex==1) {
val=tan[rowIndex];
}
if(rowIndex==1 && columnIndex==0) {
val=tan[rowIndex];
}
return val;
}
}
//mon TableCellRenderer:
public class tablerenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(column==0){
value=table.getValueAt(row, column);
check=new JCheckBox((String)value);
if(isSelected){
check.addActionListener(com);
System.out.println("action saisis");
}
return check;
}
if(column==1){
value=table.getValueAt(row, column);
messag=new JButton((String)value);
if(isSelected){
messag.addActionListener(com);
System.out.println("action saisis buton");
}
return messag;
}
return new JTextField();
}
}
//mon DefaultCellEditor:
public class tabeditor extends DefaultCellEditor{
public tabeditor(JCheckBox c){
super(c);
check=c;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
//return super.getTableCellEditorComponent(table, value, isSelected, row, column);
if(column==0){
return check;
}
if(column==1){
return messag;
}
else
return new JTextField();
}
}
}
//ma classe de controle d'execution d'action correspondante:
public class Controle implements ActionListener{
private Classtest Test=null;
public Controle(Classtest test){
Test=test;
}
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if(obj.equals(Test.check)){
//action à realiser
}
if(obj.equals(Test.messag)){
action à realiser
}
}
} |
Partager