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
|
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.text.*;
public class essai extends JPanel {
public String[] columnNames = {"Nom", "Prenom" };
private String[] col0 = { "Beon", "Rolland C", "Lucas" };
private String[] col1 = { "Yves", "Christophe", "Herve" };
private ArrayList[] data;
public essai() {
data = new ArrayList[columnNames.length];
for (int i=0; i<columnNames.length; i++)
data[i] = new ArrayList();
for (int i = 0; i < col0.length; i++) {
data[0].add(col0[i]); // nom
data[1].add(col1[i]); // prenom
}
TableModel principalModel = new MyTableModel(data);
JTable table = new JTable(principalModel);
table.setPreferredScrollableViewportSize(new Dimension(450, 400));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
principalModel.update("Coco", "Lapin");
}
public class MyTableModel extends AbstractTableModel {
ArrayList[] donnees;
ArrayList[] newd;
String nom;
String prenom;
public MyTableModel(ArrayList[] donnees) {
updatetab(donnees);
}
public void updatetab(ArrayList[] newd) {
this.donnees = newd;
fireTableDataChanged();
}
public void update(String nom, String prenom) {
this.nom = nom;
this.prenom = prenom;
donnees[0].add(nom);
donnees[1].add(prenom);
fireTableDataChanged();
}
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 donnees[col].get(row);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
donnees[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);
JComponent newContentPane = new essai();
newContentPane.setOpaque(true); //content panes must be opaque
frame.getContentPane().add(new essai(),
BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
Partager