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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| public class ClasseJtable extends JFrame {
private JTable tableau;
private JButton ajouter,supprimer;
// private JPanel pan;
private String[] combodata={"","Masculin","Feminin"};
public ClasseJtable()
{
this.setSize(600, 250);
this.setLocationRelativeTo(null);
this.setTitle("liste des enfants");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Object[][] data={{"","","","",combodata[0]},{"","","","",combodata[0]},{"","","","",combodata[0]}};
String[] title={"Nom","Prenom","Date de Naissance","Lieu de Naissance","Sexe"};
JComboBox combo=new JComboBox(combodata);
MonModel mnmodel=new MonModel(data,title);
DefaultTableCellRenderer drc=new DefaultTableCellRenderer();
this.tableau=new JTable(mnmodel);
this.tableau.getColumn("Sexe").setCellRenderer(drc);
this.tableau.getColumn("Sexe").setCellEditor(new DefaultCellEditor(combo));
this.tableau.getColumn("Nom").setCellRenderer(new BoldCellRenderer());
this.tableau.getColumn("Date de Naissance").setCellRenderer(new DateCellRenderer() );
this.tableau.setRowHeight(25);
ajouter= new JButton("Ajouter une ligne");
supprimer=new JButton("Supprimer une ligne");
JPanel pan=new JPanel();
pan.add( ajouter);
pan.add(supprimer);
this.getContentPane().add(pan,BorderLayout.SOUTH);
ajouter.addActionListener(new MoreListener());
supprimer.addActionListener(new DelListener());
this.getContentPane().add(new JScrollPane(tableau),BorderLayout.CENTER);
}
class MonModel extends AbstractTableModel
{
private Object[][] data;
private String[] title;
public MonModel(Object[][] data, String[] title)
{
this.data = data;
this.title = title;
}
@Override
public String getColumnName(int col) {
return this.title[col];
}
/**
* Retourne le nombre de colonnes
*/
public int getColumnCount() {
return this.title.length;
}
/**
* Retourne le nombre de lignes
*/
public int getRowCount() {
return this.data.length;
}
/**
* Retourne la valeur à l'emplacement spécifié
*/
public Object getValueAt(int row, int col) {
return this.data[row][col];
}
/**
* Défini la valeur à l'emplacement spécifié
*/
@Override
public void setValueAt(Object value, int row, int col) {
this.data[row][col] = value;
}
@Override
public Class getColumnClass(int col){
//On retourne le type de la cellule à la colonne demandée
//On se moque de la ligne puisque les données sur chaque ligne sont les mêmes
//On choisit donc la première ligne
return this.data[0][col].getClass();
}
public void removeRow(int position){
int indice =0, indice2 = 0, nbRow = this.getRowCount()-1, nbCol = this.getColumnCount();
Object temp[][] = new Object[nbRow][nbCol];
for(Object[] value : this.data){
if((indice != position)){
temp[indice2++] = value;
System.out.println("Quand indice different de position,l'indice est " + indice+"\n la position est:"+position);
}
System.out.println("Quand indice=position Indice = " + indice+"\n position :"+position);
indice++;
}
this.data = temp;
temp = null;
//Cette méthode permet d'avertir le tableau que les données ont été modifiées
//Ce qui permet une mise à jours complète du tableau
this.fireTableDataChanged();
}
@Override
public boolean isCellEditable(int row, int col){
return true;
}
public void addRow(Object[] data){
int indice = 0, nbRow = this.getRowCount(), nbCol = this.getColumnCount();
Object temp[][] = this.data;
this.data = new Object[nbRow+1][nbCol];
for(Object[] value : temp)
this.data[indice++] = value;
this.data[indice] = data;
temp = null;
this.fireTableDataChanged();
}
}
class MoreListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
Object[] donnee = new Object[]{"", "","","",combodata[0]};
((MonModel)tableau.getModel()).addRow(donnee);
}
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
ClasseJtable fen = new ClasseJtable();
fen.setVisible(true);
}
private void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new JPanel();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
});
}
class DelListener implements ActionListener {
private int row,column;
private JTable table;
public void setRow(int row)
{this.row = row;}
public void setTable(JTable table)
{this.table = table;}
public void actionPerformed(ActionEvent event) {
if(tableau.getModel().getRowCount() > 0){
((MonModel)tableau.getModel()).removeRow(this.row);
}
}
}
/* Classe implémentant le rendu du JSpiner*/
public class DateCellRenderer extends JPanel implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Calendar calendar = Calendar.getInstance();
JFormattedTextField ftf = null;
JSpinner spinner=new JSpinner();
ftf =getTextField(spinner);
if (ftf != null ) {
ftf.setColumns(8); //specify more width than we need
ftf.setHorizontalAlignment(JTextField.RIGHT);
// Ajout du libellé "date de naissance"
//Add the third label-spinner pair.
Date initDate = calendar.getTime();
calendar.add(Calendar.YEAR, -100);
Date earliestDate = calendar.getTime();
calendar.add(Calendar.YEAR, 200);
Date latestDate = calendar.getTime();
SpinnerModel dateModel = new SpinnerDateModel(initDate,
earliestDate,
latestDate,
Calendar.YEAR);//ignored for user input
spinner = addLabeledSpinner(this, dateModel);
spinner.setEditor(new JSpinner.DateEditor(spinner, "dd/MM/yyyy"));
}
return this;
}
//}
public JFormattedTextField getTextField(JSpinner spinner) {
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor)editor).getTextField();
} else {
System.err.println("Type d'éditeur inattendu: "
+ spinner.getEditor().getClass()
+ " n'est pas un descendant de DefaultEditor");
return null;
}
}
protected JSpinner addLabeledSpinner(Container c,SpinnerModel model) {
JSpinner spinner = new JSpinner(model);
c.add(spinner);
return spinner;
}
}
//classe permettant de boucler selon le jour,mois ou l'année
public class CyclingSpinnerListModel extends SpinnerListModel {
Object firstValue, lastValue;
SpinnerModel linkedModel = null;
public CyclingSpinnerListModel(Object[] values) {
super(values);
firstValue = values[0];
lastValue = values[values.length - 1];
}
public void setLinkedModel(SpinnerModel linkedModel) {
this.linkedModel = linkedModel;
}
@Override
public Object getNextValue() {
Object value = super.getNextValue();
if (value == null) {
value = firstValue;
if (linkedModel != null) {
linkedModel.setValue(linkedModel.getNextValue());
}
}
return value;
}
@Override
public Object getPreviousValue() {
Object value = super.getPreviousValue();
if (value == null) {
value = lastValue;
if (linkedModel != null) {
linkedModel.setValue(linkedModel.getPreviousValue());
}
}
return value;
}
}
} |
Partager