Object[][] mesdonnees = new Object[maliste.size()][]; for (int i = 0; i < maliste.size(); i++) { Personne donnee = maliste.get(i); mesdonnees[i] = new Object[] { i + 1, donnee.getName(), donnee.getGivenName(), donnee.getPhoneNumber(), donnee.getBirthday(), donnee.getEmail() }; }
if (a.getSource() == bouttonValidAjout) { StringBuilder warning = new StringBuilder(); String nom = jtfnom.getText(); if (nom.trim().isEmpty()) warning.append("Le champ 'nom' est vide.\n"); String prenom = jtfprenom.getText(); if (prenom.trim().isEmpty()) warning.append("Le champ 'prenom' est vide.\n"); String tel = jtftel.getText(); if (tel.trim().isEmpty()) warning.append("Le champ 'tel' est vide.\n"); String anniv = jtfanniv.getText(); if (anniv.trim().isEmpty()) warning.append("Le champ 'anniv' est vide.\n"); String email = jtfemail.getText(); if (email.trim().isEmpty()) warning.append("Le champ 'email' est vide.\n"); if (warning.length() > 0) { JOptionPane.showMessageDialog(this, warning.toString(), "Formulaire incomplet", JOptionPane.WARNING_MESSAGE); return; } maliste.add(new Contact(nom, prenom, tel, anniv, email)); jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); labelphoto.setVisible(false); fenetreAjout.setVisible(false); }
public class ContactManager extends JFrame implements ActionListener{ ArrayList <Contact> maliste = new ArrayList <Contact>(); // Declarations de mes differents composants /*Les boutons avec le constructeur de mma fenetre principale qui contient : ajouter,modifier,supprimer,rechercher,afficher liste.*/ JButton bouttonAjout= new JButton("Ajouter"); JButton bouttonValidAjout = new JButton("Valider"); JButton bouttonInvalidAjout = new JButton("Annuler"); JButton bouttonModif= new JButton("Modifier"); JButton bouttonValidModif= new JButton("Valider"); JButton bouttonInvalidModif= new JButton("Annuler"); JButton bouttonSupp= new JButton("Supprimer"); JButton bouttonValidSupp= new JButton("Valider"); JButton bouttonInvalidSupp= new JButton("Annuler"); JButton bouttonCherch= new JButton("Rechercher"); JButton bouttonValidCherch= new JButton("Valider"); JButton bouttonInvalidCherch= new JButton("Annuler"); JButton bouttonAffich= new JButton("Afficher Liste"); JPanel panelMesboutons= new JPanel(); JPanel constructPanelMesboutons() { panelMesboutons.setLayout(new GridLayout(1,5)); panelMesboutons.add(bouttonAjout); bouttonAjout.addActionListener(this); panelMesboutons.add(bouttonModif); bouttonModif.addActionListener(this); panelMesboutons.add(bouttonSupp); bouttonSupp.addActionListener(this); panelMesboutons.add(bouttonCherch); bouttonCherch.addActionListener(this); panelMesboutons.add(bouttonAffich); bouttonAffich.addActionListener(this); return panelMesboutons; } /*Le JPanel avec fond personnalisé.*/ ImageArrierePlan monPanel= new ImageArrierePlan("C:\\Users\\Mary\\Pictures\\image7.jpg"); /*constructeur de monPanel*/ public JPanel constructMonpanel() { monPanel.setLayout(new BorderLayout()); monPanel.add("South",constructPanelMesboutons()); return monPanel; } /*Mes JLabels et mes JTextFields pour les ecrans de saisie de mes fenetres*/ JLabel labelphoto = new JLabel(); JLabel labelnom = new JLabel("Nom"); JTextField jtfnom = new JTextField(); JLabel labelprenom = new JLabel("Prenom(s)"); JTextField jtfprenom = new JTextField(); JLabel labeltel = new JLabel("Telephone"); JTextField jtftel = new JTextField(); JLabel labelanniv = new JLabel("Date de naissance"); JTextField jtfanniv = new JTextField(); JLabel labelemail = new JLabel("Email"); JTextField jtfemail = new JTextField(); JLabel labelposition = new JLabel("Position"); JLabel labelElem = new JLabel("de l'element : "); JTextField jtfposition = new JTextField(); JButton ajoutphoto= new JButton("Ajouter photo"); /*Mes fenetres et leurs constructeurs*/ JFrame fenetreAjout = new JFrame(); JPanel panelFenetreAjout =new JPanel(); public JPanel constructPanelFenetreAjout() { panelFenetreAjout.setLayout(null); ajoutphoto.setBounds(350, 5, 150, 30); labelnom.setBounds(10, 10, 100, 15); jtfnom.setBounds(150, 5, 150, 30); labelprenom.setBounds(10, 60, 100, 15); jtfprenom.setBounds(150, 55, 150, 30); labeltel.setBounds(10, 110, 100, 15); jtftel.setBounds(150, 105, 150, 30); labelanniv.setBounds(10, 160, 150, 15); jtfanniv.setBounds(150, 155, 150, 30); labelemail.setBounds(10, 210, 100, 15); jtfemail.setBounds(150, 205, 150, 30); labelphoto.setBounds(325, 45, 200, 200); bouttonValidAjout.setBounds(175, 255, 80, 30); bouttonInvalidAjout.setBounds(390, 255, 80, 30); ajoutphoto.addActionListener(this); panelFenetreAjout.add(ajoutphoto); panelFenetreAjout.add(labelnom); panelFenetreAjout.add(jtfnom); panelFenetreAjout.add(labelprenom); panelFenetreAjout.add(jtfprenom); panelFenetreAjout.add(labeltel); panelFenetreAjout.add(jtftel); panelFenetreAjout.add(labelanniv); panelFenetreAjout.add(jtfanniv); panelFenetreAjout.add(labelemail); panelFenetreAjout.add(jtfemail); panelFenetreAjout.add(labelphoto); panelFenetreAjout.add(bouttonValidAjout); bouttonValidAjout.addActionListener(this); panelFenetreAjout.add(bouttonInvalidAjout); bouttonInvalidAjout.addActionListener(this); return panelFenetreAjout; } JFrame affichemsgmodif = new JFrame(); JPanel panelFenetreModif= new JPanel(); public JPanel constructPanelFenetreModif() { panelFenetreModif.setLayout(null); labelnom.setBounds(10, 10, 100, 15); jtfnom.setBounds(150, 5, 150, 30); labelprenom.setBounds(10, 60, 100, 15); jtfprenom.setBounds(150, 55, 150, 30); labeltel.setBounds(10, 110, 100, 15); jtftel.setBounds(150, 105, 150, 30); labelanniv.setBounds(10, 160, 150, 15); jtfanniv.setBounds(150, 155, 150, 30); labelemail.setBounds(10, 210, 100, 15); jtfemail.setBounds(150, 205, 150, 30); labelphoto.setBounds(325, 45, 200, 200); bouttonValidModif.setBounds(175, 255, 80, 30); bouttonInvalidModif.setBounds(350, 255, 80, 30); labelposition.setBounds(320, 10, 100, 15); jtfposition.setBounds(400, 5, 30, 30); panelFenetreModif.add(labelnom); panelFenetreModif.add(jtfnom); panelFenetreModif.add(labelprenom); panelFenetreModif.add(jtfprenom); panelFenetreModif.add(labeltel); panelFenetreModif.add(jtftel); panelFenetreModif.add(labelanniv); panelFenetreModif.add(jtfanniv); panelFenetreModif.add(labelemail); panelFenetreModif.add(jtfemail); panelFenetreModif.add(labelposition); panelFenetreModif.add(jtfposition) ; panelFenetreModif.add(labelphoto); panelFenetreModif.add(bouttonValidModif); bouttonValidModif.addActionListener(this); panelFenetreModif.add(bouttonInvalidModif); bouttonInvalidModif.addActionListener(this); return panelFenetreModif; } JFrame affichemsgsupp = new JFrame(); JPanel panelFenetreSupp= new JPanel(); public JPanel constructPanelFenetreSupp() { panelFenetreSupp.setLayout(null); labelposition.setBounds(10, 20, 100, 15); labelElem.setBounds(60,20,100,15); jtfposition.setBounds(180, 15, 35, 35); bouttonValidSupp.setBounds(10, 70, 80, 30); bouttonInvalidSupp.setBounds(140, 70, 80, 30); panelFenetreSupp.add(bouttonValidSupp); panelFenetreSupp.add(bouttonInvalidSupp); panelFenetreSupp.add(jtfposition); panelFenetreSupp.add(labelposition); panelFenetreSupp.add(labelElem) ; panelFenetreSupp.add(bouttonValidSupp); bouttonValidSupp.addActionListener(this); panelFenetreSupp.add(bouttonInvalidSupp); bouttonInvalidSupp.addActionListener(this); return panelFenetreSupp; } JFrame affichemsgrecherch = new JFrame(); JPanel panelFenetreCherch= new JPanel(); public JPanel constructPanelFenetreCherch() { panelFenetreCherch.setLayout(null); labelnom.setBounds(10, 10, 100, 15); jtfnom.setBounds(150, 5, 150, 30); labelprenom.setBounds(10, 60, 100, 15); jtfprenom.setBounds(150, 55, 150, 30); labeltel.setBounds(10, 110, 100, 15); jtftel.setBounds(150, 105, 150, 30); labelanniv.setBounds(10, 160, 150, 15); jtfanniv.setBounds(150, 155, 150, 30); labelemail.setBounds(10, 210, 100, 15); jtfemail.setBounds(150, 205, 150, 30); labelphoto.setBounds(325, 45, 200, 200); bouttonValidCherch.setBounds(175, 255, 80, 30); bouttonInvalidCherch.setBounds(350, 255, 80, 30); panelFenetreCherch.add(labelnom); panelFenetreCherch.add(jtfnom); panelFenetreCherch.add(labelprenom); panelFenetreCherch.add(jtfprenom); panelFenetreCherch.add(labeltel); panelFenetreCherch.add(jtftel); panelFenetreCherch.add(labelanniv); panelFenetreCherch.add(jtfanniv); panelFenetreCherch.add(labelemail); panelFenetreCherch.add(jtfemail); panelFenetreCherch.add(labelphoto); panelFenetreCherch.add(bouttonValidCherch); bouttonValidCherch.addActionListener(this); panelFenetreCherch.add(bouttonInvalidCherch); bouttonInvalidCherch.addActionListener(this); return panelFenetreCherch; } /*Le constructeur de ma classe principale*/ ContactManager() { this.setTitle("Gestionnaire de contacts"); this.setSize(600, 600); this.setLocationRelativeTo(null); this.setContentPane(constructMonpanel()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); } /*Ma methode actionPerformed qui gere tous les évènements survenuent suite au clicke d'un boutton.*/ public void actionPerformed(ActionEvent a) { //Gestion de l'ajout d'un contact. if(a.getSource()==this.bouttonAjout) { fenetreAjout.setTitle("Ecran de saisie de contact"); fenetreAjout.add(constructPanelFenetreAjout()); fenetreAjout.setSize(560,330); fenetreAjout.setLocationRelativeTo(null); fenetreAjout.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); fenetreAjout.setVisible(true); } if(a.getSource()==this.bouttonValidAjout) { String nom=jtfnom.getText(); String prenom=jtfprenom.getText(); String tel=jtftel.getText(); String anniv=jtfanniv.getText(); String email=jtfemail.getText(); Contact contact= new Contact(nom,prenom,tel,anniv,email); maliste.add(contact); jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); labelphoto.setVisible(false); fenetreAjout.setVisible(false); } if(a.getSource()==this.bouttonInvalidAjout) { jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); fenetreAjout.setVisible(false); } //Gestion de l'ajout d'une photo. if(a.getSource()==this.ajoutphoto) { JFileChooser dialogue = new JFileChooser(new File("C:\\Users\\Mary\\Documents\\NetBeansProjects\\Images")); if (dialogue.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { ImageIcon monimage= new ImageIcon(dialogue.getSelectedFile().getAbsolutePath()); labelphoto.setIcon(monimage); } } //Gestion de la modification d'un contact if(a.getSource()==this.bouttonModif) { affichemsgmodif.setContentPane(constructPanelFenetreModif()); affichemsgmodif.setTitle("Modification"); affichemsgmodif.setSize(590, 330); affichemsgmodif.setLocationRelativeTo(null); affichemsgmodif.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); affichemsgmodif.setVisible(true); } if(a.getSource()==this.bouttonValidModif) { int i=Integer.parseInt(jtfposition.getText()); Contact cont= new Contact(jtfnom.getText(),jtfprenom.getText(),jtftel.getText(),jtfanniv.getText(),jtfemail.getText()); maliste.set(i-1,cont); jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); jtfposition.setText(""); affichemsgmodif.dispose(); affichemsgmodif.setVisible(false); } if(a.getSource()==this.bouttonInvalidModif) { jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); affichemsgmodif.setVisible(false); } //Gestion de la suppression d'un contact if(a.getSource()==this.bouttonSupp) { affichemsgsupp.setContentPane(constructPanelFenetreSupp()); affichemsgsupp.setTitle("Suppression"); affichemsgsupp.setSize(250, 150); affichemsgsupp.setLocationRelativeTo(null); affichemsgsupp.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); affichemsgsupp.setVisible(true); } if(a.getSource()==this.bouttonValidSupp) { int i=Integer.parseInt(jtfposition.getText()); maliste.remove(i-1); jtfposition.setText(""); affichemsgsupp.dispose(); affichemsgsupp.setVisible(false); } if(a.getSource()==this.bouttonInvalidSupp) { affichemsgsupp.dispose(); jtfposition.setText(""); affichemsgsupp.setVisible(false); } //Gestion de la recherche d'un contact. if(a.getSource()==this.bouttonCherch) { affichemsgrecherch.setTitle("Recherche contact"); affichemsgrecherch.add(constructPanelFenetreCherch()); affichemsgrecherch.setSize(520,330); affichemsgrecherch.setLocationRelativeTo(null); affichemsgrecherch.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); affichemsgrecherch.setVisible(true); } if(a.getSource()==this.bouttonValidCherch) { Contact cRchrch= new Contact(jtfnom.getText(),jtfprenom.getText(),jtftel.getText(),jtfanniv.getText(),jtfemail.getText()); recherche(cRchrch); jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); jtfposition.setText(""); affichemsgrecherch.dispose(); affichemsgrecherch.setVisible(false); } if(a.getSource()==this.bouttonInvalidCherch) { affichemsgrecherch.dispose(); jtfnom.setText(""); jtfprenom.setText(""); jtftel.setText(""); jtfanniv.setText(""); jtfemail.setText(""); affichemsgrecherch.setVisible(false); } // Gestion de l'affichage de mes contacts. if(a.getSource()==this.bouttonAffich) { affiche(); } } /*Mes methodes affiche et recherche que j'appelle pour l'affichage et la recherche de contact.*/ private void affiche() { String []entetes ={"Numeros de liste","Noms","Prenoms","Telephones","Date d'anniversaire","Emails"}; Object [][] mesdonnees= new Object[maliste.size()][6]; for(int i=0;i<maliste.size();i++) { mesdonnees[i][0]=i+1; mesdonnees[i][1]=maliste.get(i).getName(); mesdonnees[i][2]=maliste.get(i).getGivenName(); mesdonnees[i][3]=maliste.get(i).getPhoneNumber(); mesdonnees[i][4]=maliste.get(i).getBirthday(); mesdonnees[i][5]=maliste.get(i).getEmail(); } JTable majtable= new JTable(mesdonnees, entetes); JFrame affichemajtable = new JFrame(); affichemajtable.setTitle("Ma liste de contacts"); affichemajtable.setSize(650,250); affichemajtable.setResizable(false); affichemajtable.add(constructPanelentetes(),"North"); affichemajtable.add(new JScrollPane(majtable),BorderLayout.CENTER); affichemajtable.setLocationRelativeTo(null); affichemajtable.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); affichemajtable.setVisible(true); } private void recherche(Contact someone) { Object [][] donnes= new Object[maliste.size()][6]; String []entetes ={"Numeros de liste","Noms","Prenoms","Telephones","Date d'anniversaire","Emails"}; for(int i=0;i<maliste.size();i++) { if(someone.equals(maliste.get(i))) { donnes[i][0]=i+1; donnes[i][1]=maliste.get(i).getName(); donnes[i][2]=maliste.get(i).getGivenName(); donnes[i][3]=maliste.get(i).getPhoneNumber(); donnes[i][4]=maliste.get(i).getBirthday(); donnes[i][5]=maliste.get(i).getEmail(); } else { donnes[i][0]=i+1; donnes[i][1]="Aucun resultat"; donnes[i][2]="Aucun resultat"; donnes[i][3]="Aucun resultat"; donnes[i][4]="Aucun resultat"; donnes[i][5]="Aucun resultat"; } } JTable elemFound= new JTable(donnes,entetes); JFrame elemTrouves = new JFrame(); elemTrouves.setTitle("Resultat de recherche"); elemTrouves.setSize(650,250); elemTrouves.setResizable(false); elemTrouves.add(new JScrollPane(elemFound),BorderLayout.CENTER); elemTrouves.setLocationRelativeTo(null); elemTrouves.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); elemTrouves.setVisible(true); } private LayoutManager BorderLayout() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
/* Mes fenetres et leurs constructeurs */ JFrame fenetreAjout = new JFrame(); JPanel panelFenetreAjout = null; public JPanel constructPanelFenetreAjout() { if (panelFenetreAjout != null) return panelFenetreAjout; panelFenetreAjout = new JPanel(); panelFenetreAjout.setLayout(null);
if (a.getSource() == this.
JPanel constructPanelMesboutons() { panelMesboutons.setLayout(new GridLayout(1, 5)); panelMesboutons.add(bouttonAjout); bouttonAjout.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fenetreAjout.setTitle("Ecran de saisie de contact"); fenetreAjout.add(constructPanelFenetreAjout()); fenetreAjout.setSize(560, 330); fenetreAjout.setLocationRelativeTo(null); fenetreAjout.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); fenetreAjout.setVisible(true); } }); panelMesboutons.add(bouttonModif); bouttonModif.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { affichemsgmodif.setContentPane(constructPanelFenetreModif()); affichemsgmodif.setTitle("Modification"); affichemsgmodif.setSize(590, 330); affichemsgmodif.setLocationRelativeTo(null); affichemsgmodif.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); affichemsgmodif.setVisible(true); } }); // ...
private void actionPerformedBouttonAjout(ActionEvent e) { fenetreAjout.setTitle("Ecran de saisie de contact"); fenetreAjout.add(constructPanelFenetreAjout()); fenetreAjout.setSize(560, 330); fenetreAjout.setLocationRelativeTo(null); fenetreAjout.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); fenetreAjout.setVisible(true); } private void actionPerformedBouttonModif(ActionEvent e) { affichemsgmodif.setContentPane(constructPanelFenetreModif()); affichemsgmodif.setTitle("Modification"); affichemsgmodif.setSize(590, 330); affichemsgmodif.setLocationRelativeTo(null); affichemsgmodif.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); affichemsgmodif.setVisible(true); } JPanel constructPanelMesboutons() { panelMesboutons.setLayout(new GridLayout(1, 5)); panelMesboutons.add(bouttonAjout); bouttonAjout.addActionListener(this::actionPerformedBouttonAjout); panelMesboutons.add(bouttonModif); bouttonModif.addActionListener(this::actionPerformedBouttonModif); // ...