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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
public class Demo extends JPanel {
private String[] col0 = { "Beon", "Rolland", "Lucas" };
private String[] col1 = { "Yves", "Christophe", "Herve" };
private Boolean[][] coln = { { true, false, true },
{ true, true, true },
{ true, true, false },
{ true, true, true } };
public Demo() {
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 400));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
JTable tablecr = new JTable(new MyTableModelRep());
tablecr.setPreferredScrollableViewportSize(new Dimension(250, 400));
JScrollPane scrollPane2 = new JScrollPane(tablecr);
add(scrollPane2);
}
// 2è modèle
class MyTableModelRep extends AbstractTableModel {
private String[] columnNames2 = {"Nom", "Prenom", "Date"};
private ArrayList[] data2;
public MyTableModelRep() {
data2 = new ArrayList[columnNames2.length];
for (int i=0; i<columnNames2.length; i++)
data2[i] = new ArrayList();
init2();
}
public void init2() {
for (int i = 0; i < col0.length; i++) {
if (MyTableModel.getValueAt(i,2) == true) {
//code qui fait qqchose
}
}
}
public int getColumnCount() {
return columnNames2.length;
}
public int getRowCount() {
return data2[0].size();
}
public String getColumnName(int col) {
return columnNames2[col];
}
public Object getValueAt(int row, int col) {
return data2[col].get(row);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data2[col].set(row, value);
fireTableCellUpdated(row, col);
}
}
// modèle principal
public class MyTableModel extends AbstractTableModel {
public String[] columnNames = {"Nom", "Prenom",
"Fait Croissant", "Fait Apero", "Fait Plat", "Fait dessert"};
private ArrayList[] data;
public MyTableModel() {
data = new ArrayList[columnNames.length];
for (int i=0; i<columnNames.length; i++)
data[i] = new ArrayList();
init();
}
public void init() {
for (int i = 0; i < col0.length; i++) {
data[0].add(col0[i]);
data[1].add(col1[i]);
data[2].add(coln[0][i]); // [0]=croissant
data[3].add(coln[1][i]);
data[4].add(coln[2][i]);
data[5].add(coln[3][i]);
}
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data[0].size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[col].get(row);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data[col].set(row, value);
fireTableCellUpdated(row, col);
}
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Demo newContentPane = new Demo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
Partager