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
|
public class Test extends javax.swing.JFrame {
private JScrollPane jscrollp;
private JTable table;
private JPanel jp;
private int CountRowsInit;
// le model
DefaultTableModel model = new DefaultTableModel() {
public boolean isCellEditable(int row, int col) { // OK pour abstract
fireTableCellUpdated(row, col);
fireTableDataChanged();
if (row < CountRowsInit)
return (col < (table.getColumnCount() - 1)) ? false : true;
return true;
}
};
Object[][] cellData = {
{ "1-1", "1-2", "1-3", "1-4", "1-5","1-6" },
{ "2-1", "1-2", "1-3", "1-4", "1-5","1-6" },
{ "3-1", "1-2", "1-3", "1-4", "1-5","1-6" },
{ "4-1", "1-2", "1-3", "1-4", "1-5","1-6" },
{ "5-1", "1-2", "1-3", "1-4", "1-5","1-6" },
{ "6-1", "1-2", "1-3", "1-4", "1-5","1-6" }};
String[] columnNames = { "col1", "col2", "col3", "col4", "col5", "col6" };
public Test() {
super("Table");
// Create a table with initial data
Vector<Object> rowData = new Vector<Object>();
initDataInTab(rowData, cellData);
Vector<String> columnNamesV = new Vector<String>(Arrays
.asList(columnNames));
table = new JTable(model);
model.setDataVector(rowData, columnNamesV);
// pour le tri !!!
table.setAutoCreateRowSorter(true);
CountRowsInit = cellData.length;
jscrollp = new JScrollPane(table);
jp = new JPanel();
// Create an action
Action actionAdd = new AbstractAction("addCopy") {
private static final long serialVersionUID = 1L;
// This method is called when the button is pressed
public void actionPerformed(ActionEvent evt) {
if (table.getRowCount() > 0) {
/* Pour ajouter une copy ligne avec des données connus */
model.insertRow(0, copyRowAt(table.getRowCount() - 1));
printDebugData(table);
} else {
model.addRow(emptyRow());
printDebugData(table);
}
}
};
Action actionDel = new AbstractAction("Del") {
private static final long serialVersionUID = 1L;
// This method is called when the button is pressed
public void actionPerformed(ActionEvent evt) {
if (table.getRowCount() != 0) {
model.removeRow(table.getRowCount() - 1);
printDebugData(table);
}
}
};
Action actionEmp = new AbstractAction("Emp") {
private static final long serialVersionUID = 1L;
// This method is called when the button is pressed
public void actionPerformed(ActionEvent evt) {
model.insertRow(table.getRowCount(), emptyRow());
printDebugData(table);
}
};
// Create the button
JButton buttonAdd = new JButton(actionAdd);
JButton buttonDel = new JButton(actionDel);
JButton buttonEmp = new JButton(actionEmp);
jp.add(buttonAdd);
jp.add(buttonDel);
jp.add(buttonEmp);
jp.setOpaque(true);
jp.add(jscrollp, BorderLayout.CENTER);
getContentPane().add(jp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setBounds(100, 100, 500, 500);
}
/* transforme le tableau cellData en un tableau de vecteur -d'objet rowData- */
private void initDataInTab(Vector<Object> rowData, Object[][] cellData) {
for (int i = 0; i < cellData.length; i++) {
Vector<Object> colData = new Vector<Object>(Arrays
.asList(cellData[i]));
rowData.add(colData);
}
}
private Vector<Object> copyRowAt(int row) {
Vector<Object> data = model.getDataVector();
Vector<Object> copy = (Vector<Object>) data.elementAt(row);
copy = (Vector<Object>) copy.clone();
return copy;
}
private Vector<Object> emptyRow() {
Vector<Object> ligneVide = new Vector<Object>();
for (int i = 0; i < table.getRowCount(); i++)
ligneVide.add(" ");
return ligneVide;
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
Test inst = new Test();
inst.setVisible(true);
}
} |
Partager