Jtable celleditable ne marche pas ?!!

Fermé
nesro04 - 14 nov. 2012 à 20:15
KX Messages postés 16741 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 30 mai 2024 - 15 nov. 2012 à 00:41
Bonjour,
j'ai besoin d'aide sur un code où ma classe remplit un jtable à partir d'une arraylist et l'affiche.

le prb pour moi c'est de pouvoir modifier les cellules. j'ai configurer la methode iscelleditable mais ça ne marche pas, en faite je peux ecrire dans la cellule mais des que je quitte la cellule les changements que j'ai apporter à la cellule ne sont pas pri en charge.

voici le code de ma classe:



package carnet;
import java.awt.*;
//import javax.swing.table;
import javax.swing.*;
import javax.swing.table.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.*;
import javax.swing.ListSelectionModel;



class ModeleDynamiqueObjet extends AbstractTableModel {

public static ArrayList<Prof> list_prof;


private final String[] entetes = {"Nom", "Prenom", "@ Mail", "Tel Personnel", "Compagny", "Tel Professionnel"};

public ModeleDynamiqueObjet(final ArrayList<Prof> list_prof) {
super();
this.list_prof = list_prof;
}

public boolean isCellEditable(int row, int col) {
return true;
}
public int getRowCount() {
return list_prof.size();
}

public int getColumnCount() {
return entetes.length;
}

public String getColumnName(int columnIndex) {
return entetes[columnIndex];
}

public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex){

case 0:
return list_prof.get(rowIndex).nom;
case 1:
return list_prof.get(rowIndex).prenom;
case 2:
return list_prof.get(rowIndex).mail;
case 3:
return list_prof.get(rowIndex).tel;
case 4:
return list_prof.get(rowIndex).compagny;
case 5:
return list_prof.get(rowIndex).tel_comp;

default:
return null;
}
}

public void removeAmi(int rowIndex) {

list_prof.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);

try{
Prof.removecontact(list_prof,"D:\\course\\tpjava\\Application\\carnet\\contact_prof.txt");
} catch (IOException exception) {System.out.println ("exception : " + exception);}

}
}



public class affich_prof extends JFrame {

private ModeleDynamiqueObjet modele;
public static ArrayList<Prof> list_prof;
public static JTable tableauf;
public JFrame frame;
public static JPanel boutons;

public affich_prof( JFrame frame, final ArrayList<Prof> list_prof) {
super();
this.frame=frame;

this.list_prof= list_prof;
modele = new ModeleDynamiqueObjet(list_prof);
frame.setTitle("Gestion Contact :: Affichage des Contacts Professionnels");


tableauf = new JTable(modele);
tableauf.setAutoCreateRowSorter(true);


frame.getContentPane().add(new JScrollPane(tableauf), BorderLayout.CENTER);


JPanel boutons = new JPanel();

boutons.add(new JButton(new RemoveAction()));
boutons.add(new JButton(new UpdateAction()));

frame.getContentPane().add(boutons, BorderLayout.SOUTH);

pack();


frame.setVisible(true);
}


private class RemoveAction extends AbstractAction {
private RemoveAction() {
super("Supprimmer");
}

public void actionPerformed(ActionEvent e) {
int[] selection = tableauf.getSelectedRows();

for(int i = selection.length - 1; i >= 0; i--){
modele.removeAmi(selection[i]);
}
}
}

private class UpdateAction extends AbstractAction {
private UpdateAction() {
super("Modifier");
}

public void actionPerformed(ActionEvent e) {

String s= (String)modele.getValueAt(0, 0);
System.out.println(" val de (0,0) est "+ s);


}
}
}

7 réponses

KX Messages postés 16741 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 30 mai 2024 3 016
14 nov. 2012 à 20:32
Tu pourrais nous donner un exemple de méthode main et éventuellement du fichier contact_prof.txt si c'est important, de manière à pouvoir tester...
0
nesro04 Messages postés 5 Date d'inscription mercredi 14 novembre 2012 Statut Membre Dernière intervention 15 novembre 2012
14 nov. 2012 à 23:06
salut
je 'nai pas de prb avec le fichier text, ceci s'ouvre normalement et je peux voir meme supprimer des lignes de donnees.
en tout cas voici un apercu de mon fichier text, une screenshot du resultat:
le contenu
le contact_prof.txt :

var walid lar_essma@yahoo.fr 661732648 logica 65329874
var walid lar_essma@yahoo.fr 661732688 logica 65329874
bar nesro lar_essma@yahoo.fr 661732658 ibm 65329874
bar nesro lar_essma@yahoo.fr 661732658 ibm 65329874
bar nesro lar_essma@yahoo.fr 661732668 ibm 65329874
bar nesro lar_essma@yahoo.fr 661732658 ibm 65329874
var walid lar_essma@yahoo.fr 661732658 logica 1111111
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874
lar essma lar_essma@yahoo.fr 661732658 cisco 65329874

bon pour la screenshot je ne sais pas comment l'uploader
anyway.


voici mon void fonction qui appel la class affich_prof:

package carnet;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;



public class ZFenetre extends JFrame {

public static ArrayList<Prof> list_prof = new ArrayList<Prof>();
public static ArrayList<Prof> list_prof_recherche = new ArrayList<Prof>();
public static ArrayList<Perso> list_perso = new ArrayList<Perso>();
public static ArrayList<Perso> list_perso_recherche = new ArrayList<Perso>();

public static JPanel panel = new JPanel();

public JFrame frame = new JFrame();
private JMenuBar menuBar = new JMenuBar();


private JMenu ajout = new JMenu("Ajout");
private JMenu recherche = new JMenu("Recherche");
private JMenu r_prof = new JMenu("Contacts professionnels");
private JMenu r_pers = new JMenu("Contacts personnels");
private JMenu affichage = new JMenu("Affichage");
private JMenu apropos = new JMenu("?");

private JMenuItem ajout_prof = new JMenuItem("Contact Professionnel");
private JMenuItem ajout_pers = new JMenuItem("Contact Personnel");
private JMenuItem fermer = new JMenuItem("Fermer");
private JMenuItem r_prof_nom = new JMenuItem("/ Nom");
private JMenuItem r_prof_prenom = new JMenuItem("/ Prenom");
private JMenuItem r_prof_comp = new JMenuItem("/ Compagny");
private JMenuItem r_prof_tel = new JMenuItem("/ N° Tel");
private JMenuItem r_prof_tel_comp = new JMenuItem("/ N° Tel Prof");
private JMenuItem r_prof_mc = new JMenuItem("Multi-Critéres");

private JMenuItem r_pers_nom = new JMenuItem("/ Nom");
private JMenuItem r_pers_prenom = new JMenuItem("/ Prenom");
private JMenuItem r_pers_af = new JMenuItem("/ Alias Facebook");
private JMenuItem r_pers_at = new JMenuItem("/ Alias Twitter");
private JMenuItem r_pers_tel = new JMenuItem("/ N° Tel");
private JMenuItem r_pers_mc = new JMenuItem("Multi-Critéres");

private JMenuItem a_prof = new JMenuItem("Contacts Professionnels");
private JMenuItem a_pers = new JMenuItem("Contacts Personnels");

private JMenuItem ap_q = new JMenuItem("APropos");




public ZFenetre(){
frame.setTitle("Gestion de Contacts ");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);



this.ajout.add(ajout_prof);
this.ajout.add(ajout_pers);
this.ajout.add(fermer);


//On ajoute les éléments dans notre sous-menu recherche professionnels
this.r_prof.add(r_prof_nom);
this.r_prof.add(r_prof_prenom);
this.r_prof.add(r_prof_comp);
this.r_prof.add(r_prof_tel);
this.r_prof.add(r_prof_tel_comp);
this.r_prof.addSeparator();
this.r_prof.add(r_prof_mc);

//On ajoute les éléments dans notre sous-menu recherche personnels
this.r_pers.add(r_pers_nom);
this.r_pers.add(r_pers_prenom);
this.r_pers.add(r_pers_af);
this.r_pers.add(r_pers_at);
this.r_pers.add(r_pers_tel);
this.r_pers.addSeparator();
this.r_pers.add(r_pers_mc);
//On ajoute les sous-menu recherche dans le menu recherche
this.recherche.add(this.r_prof);
this.recherche.add(this.r_pers);

this.affichage.add(a_prof);
this.affichage.add(a_pers);

this.apropos.add(ap_q);

ajout_prof.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {

Insertion_contact_Prof contact_prof= new Insertion_contact_Prof();
}
});

ajout_pers.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {

Insertion_contact_Perso contact_perso= new Insertion_contact_Perso();
}
});
a_prof.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
int i=0;
try {
list_prof= Prof.readcontact("D:\\course\\tpjava\\Application\\carnet\\contact_prof.txt");
} catch (IOException exception) {System.out.println ("exception : " + exception);}

if(affich_pers.boutons != null){
panel.remove(affich_pers.boutons);
panel.revalidate();
panel.repaint();
frame.setVisible(true);
}
affich_prof aff_prof = new affich_prof(frame, list_prof);

}
});

fermer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});

ap_q.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane jop2 = new JOptionPane();

String message = "Micor Projet Java : Gestion d'Annuaire\n\n";
message += "Réalisé par : Larech Nasr Allah\n";
message += "\n Version : 1.0\n";

jop2.showMessageDialog(null, message, "A Propos de L'application Gestion Contact", JOptionPane.INFORMATION_MESSAGE);

}
});

frame.add(ajout);
//this.menuBar.add(ajout);
ajout.setMnemonic('A');
menuBar.add(ajout);

this.menuBar.add(recherche);
recherche.setMnemonic('R');
menuBar.add(recherche);

this.menuBar.add(affichage);
affichage.setMnemonic('F');
menuBar.add(affichage);


this.menuBar.add(apropos);
apropos.setMnemonic('?');
menuBar.add(apropos);


frame.setJMenuBar(menuBar);
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension (800,600));
frame.add(panel);
//frame.getContentPane().add(panel);
frame.setVisible(true);
//this.setJMenuBar(menuBar);
//this.setVisible(true);
}
public static void main(String[] args) {


ZFenetre fen= new ZFenetre();
}
0
KX Messages postés 16741 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 30 mai 2024 3 016
14 nov. 2012 à 23:53
Bon, désolé, mais je n'arrive pas à tester ton code.
J'arrive à le compiler mais comme je n'ai pas tout, une fois lancé je n'ai pas la grille.

Pourtant les paramètres par défaut permettent de modifier les cases.
Voici un code minimal pour que l'on puisse travailler à résoudre ton problème, dis moi ce qui ne fonctionne pas chez toi :

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Test 
{
    public static void main(String[] args)
    {
        String[] entetes = {"Nom", "Prenom", "@ Mail", "Tel Personnel", "Compagny", "Tel Professionnel"};
        
        String lignes[] = 
        {
            "var walid lar_essma@yahoo.fr 661732648 logica 65329874",
            "var walid lar_essma@yahoo.fr 661732688 logica 65329874",
            "bar nesro lar_essma@yahoo.fr 661732658 ibm 65329874",
            "bar nesro lar_essma@yahoo.fr 661732658 ibm 65329874",
            "bar nesro lar_essma@yahoo.fr 661732668 ibm 65329874",
            "bar nesro lar_essma@yahoo.fr 661732658 ibm 65329874",
            "var walid lar_essma@yahoo.fr 661732658 logica 1111111",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874",
            "lar essma lar_essma@yahoo.fr 661732658 cisco 65329874"
        };

        String[][] data = new String[lignes.length][];
        for (int i=0; i<lignes.length; i++)
            data[i]=lignes[i].split("\\s");
        
        DefaultTableModel model = new DefaultTableModel(data,entetes);
        JTable table = new JTable(model);
                
        JFrame frame = new JFrame();
        frame.add(table.getTableHeader(), BorderLayout.NORTH);
        frame.add(table, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
0
nesro04 Messages postés 5 Date d'inscription mercredi 14 novembre 2012 Statut Membre Dernière intervention 15 novembre 2012
15 nov. 2012 à 00:20
merci pour cebou de code. en faite j'ai deja fiat ça et le celledit marche sans prb.
mon cas à moi passe par les modele de tableau. et c't la où ça ne marche pas pour moi.
avec les classes restantes, vous serez en mesure de compiler sans prb mon prg

classe perso:

apackage carnet;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;


/**
* la class "Prof" gère les contacts personnels
**/
public class Perso extends Personne {

String aliasf; // alias twitter
String aliast; // alias facebook

// ********************* methodes *********************************//

// ******************** ajout de contact ********************************** //
/**
* Il s'agit d'une methode pour ajouter les enregistrements des contact au fichier contact.txt
@param chemin de type string qui indique le chmein du fchier contact.txt
@param ligne la ligne à inserer representant un contact

**/
public static void addcontact(String chemin, String ligne) throws IOException {

File fichier = new File(chemin);
FileWriter FileWriter = null;

if (fichier.exists()) // test d'existance du fichier
FileWriter= new FileWriter(chemin, true);
else
FileWriter= new FileWriter(chemin);

PrintWriter out = new PrintWriter(FileWriter);
out.append(ligne);
out.println(); // saut de ligne
out.flush(); // effacement du stream
out.close(); // cloture du stream

}

// **************** lecture des contact à partir du fichier contact.txt ******************** //
/**
* Il s'agit d'une methode pour lire les enregistrements des contact a partir du fichier contact.txt
@param chemin de type string qui indique le chmein du fchier contact.txt

**/



public static ArrayList<Perso> readcontact(String chemin) throws IOException {

int i=0, tel = 0;
ArrayList<Perso> list_perso = new ArrayList<Perso>();
Perso contact_perso;
BufferedReader in = null;
FileReader fichier=new FileReader(chemin);
String nom="aa", prenom="bb", mail="cc", aliast="dd", aliasf="ee";

in = new BufferedReader (fichier);
for(String line=in.readLine(); line!=null;line=in.readLine()) {

StringTokenizer tok=new StringTokenizer(line, " "); // tok recoit la nouvelle ligne du fichier avec espace comme delimiteur


nom = tok.nextToken();
prenom = tok.nextToken();
mail = tok.nextToken();
tel = Integer.parseInt(tok.nextToken());
aliasf = tok.nextToken();
aliast = tok.nextToken();
contact_perso = new Perso(nom,prenom, mail,tel,aliasf, aliast );
list_perso.add(contact_perso);
i++;

}

if (in!= null) in.close();
// fermeture du buffer de lecture sil a ete ouvert

return list_perso;
}

// ******************* recherche d'un contact à base d'arguments ************************** //
/**
* Il s'agit d'une methode pour rechercher les contacts a partir du fichier contact.txt qui correspondent aux parametres de recherhce
@param list_perso arraylist des contacts professionnels
@param chaine[] de type tableau de string qui contient les parametres de recherche
@param crit_rech un entier indiquant le type de recherche et le parametre recherche
**/
public static ArrayList<Perso> serachcontact(ArrayList<Perso> list_perso, String chaine[], int crit_rech) {

int i=0,j=0, long_chaine=0;
ArrayList<Perso> list_perso_recherche = new ArrayList<Perso>();

long_chaine=chaine.length;

//ystem.out.println("valde chaine est" + chaine[0]);

if(crit_rech == 1){
for(i=0; i < list_perso.size(); i++){ // recherche par nom
if(list_perso.get(i).nom.equals(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
else if(crit_rech == 2){
for(i=0; i < list_perso.size(); i++){ // recherche par prenom
if(list_perso.get(i).prenom.equals(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
else if(crit_rech == 3){
for(i=0; i < list_perso.size(); i++){ // recherche par aliasf
if(list_perso.get(i).aliasf.equals(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
else if(crit_rech == 4){
for(i=0; i < list_perso.size(); i++){ // recherche par aliast
if(list_perso.get(i).aliast.equals(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
else if(crit_rech == 21){
for(i=0; i < list_perso.size(); i++){ // recherche par %nom%
if(list_perso.get(i).nom.contains(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}

else if(crit_rech == 5){
for(i=0; i < list_perso.size(); i++){ // recherche par tel
if(list_perso.get(i).tel== Integer.parseInt(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
else if(crit_rech == 22){
for(i=0; i < list_perso.size(); i++){ // recherche par %prenom%
if(list_perso.get(i).prenom.contains(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}

}
else if(crit_rech == 23){
for(i=0; i < list_perso.size(); i++){ // recherche par %aliasf%
if(list_perso.get(i).aliasf.contains(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
else if(crit_rech == 24){
for(i=0; i < list_perso.size(); i++){ // recherche par %aliast%
if(list_perso.get(i).aliast.contains(chaine[0])){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}

}
// ******************* recherche multi criteres ******************* //

if(crit_rech == 30){
for(i=0; i < list_perso.size(); i++){ // recherche par nom et prenom
if((list_perso.get(i).nom.equals(chaine[0]))&&(list_perso.get(i).prenom.equals(chaine[1]))){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}

else if(crit_rech == 31){
for(i=0; i < list_perso.size(); i++){ // recherche par %nom et prenom%
if((list_perso.get(i).nom.contains(chaine[0]))&&(list_perso.get(i).prenom.contains(chaine[1]))){
list_perso_recherche.add(j, list_perso.get(i));
j++;
}
}
}
return list_perso_recherche;
}
// ****************** tri de l'affichage selon le critere demande ************************ //

/**
* Il s'agit d'une methode pour trier les contact a partir du fichier contact.txt en vue de leurs affichage
@param list_perso arralyist contenant les contacts
@param opt le critére du tri
**/
public static ArrayList<Perso> sortcontact(ArrayList<Perso> list_perso, String opt){
int i = 0 , j=0, res = 0;
Perso contact_perso;

if (opt.equals("prenom")){ // tri par prenom
for(i=0; i < list_perso.size()-1; i++){
for(j=i+1; j < list_perso.size(); j++){
res = list_perso.get(i).prenom.compareTo(list_perso.get(j).prenom);
if (res < 0){
contact_perso = null;
contact_perso = list_perso.get(i);
list_perso.set(i, list_perso.get(j));
list_perso.set(j,contact_perso);
}
}
}
}
else if (opt.equals("nom")){ // tri par nom
for(i=0; i < list_perso.size()-1; i++){
for(j=i+1; j < list_perso.size(); j++){
res = list_perso.get(i).nom.compareTo(list_perso.get(j).nom);
if (res < 0){
contact_perso = null;
contact_perso = list_perso.get(i);
list_perso.set(i, list_perso.get(j));
list_perso.set(j,contact_perso);
}
}
}
}
else if (opt.equals("aliasf")){ // tri par aliasf
for(i=0; i < list_perso.size()-1; i++){
for(j=i+1; j < list_perso.size(); j++){
res = list_perso.get(i).aliasf.compareTo(list_perso.get(j).aliasf);
if (res < 0){
contact_perso = null;
contact_perso = list_perso.get(i);
list_perso.set(i, list_perso.get(j));
list_perso.set(j,contact_perso);
}
}
}
}
else if (opt.equals("aliast")){ // tri par aliast
for(i=0; i < list_perso.size()-1; i++){
for(j=i+1; j < list_perso.size(); j++){
res = list_perso.get(i).aliast.compareTo(list_perso.get(j).aliast);
if (res < 0){
contact_perso = null;
contact_perso = list_perso.get(i);
list_perso.set(i, list_perso.get(j));
list_perso.set(j,contact_perso);
}
}
}
}

// for(i=0; i < list_perso.size(); i++)
// System.out.println(list_perso.get(i).nom + list_perso.get(i).prenom + list_perso.get(i).mail + list_perso.get(i).tel + list_perso.get(i).aliasf + list_perso.get(i).aliast); // assignement de leleve a larraylist eleve
return list_perso;
}

// ******************** suppression de contact ********************************** //
/**
* Il s'agit d'une methode pour supprimer un contact a partir du fichier contact.txt
@param list_perso arralyist contenant les contacts
@param chemin du fichier contact.txt
@param nom nom du contact à supprimer
**/
public static ArrayList<Perso> removecontact(ArrayList<Perso> list_perso, String chemin) throws IOException {

int i=0; String ligne ="";
File fichier = new File(chemin);


fichier.delete();

for(i=0; i < list_perso.size(); i++){

ligne ="";
ligne = ligne + list_perso.get(i).nom +" "+list_perso.get(i).prenom +" "+list_perso.get(i).mail +" "+ list_perso.get(i).tel +" "+list_perso.get(i).aliasf+" "+list_perso.get(i).aliast;
addcontact( chemin, ligne);
}

return list_perso;
}

// ******************** modification de contact ********************************** //
/**
* Il s'agit d'une methode pour modifier les parametres d'un contact
@param list_perso arralyist contenant les contacts
@param chemin du fichier contact.txt
@param indice indice du contact à modifier dans la liste
@param contact à inserer et dont les attributs ont ete modifies
**/
public static ArrayList<Perso> modifcontact(ArrayList<Perso> list_perso, String chemin, int indice, Perso contact) throws IOException {

int i=0; String ligne ="";
File fichier = new File(chemin);

list_perso.remove(indice);
list_perso.add(contact);

fichier.delete();

for(i=0; i < list_perso.size(); i++){

ligne ="";
ligne = ligne + list_perso.get(i).nom +" "+list_perso.get(i).prenom +" "+list_perso.get(i).mail +" "+ list_perso.get(i).tel +" "+list_perso.get(i).aliasf+" "+list_perso.get(i).aliast;
addcontact( chemin, ligne);
}

return list_perso;
}
// ***************************** construcuteur Personne ************************************** //
/**
* constructeur Personne avec "this"
@param nom
@param prenom
@mail mail
@param aliast
@param aliasf
**/
public Perso(String nom, String prenom, String mail,int tel, String aliasf,String aliast ){
super(nom, prenom, mail, tel);
this.aliasf = aliasf;
this.aliast = aliast;
this.tel = tel; // tel personnelle
}

}
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
nesro04 Messages postés 5 Date d'inscription mercredi 14 novembre 2012 Statut Membre Dernière intervention 15 novembre 2012
15 nov. 2012 à 00:20
voici la class Prof:


package carnet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;


/**
* la class "Prof" gère les contacts professionels
**/
public class Prof extends Personne {


String compagny;
int tel_comp;

// **************** lecture des contact à partir du fichier contact_prof.txt ******************** //
/**
* Il s'agit d'une methode pour lire les enregistrements des contact a partir du fichier contact.txt
@param chemin de type string qui indique le chmein du fchier contact.txt

**/



public static ArrayList<Prof> readcontact(String chemin) throws IOException {

int i=0, tel = 0, tel_cmp =0;
ArrayList<Prof> list_prof = new ArrayList<Prof>();
Prof contact_prof;
BufferedReader in = null;
FileReader fichier=new FileReader(chemin);
String nom="aa", prenom="bb", mail="cc", compagny="dd";

in = new BufferedReader (fichier);
for(String line=in.readLine(); line!=null;line=in.readLine()) {

StringTokenizer tok=new StringTokenizer(line, " "); // tok recoit la nouvelle ligne du fichier avec espace comme delimiteur


nom = tok.nextToken();
prenom = tok.nextToken();
mail = tok.nextToken();
tel = Integer.parseInt(tok.nextToken());
compagny = tok.nextToken();
tel_cmp = Integer.parseInt(tok.nextToken());
contact_prof = new Prof(nom,prenom, mail,tel, compagny, tel_cmp );
list_prof.add(contact_prof);
i++;

}

if (in!= null) in.close();
// fermeture du buffer de lecture sil a ete ouvert

return list_prof;
}

// ******************** suppression de contact ********************************** //
/**
* Il s'agit d'une methode pour supprimer un contact a partir du fichier contact.txt
@param list_prof arralyist contenant les contacts
@param chemin du fichier contact.txt

**/
public static ArrayList<Prof> removecontact(ArrayList<Prof> list_prof, String chemin) throws IOException {

int i=0; String ligne ="";
File fichier = new File(chemin);

fichier.delete();

for(i=0; i < list_prof.size(); i++){

ligne ="";
ligne = ligne + list_prof.get(i).nom +" "+list_prof.get(i).prenom +" "+list_prof.get(i).mail +" "+ list_prof.get(i).tel +" "+list_prof.get(i).compagny+" "+list_prof.get(i).tel_comp;
Perso.addcontact( chemin, ligne);
}

return list_prof;
}

// ******************* recherche d'un contact professionnel à base d'arguments ************************** //
/**
* Il s'agit d'une methode pour rechercher les contacts professionnel a partir du fichier contact_prof.txt qui correspondent aux parametres de recherhce
@param list_prof arraylist des contacts professionnels
@param chaine[] de type tableau de string qui contient les parametres de recherche
@param crit_rech un entier indiquant le type de recherche et le parametre recherche
**/
public static ArrayList<Prof> serachcontact(ArrayList<Prof> list_prof, String chaine[], int crit_rech) {

int i=0,j=0, long_chaine=0;
ArrayList<Prof> list_prof_recherche = new ArrayList<Prof>();

long_chaine=chaine.length;

//System.out.println("valde chaine est" + chaine[0]);

if(crit_rech == 1){
for(i=0; i < list_prof.size(); i++){ // recherche par nom
if(list_prof.get(i).nom.equals(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}
else if(crit_rech == 2){
for(i=0; i < list_prof.size(); i++){ // recherche par prenom
if(list_prof.get(i).prenom.equals(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}
else if(crit_rech == 5){
for(i=0; i < list_prof.size(); i++){ // recherche par compagny
if(list_prof.get(i).compagny.equals(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}

else if(crit_rech == 4){
for(i=0; i < list_prof.size(); i++){ // recherche par tel
if(list_prof.get(i).tel== Integer.parseInt(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}
else if(crit_rech == 6){
for(i=0; i < list_prof.size(); i++){ // recherche par tel_comp
if(list_prof.get(i).tel_comp== Integer.parseInt(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}
else if(crit_rech == 21){
for(i=0; i < list_prof.size(); i++){ // recherche par %nom%
if(list_prof.get(i).nom.contains(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}

else if(crit_rech == 22){
for(i=0; i < list_prof.size(); i++){ // recherche par %prenom%
if(list_prof.get(i).prenom.contains(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}

}
else if(crit_rech == 25){
for(i=0; i < list_prof.size(); i++){ // recherche par %compagny%
if(list_prof.get(i).compagny.contains(chaine[0])){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}

// ******************* recherche multi criteres ******************* //

if(crit_rech == 30){
for(i=0; i < list_prof.size(); i++){ // recherche par nom et prenom
if((list_prof.get(i).nom.equals(chaine[0]))&&(list_prof.get(i).prenom.equals(chaine[1]))){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}

else if(crit_rech == 31){
for(i=0; i < list_prof.size(); i++){ // recherche par %nom et prenom%
if((list_prof.get(i).nom.contains(chaine[0]))&&(list_prof.get(i).prenom.contains(chaine[1]))){
list_prof_recherche.add(j, list_prof.get(i));
j++;
}
}
}
return list_prof_recherche;
}
// ****************** tri de l'affichage selon le critere demande ************************ //

/**
* Il s'agit d'une methode pour trier les contacts professionnel à partir du fichier contact_prof.txt en vue de leurs affichage
@param list_prof arralyist contenant les contacts
@param opt le critére du tri
**/
public static ArrayList<Prof> sortcontact(ArrayList<Prof> list_prof, String opt){
int i = 0 , j=0, res = 0;
Prof contact_prof;

if (opt.equals("prenom")){ // tri par prenom
for(i=0; i < list_prof.size()-1; i++){
for(j=i+1; j < list_prof.size(); j++){
res = list_prof.get(i).prenom.compareTo(list_prof.get(j).prenom);
if (res < 0){
contact_prof = null;
contact_prof = list_prof.get(i);
list_prof.set(i, list_prof.get(j));
list_prof.set(j,contact_prof);
}
}
}
}
else if (opt.equals("nom")){ // tri par nom
for(i=0; i < list_prof.size()-1; i++){
for(j=i+1; j < list_prof.size(); j++){
res = list_prof.get(i).nom.compareTo(list_prof.get(j).nom);
if (res < 0){
contact_prof = null;
contact_prof = list_prof.get(i);
list_prof.set(i, list_prof.get(j));
list_prof.set(j,contact_prof);
}
}
}
}
else if (opt.equals("compagny")){ // tri par compagny
for(i=0; i < list_prof.size()-1; i++){
for(j=i+1; j < list_prof.size(); j++){
res = list_prof.get(i).compagny.compareTo(list_prof.get(j).compagny);
if (res < 0){
contact_prof = null;
contact_prof = list_prof.get(i);
list_prof.set(i, list_prof.get(j));
list_prof.set(j,contact_prof);
}
}
}
}


// for(i=0; i < list_prof.size(); i++)
// System.out.println(list_prof.get(i).nom + list_prof.get(i).prenom + list_prof.get(i).mail + list_prof.get(i).tel + list_prof.get(i).aliasf + list_prof.get(i).aliast); // assignement de leleve a larraylist eleve
return list_prof;
}

// ******************** modification de contact ********************************** //
/**
* Il s'agit d'une methode pour modifier les parametres professionnel d'un contact
@param list_prof arralyist contenant les contacts
@param chemin du fichier contact_prof.txt
@param indice indice du contact à modifier dans la liste
@param contact à inserer et dont les attributs ont ete modifies
**/
public static ArrayList<Prof> modifcontact(ArrayList<Prof> list_prof, String chemin, int indice, Prof contact) throws IOException {

int i=0; String ligne ="";
File fichier = new File(chemin);

list_prof.remove(indice);
list_prof.add(contact);

fichier.delete();

for(i=0; i < list_prof.size(); i++){

ligne ="";
ligne = ligne + list_prof.get(i).nom +" "+list_prof.get(i).prenom +" "+list_prof.get(i).mail +" "+ list_prof.get(i).tel +" "+list_prof.get(i).compagny+" "+list_prof.get(i).tel_comp;
Perso.addcontact( chemin, ligne);
}

return list_prof;
}

// ********************** constructeur Prof ********************************** //
/**
* constructeur Personne avec "this"
@param nom
@param prenom
@mail mail
@param tel_comp
@param compagny
**/
public Prof(String nom, String prenom, String mail, int tel,String compagny, int tel_comp){
super(nom, prenom, mail, tel);
this.compagny = compagny;
this.tel_comp = tel_comp;
}

}
0
nesro04 Messages postés 5 Date d'inscription mercredi 14 novembre 2012 Statut Membre Dernière intervention 15 novembre 2012
15 nov. 2012 à 00:21
et finalement la classe abstraite personne.
package carnet;

/**
* la class "Personne" est une classe abstraite sans methodes
**/
public abstract class Personne
{
String prenom;
String nom;
String mail;
int tel;

/**
* constructeur Personne avec "this"
@param nom
@param prenom
@mail
**/
public Personne(String nom, String prenom, String mail, int tel)
{
this.nom = nom;
this.prenom = prenom;
this.mail=mail;
this.tel =tel; // tel professionnel
}
}
0
nesro04 Messages postés 5 Date d'inscription mercredi 14 novembre 2012 Statut Membre Dernière intervention 15 novembre 2012
15 nov. 2012 à 00:33
salut encore et merci pour tout ce que tu as fait pour m'aider.
je viens de voir sur le net qu'il faut implementer la methode setValueAt(int x, int y) pour justement je ne pouvais pas editer mes cellules malgrés que j'ai implemente la methode isCellEditable avec un return true;
merci encore
0
KX Messages postés 16741 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 30 mai 2024 3 016
15 nov. 2012 à 00:41
Donc ça marche ou pas maintenant ?
Sinon, j'ai un problème pour ton code, dans la classe ZFenetre lignes 125/126, l'objet affich_pers n'existe pas, si tu as toujours un problème dis moi d'où sors cet objet...
0