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
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class MaTable extends AbstractTableModel
{
private final List<Ami>amis = new ArrayList<Ami>();
private final String[] entetes = {"Colonne1","Colonne2","Colonne3","Colonne4"};
public MaTable()
{
super();
amis.add(new Ami("C1","C2",Color.red,true));
amis.add(new Ami("C11","C21",Color.black,false));
amis.add(new Ami("C12","C22",Color.gray,true));
amis.add(new Ami("C13","C23",Color.green,false));
}
// Necessite de creer les 3 methodes suivantes qd herite de AbstractTableModel sinon en erreur....
public int getRowCount()
{
return amis.size();
}
public int getColumnCount()
{
return entetes.length;
}
public Object getValueAt (int rowIndex,int columnIndex)
{
switch (columnIndex){
case 0:return amis.get(rowIndex).getPrenom();
case 1:return amis.get(rowIndex).getNom();
case 2:return amis.get(rowIndex).getColor();
case 3:return amis.get(rowIndex).isHomme();
default : return null;
}
}
public String getColumnName (int columnIndex)
{
return entetes[columnIndex];
}
public void addAmi (Ami ami)
{
amis.add(ami);
fireTableRowsInserted(amis.size()-1,amis.size()-1);
}
public class Ami
{
private String nom;
private String prenom;
private Color couleur;
private boolean homme;
public Ami (String nom,String prenom,Color couleur,boolean homme)
{
super();
this.nom = nom;
this.prenom =prenom;
this.couleur = couleur;
this.homme=homme;
}
public String getNom()
{
return nom;
}
public void setNom(String nom)
{
this.nom=nom;
}
public String getPrenom()
{
return prenom;
}
public void setPrenom(String prenom)
{
this.prenom=prenom;
}
public Color getColor()
{
return couleur;
}
public void setColor (Color couleur)
{
this.couleur = couleur;
}
public boolean isHomme()
{
return homme;
}
public void setHomme(boolean homme)
{
this.homme = homme;
}
}
public static class JTableBasiqueAvecModeleDynamique extends JFrame // affichage de notre JTable dans un JSCrollPan
{
private MaTable modele = new MaTable();
private JTable tableau;
public JTableBasiqueAvecModeleDynamique()
{
super();
setTitle("JTABLE Modele Dynamique");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tableau = new JTable(modele);
getContentPane().add(new JScrollPane(tableau),BorderLayout.CENTER);
JPanel boutons = new JPanel();
boutons.add(new JButton("ajouter"));
boutons.add(new JButton("supprimer"));
getContentPane().add(boutons,BorderLayout.SOUTH);
modele.addAmi(new Ami("C1","C2",Color.red,true));// Mon problème se situe ici!!!
pack();
}
}
public static void main (String[] args)
{
new JTableBasiqueAvecModeleDynamique().setVisible(true);
}
} |
Partager