Précédent   Forum du club des développeurs et IT Pro > Java > Interfaces Graphiques en Java > AWT/SWING > Composants > Tables
Tables JTable, JXTable, ...
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 19/01/2013, 00h23   #1
sylvainkouo
Invité de passage
 
Homme
Étudiant
Inscription : novembre 2012
Messages : 7
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Cameroun

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : novembre 2012
Messages : 7
Points : 3
Points : 3
Par défaut Constuire un JTable en utilisant une ArrayList

bonjour a tous! j'ai l'habitude de construire ma table JTable en utilisant un tableau d’objet mais je ne sais comment utiliser une ArrayList.
merci d’avance
sylvainkouo est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/01/2013, 11h50   #2
Robin56
Modérateur
 
Avatar de Robin56
 
Homme Nicolas
Ingénieur développement logiciels
Inscription : juin 2009
Messages : 2 766
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels

Informations forums :
Inscription : juin 2009
Messages : 2 766
Points : 7 490
Points : 7 490
Ce tutoriel te guidera sur les subtitlités du JTable : http://baptiste-wicht.developpez.com.../swing/jtable/
__________________
Robin56 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 20/01/2013, 14h02   #3
peofofo
Membre habitué
 
Inscription : mai 2008
Messages : 336
Détails du profil
Informations personnelles :
Âge : 28
Localisation : France, Loire (Rhône Alpes)

Informations forums :
Inscription : mai 2008
Messages : 336
Points : 124
Points : 124
Tu peux créer un model de ta table:

Voici comment je procède:


Code :
1
2
3
4
public abstract class Tableau extends AbstractTableModel{
	public abstract void setValueAt(Object value, int indice, int row, int col);
	public abstract Object getValueAt(int row, int col, int indice);
}

Code :
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
public class Tableau_membres_model extends Tableau {
	static Logger logger = Logger.getLogger("Tableau_membres_model.class");
	private ArrayList<ArrayList<String>>  list_colonnes = new ArrayList<ArrayList<String>>();
	private ArrayList<ArrayList<ArrayList<Object>>>  list_lignes = new ArrayList<ArrayList<ArrayList<Object>>>();
	private DAO<Membres> membresDao = new MembresDao();
	private ArrayList<Membres> membres = new ArrayList<Membres>();
	private Datetime date;
 
	public Tableau_membres_model(){
		initTableau();
	}
 
	private void initTableau() {
		list_colonnes.add(0, new ArrayList<String>());
		list_colonnes.get(0).add(0, "Nb");
		list_colonnes.add(1, new ArrayList<String>());
		list_colonnes.get(1).add(0, "Nom");
		list_colonnes.add(2, new ArrayList<String>());
		list_colonnes.get(2).add(0, "Prenom");
		list_colonnes.add(3, new ArrayList<String>());
		list_colonnes.get(3).add(0, "Mot de passe");
		list_colonnes.add(4, new ArrayList<String>());
		list_colonnes.get(4).add(0, "Solde");
		list_colonnes.add(5, new ArrayList<String>());
		list_colonnes.get(5).add(0, "Affichage");
		list_colonnes.add(6, new ArrayList<String>());
		list_colonnes.get(6).add(0, "Forfait");
		list_colonnes.add(7, new ArrayList<String>());
		list_colonnes.get(7).add(0, "Adminitration");
		list_colonnes.add(8, new ArrayList<String>());
		list_colonnes.get(8).add(0, "Suppression");
		addrow();
	}
 
	public void addrow() {
		// TODO Auto-generated method stub
		list_lignes.clear();// On efface les lignes
		//On met a jour les lignes:
		membres.clear();
		membres=membresDao.select();
		for(int i = 0; i < membres.size(); i++){
			// On affiche la date sous le bon format
			String affichage, status, forfait;
			if(membres.get(i).getAfficher()==0){
				affichage = "NON";
			}
			else{
				affichage = "OUI";	
			}
			if(membres.get(i).getForfait()==0){
				forfait = "NON";
			}
			else{
				forfait = "OUI";	
			}
			if(membres.get(i).getAdmin()==0){
				status="Membre";
			}
			else if(membres.get(i).getAdmin()==1){
				status="Gestionnaire";	
			}
			else{
				status="Administrateur";
			}
 
			// On met les infos dans le tableau
			list_lignes.add(i, new ArrayList<ArrayList<Object>>());
			list_lignes.get(i).add(0, new ArrayList<Object>());
			list_lignes.get(i).get(0).add(0, i+1);
			list_lignes.get(i).get(0).add(1, membres.get(i).getId());
			list_lignes.get(i).add(1, new ArrayList<Object>());
			list_lignes.get(i).get(1).add(0, membres.get(i).getNom());
			list_lignes.get(i).add(2, new ArrayList<Object>());
			list_lignes.get(i).get(2).add(0, membres.get(i).getPrenom());
			list_lignes.get(i).add(3, new ArrayList<Object>());
			list_lignes.get(i).get(3).add(0, membres.get(i).getPasse());
			list_lignes.get(i).add(4, new ArrayList<Object>());
			list_lignes.get(i).get(4).add(0, Chiffre.convers_string(membres.get(i).getSolde())+"€");
			list_lignes.get(i).get(4).add(1, membres.get(i).getSolde());
			list_lignes.get(i).add(5, new ArrayList<Object>());
			list_lignes.get(i).get(5).add(0, affichage);
			list_lignes.get(i).get(5).add(1, membres.get(i).getAfficher());
			list_lignes.get(i).add(6, new ArrayList<Object>());
			list_lignes.get(i).get(6).add(0, forfait);
			list_lignes.get(i).get(6).add(1, membres.get(i).getForfait());
			list_lignes.get(i).add(7, new ArrayList<Object>());
			list_lignes.get(i).get(7).add(0, status);
			list_lignes.get(i).get(7).add(1, membres.get(i).getAdmin());
			list_lignes.get(i).add(8, new ArrayList<Object>());
			list_lignes.get(i).get(8).add(0, "Supprimer");
		}
		this.fireTableDataChanged();
	}
 
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return list_colonnes.size();
	}
 
	public int getRowCount() {
		// TODO Auto-generated method stub
		return list_lignes.size();
	}
 
	public String getColumnName(int col) {
		// TODO Auto-generated method stub
		return list_colonnes.get(col).get(0);
	}
 
	public Object getValueAt(int row, int col) {
		return list_lignes.get(row).get(col).get(0);
	}
 
	public Object getValueAt(int row, int col, int indice) {
		return list_lignes.get(row).get(col).get(indice);
	}
 
	public boolean isCellEditable(int row, int col){
		if((col>=1 && col<4) || (col>=5 && col<=8)){
			return true;
		}
		else{
			return false; 
		}
	}
 
	public void setValueAt(Object value, int row, int col) {
		if(col==1 || col==2){
			//On met a jour dans la bdd
			int id=(Integer)list_lignes.get(row).get(0).get(1);
			if(col==1){
				String nom = value.toString().trim().toUpperCase();
				if(!nom.isEmpty()){
					membresDao.update("nom", nom, id);
					logger.info("Mis a jour du prénom du membre: "+value);
					list_lignes.get(row).get(1).add(0, nom);
				}
				else{
					JOptionPane.showMessageDialog(null, "Le nom ne peut pas être nul", "Erreur", JOptionPane.ERROR_MESSAGE);	
		        	logger.error("Le nom ne peut pas être nul");
				}
			}
			else if(col==2){
				String prenom = value.toString().trim().replaceFirst(".",(value.toString().charAt(0)+"").toUpperCase());
				if(!prenom.isEmpty()){
					membresDao.update("prenom", prenom, id);
					logger.info("Mis a jour du nom du membre: "+prenom);
					list_lignes.get(row).get(2).add(0, prenom);
				}
				else{
					JOptionPane.showMessageDialog(null, "Le prénom ne peut pas être nul", "Erreur", JOptionPane.ERROR_MESSAGE);	
		        	logger.error("Le prénom ne peut pas être nul");
				}
			}
			fireTableCellUpdated(row, col);
		}
	}
 
	public Class<?> getColumnClass(int c) {
		Object o =  getValueAt(0, c);
		if(c==1 || c==2){
        	return String.class;
        }
		else if(c==3){
        	return Integer.class;
        }
        else{
        	return o.getClass();
        }
    }
 
	public void setValueAt(Object value, int indice, int row, int col) {
		// TODO Auto-generated method stub
		list_lignes.get(row).get(col).add(indice, value);
		fireTableCellUpdated(row, col);
	}
}
Ce n'est qu'un exemple:

Tous dépend ce que tu veux faire avec!
peofofo est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 20h15.


 
 
 
 
Partenaires

Hébergement Web