| 12
 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
 
 | public class Main extends JFrame {
 
	private JTable tableau;
 
	public Main() {
 
		this.setLocationRelativeTo(null);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    this.setTitle("JTable");
	    this.setSize(300, 150);
 
	    Object[][] data = {
	    		{"ligne 1", new Boolean(true)},
	    	    {"ligne 2", new Boolean(false)},
	    	    {"ligne 3", new Boolean(false)},
	    	    {"ligne 4", new Boolean(true)}
	    	};
 
	    	String  title[] = {"Case", "Case CheckBox"};
	    	MyModel model = new MyModel(data, title);
	    	this.tableau = new JTable(model);
 
	    	this.getContentPane().add(new JScrollPane(tableau));
	}
 
	class MyModel extends AbstractTableModel{
 
		private Object[][] data;
	    private String[] title;
 
	    public MyModel(Object[][] data, String[] title) {
 
	    	this.data = data;
	    	this.title = title;
	    }
 
	    public int getColumnCount() {
 
	    	return this.title.length;
	    }
 
	    public int getRowCount() {
 
	    	return this.data.length;
	    }
 
	    public Object getValueAt(int row, int col) {
 
	    	return this.data[row][col];
	    }           
 
	    public String getColumnName(int col) {
 
	    	return this.title[col];
	    }
 
	    public Class getColumnClass(int col){
 
	    	//retourne le type de classe de la cellule
                //et transforme les boolean en checkbox
                //méthode invoquée au moment de la création du rendu des cellules
	    	//on choisi n'importe quelle ligne puisque le type est le même pour chaque ligne
 
	    	//d'où le this.data[0][...]
	    	return this.data[0][col].getClass();
	    }
 
	    public boolean isCellEditable(int row, int col){
 
	    	return true; 
	    }
	}
 
	public static void main(String[] args) {
 
		Main mmain= new Main();
		mmain.setVisible(true);
	}
} | 
Partager