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
| package exple_jtable;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
/**
*
* @author Administrateur
*/
public class Jtable extends JFrame {
private JTable tableau ;
private JButton ajouter=new JButton("Ajouter une ligne");
private JButton supprimer=new JButton("Supprimer une ligne");
private String[] combodata={"Masculin","Feminin"};
public Jtable()
{
this.setLocationRelativeTo(null);
this.setSize(600, 250);
this.setTitle("Formulaire de renseignement des enfants");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Object[][] data ={{new Boolean(false),"","","","",combodata[0]},{new Boolean(false),"","","","",combodata[0]},{new Boolean(false),"","","","",combodata[0]}};
String title[]= {"OK ?","Nom","Prenom","Date de Naissance","Lieu de Naissance"," Sexe"};
JComboBox combo=new JComboBox(combodata);
LeModel lmd = new LeModel(data,title);
this.tableau=new JTable(lmd);
this.tableau.setRowHeight(30);
// this.tableau.getColumn("Sexe").setCellEditor(new DefaultCellEditor(combo));
DefaultTableCellRenderer drc=new DefaultTableCellRenderer();
// this.tableau.getColumn("Sexe").setCellRenderer(drc);
this.getContentPane().add(new JScrollPane(tableau),BorderLayout.CENTER);
JPanel pan = new JPanel();
ajouter.addActionListener(new MoreListener());
supprimer.addActionListener(new SupLigne());
pan.add(ajouter);
pan.add(supprimer);
this.getContentPane().add(pan,BorderLayout.SOUTH);
}
class LeModel extends AbstractTableModel{
private Object[][]data;
private String[] title;
public LeModel(Object[][] data,String[] title)
{
this.data=data;
this.title=title;
}
public String getColumName(int col)
{
return this.title[col];
}
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) {
//On interdit la modification sur certaine colonne !
if(!this.getColumnName(col).equals("Sexe") )//&& !this.getColumnName(col).equals("Suppression"))
this.data[row][col] = value;
}
/**
* Retourne la classe de la donnée de la colo
* @param col
*/
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("Indice = " + indice);
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();
}
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;
//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();
}
public boolean isCellEditable(int row, int col){
return true;
}
}
class MoreListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
Object[] donnee = new Object[]{new Boolean(false),"", "","","",combodata[0]};
((LeModel)tableau.getModel()).addRow(donnee);
}
}
class SupLigne implements ActionListener{
private int row,column;
private JTable table;
private JButton button;
public void setColumn(int col){this.column = col;}
public void setRow(int row){this.row = row;}
public void setTable(JTable table){this.table = table;}
public void actionPerformed(ActionEvent event) {
if(table.getRowCount() > 0){
((LeModel)table.getModel()).removeRow(this.row);
((AbstractTableModel)table.getModel()).fireTableCellUpdated(this.row, this.column - 1);
}
}
}
public static void main(String[] args){
Jtable fen = new Jtable();
fen.setVisible(true);
}
public class TableComponent extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
//Si la valeur de la cellule est un JCombo, on transtype notre valeur
if (value instanceof JComboBox){
// this.tableau.setDefaultRenderer(JComponent.class, new TableComponent());
return (JComboBox) value;
}
else
return this;
}
}
public class ComboRenderer extends JComboBox implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean isFocus, int row, int col) {
this.addItem("Masculin");
this.addItem("Feminin");
return this;
}
}
} |
Partager