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
| public class MyFrame extends JFrame{
JTable table = new JTable(new MyModel());
JButton button = new JButton("Ajouter une ligne");
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
public MyFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane,BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.NORTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
((MyModel)table.getModel()).addRow();
}});
pack();
show();
}
class MyModel extends AbstractTableModel{
private Vector data;
private String[] colNames = new String[]{"un","deux","trois"};
private Class[] colClass = new Class[]{Boolean.class,Boolean.class,Boolean.class};
public MyModel(){
super();
data = new Vector();
data.add(new Object[] {new Boolean(true), new Boolean(true),new Boolean(true)});
data.add(new Object[] {new Boolean(false), new Boolean(false),new Boolean(false)});
}
public int getColumnCount() {
return colNames.length;
}
public int getRowCount() {
return data.size();
}
public Object getValueAt(int row, int col) {
Object[] obj = (Object[]) data.get(row);
return obj[col];
}
public void setValueAt(Object val, int row, int col) {
Object[] obj = (Object[]) data.get(row);
obj[col] = val;
}
public boolean isCellEditable(int arg0, int arg1) {
return true;
}
public Class getColumnClass(int arg0) {
return colClass[arg0];
}
/* Pour ajouter une ligne au Model */
public void addRow(){
data.add(new Object[] {new Boolean(true), new Boolean(true),new Boolean(true)});
/* Pour que le changement dans les donnees soit pris en compte */
fireTableDataChanged();
}
}//:-
}///:- |
Partager