hallo guys.i am newbie at java and i want ur help. i have created the 5 five buttons but not the action listeners for them.
the five buttons are : add person, see ll persons, search a person,update a person, delete person.so when i press add person the programm should be asking about the
name:....... ,
sur name:....,
city:..... , etc... i wanna ask u where should i write the jlabels for these variables inside the action listener method or outside and where? same for delete,search and find and delete methods.
i am sorry for my english and thanks for ur help here is my code:

/**
 * Simple Agenda1 Class - no error checking.
 * 
 * 
 * @version Version 1.0 - 
 */
import java.util.ArrayList;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Agenda1 extends JFrame
{

    private ArrayList<Person> persons; 
        JPanel contentPane;
        BorderLayout borderLayout1 = new BorderLayout ();
        JPanel centrePanel = new JPanel ();
        JTextField titleTextField = new JTextField ();
        JTextField IDTextField = new JTextField ();
        JPanel southPanel = new JPanel ();
        JButton addButton = new JButton ();
        JPanel northPanel = new JPanel ();
        JLabel northLabel = new JLabel ();
        JButton searchButton = new JButton ();
        JButton deleteButton = new JButton ();
        JButton sealldataButton = new JButton ();
        JButton findButton = new JButton ();
        JButton updateButton = new JButton ();
        JButton exitButton = new JButton ();


  
    public Agenda1(){
       persons = new ArrayList<Person>();
       makeFrame();
       setVisible (true);

       
} 
 private void makeFrame ()
{
contentPane = (JPanel) this.getContentPane ();
contentPane.setLayout (borderLayout1);
this.setSize (new Dimension(420, 200));
this.setTitle ("Agenda menu");



addButton.setText ("Add a person");
contentPane.add(centrePanel, BorderLayout.CENTER);
centrePanel.add(titleTextField, null);
centrePanel.add(IDTextField, null);
contentPane.add(southPanel, BorderLayout.SOUTH);
southPanel.add(addButton, null);
southPanel.add(searchButton, null);
southPanel.add(sealldataButton, null);
southPanel.add(deleteButton, null);
southPanel.add(updateButton, null);
southPanel.add(exitButton, null);
contentPane.add(northPanel, BorderLayout.NORTH);
northLabel.setText ("");
searchButton.setText ("Search Person");
sealldataButton.setText ("See All Data");
deleteButton.setText("Delete a Contact");     
updateButton.setText("Update a Contact"); 
exitButton.setText("Exit"); 

}




 
public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == addButton)
{




}

if(ae.getSource() == searchButton)

{



}
if(ae.getSource() == deleteButton)
{


}
if(ae.getSource() == updateButton)


/** part b. 
     * @param no param -
     * @param toString -add a contact
     */      
}  
public void addData()
{

Scanner s=new Scanner(System.in);
System.out.println("give name");
String name=s.next();
System.out.println("give surname");
String surname=s.next();
System.out.println("give tel");
String tel=s.next();
System.out.println("zip");
String zip=s.next ();
System.out.println("address");
String address=s.next();
System.out.println("email");
String email=s.next();
System.out.println("birthday");
String birthday=s.next();
System.out.println("polh");
String polh=s.next();
Person p1=new Person(name,surname,tel,zip,address,email,birthday,polh);
persons.add(p1);

System.out.println("the data were saved succesfylly");
}
/** part c. 
     * @param no param -
     * @param addData - adding contact using String parameters
     */
    public void addData(String name,String surname,String tel,String zip,String address,String email,String birthday,String polh)
    {   
       Person p1=new Person(name,surname,tel,zip,address,email,birthday,polh);
        persons.add(p1);
    }
    /** part d. 
     * @param no param -
     * @param numberOfdata -return all contacts
     */
    public int numberOfdata(){
            return persons.size();
    }
     /** part e. 
     * @param  - int
     * @param datanumber -shows contact by editing an Int
     */
    public void showdata(int dataNumber){
               System.out.println(persons.get(dataNumber).toString());
        }    
        
    public static void main(String args[]){
        new Agenda1();
    }
    
    /** part f. 
     * @param  - no param
    * @param findbyname -find method using scanner util for searching a contact
     */
   public int findByName(){
    Scanner s=new Scanner(System.in);
    System.out.println("give name");
    String name=s.next();
    System.out.println("give surname");
    String surname=s.next();
    int count=0;
    for (int i=0;i<persons.size();i++){
        Person temp=persons.get(i);
        String name1=temp.getname();
        String surname1=temp.getSurname();
        if (name.equals(name1) && surname.equals(surname1))
                return  count;
 
            count++;
         
        
       
   }
     return  count;
 
}
 /** part g. 
     * @param  -STring
    * @param findbyname -find method using String param and return an int(person)
     */
  public int findByName(String name,String surname ){
    
   
    
    for (int i=0;i<persons.size();i++){
        Person temp=persons.get(i);
        String name1=temp.getname();
        String surname1=temp.getSurname();
        if (name.equals(name1) && surname.equals(surname1))
                return  i;
 
             
         
        
       
   }
     return  -1;
 
}
 /** part h. 
     * @param  no param
    * @param RemoveByname -remove method using findbyname method
     */
public void RemoveByname()
{
Scanner s=new Scanner(System.in);
    System.out.println("give id");
    String name =s.next();
    System.out.println("give surname");
     String surname=s.next();
    
    
    
    int count=findByName(  name,  surname );
     if (count!=-1) persons.remove(count);
   System.out.println("removed ");

      
}
/** part i. 
     * @param  no param
    * @param RemoveByid -remove method using int param to remove person
     */
public void RemoveByID()
{
    Scanner s=new Scanner(System.in);
    System.out.println("give id");
    int id =s.nextInt();
    //System.out.println("give surname");
     //surname=s.next();
    
    
    persons.remove(id);
    /*int count=1;
    //for (int i=1;i<persons.size();i--){
        Person temp=persons.remove(i);
        String name1=temp.getname();
        String surname1=temp.getSurname();
        if (name.equals(name1) && surname.equals(surname1)){
            System.out.println(temp.toString());
            count--;
        }

   }*/
   System.out.println("removed ");

       
   
}  
   /** part j. 
     * @param  no param
    * @param Listpersons -Lists all contancts
     */
    public void listPersons(){
    for(Person per : persons) {
        System.out.println(per.toString());
    }

}
/** part k. 
     * @param STring
    * @param found person -finds person using String param
     */


   public Person foundPerson(String name,String surname){
    Person found=null;
    for (int i=0;i<persons.size();i++){
        Person temp=persons.get(i);
        String name1=temp.getname();
        String surname1=temp.getSurname();
        if (name.equals(name1) && surname.equals(surname1)){
            found=temp;
            break;
        }

   }
  return found;
}
/** part L. 
     * @param  no param
    * @param ypdate -Edits a person
     */
 public void update()
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Give Name"); String name=in.next();
        System.out.println("Give Surname"); String surname=in.next();
        Person temp=foundPerson(name,surname);
        if (temp==null) 
            System.out.println("Not Found");
        else{
            System.out.println("give new mobile"); String mobile=in.next();
            temp.setTel(mobile);
            System.out.println("give new City"); String polh=in.next();
            temp.setCity(polh);
            System.out.println("give new email"); String email=in.next();
            temp.setEmail(email);
            System.out.println("give new address"); String address=in.next();
            temp.setAddress(address);
            System.out.println("give new Birthday"); String Birthday=in.next();
            temp.setBirthday(Birthday);
            
        }
    }
    /** part M. 
     * @param  no param
    * @param menu -gives number of choice connecting with methods of arraylist
     */
    public void menu(){
     int choice=1;
     Scanner s=new Scanner(System.in);
     while (choice!=0){ 
        System.out.println("1. Insert new Person");
        System.out.println("2. Find by name");
        System.out.println("3. REmove by id");
        System.out.println("4. see all data");
        System.out.println("5. update by name");
        System.out.println("6. REmove by name");
        System.out.println("0. Exit");
        String strchoice=s.next ();
        choice= Integer.parseInt(strchoice);
        switch(choice){
            case 1: addData();break;
            case 2: findByName(); break;
            case 3: RemoveByID(); break;
            case 4: listPersons();break;
            case 5: update();break;
             case 6: RemoveByname();break;
            case 0: System.out.println("closing...");
        }
    }
}
}

my person class

/**
 * Simple Person Class - no error checking.
 * 
 * @author Nick Michailidis
 * @version Version 1.0 - 15/02/2010
 */
public class Person{
	    private String name;
    	private String surname;
		private String tel;
	    private String zip;
        private String address;
        private String email;
        private String birthday;
        private String city;
 
 
public Person(String name,String surname,String tel,String zip,String address,String email,String birthday,String city){
 this.name=name;
 this.surname=surname;
 this.tel=tel;
 this.zip=zip;
 this.address=address;
 this.email=email;
 this.birthday=birthday;
 this.city=city;
}
/** part b. 
     * @param no param -
     * @param toString - returns all data to String sample
     */
public String toString(){
	String data="";
	data="name="+getname()+" ";
	data+="surname="+getSurname()+" ";
	data+="tel="+getTel()+" ";
	data+="address="+getAddress()+" ";
	data+="Email="+getEmail()+" ";
	data+="Zip="+getZip()+" ";
	data+="Birthday="+getBirthday()+" ";
	data+="City="+getCity();
	return data;
}
 /** part c. 
     * this method set the name
     * 
     * */ 
public void setname(String name){
  this.name=name;
}
 /** part d. 
     * this method returns the name
     * 
     * */ 
public String getname(){ 
return name;
}
 /** part e. 
     * this method prints the name
     * 
     * */ 
public void printname(){
 System.out.println("Name:"+name);}
  /** part f. 
     * this method sets the surname
     * 
     * */
 
public void setSurname(String surname){
  this.surname=surname;
}
 /** part g. 
     * this method prints the surname
     * 
     * */
public void printSurname(){
  System.out.println("Surname:"+surname);
}
 /** part h. 
     * this method returns the surname
     * 
     * */
public String getSurname(){
    return surname;
}
 /** part f. 
     * this method sets the surname
     * 
     * */
public void setTel(String tel){   
    this.tel=tel;
}
 /** part h. 
     * this method returns the tel
     * 
     * */
public String getTel(){   
    return tel;
}
 /** part g. 
     * this method prints the tel
     * 
     * */
public void printTel(){
    System.out.println("Tel:"+tel);
}
 /** part f. 
     * this method sets the surname
     * 
     * */
public void setAddress(String address){
    this.address=address;
}
 /** part h. 
     * this method returns the address
     * 
     * */
public String getAddress(){   
    return address;
}
 /** part g. 
     * this method prints the address
     * 
     * */
public void printAddress(){
    System.out.println("Address:"+address);
}
 /** part f. 
     * this method sets the surname
     * 
     * */
public void setCity(String city){
    this.city=city;
}
 /** part h. 
     * this method returns the city
     * 
     * */
public String getCity(){   
    return city;
}
 /** part g. 
     * this method prints the city
     * 
     * */
public void printCity(){
    System.out.println("City:"+city);
}
 /** part f. 
     * this method sets the surname
     * 
     * */
public void setEmail(String email){
    this.email=email;
}
 /** part h. 
     * this method returns the email
     * 
     * */
public String getEmail(){   
    return email;
}
 /** part g. 
     * this method prints the email
     * 
     * */
public void printEmail(){
    System.out.println("Email:"+email);
}
 /** part h. 
     * this method returns the zip
     * 
     * */
public String getZip(){
    return zip;
}
 /** part g. 
     * this method prints the zip
     * 
     * */
public void printZip(){
    System.out.println("Zip:"+zip);
}
 /** part f. 
     * this method sets the surname
     * 
     * */
public void setZip(String zip){   
    this.zip=zip;
}
 /** part f. 
     * this method sets the surname
     * 
     * */
public void setBirthday(String birthday){   
    this.birthday=birthday;
}
 /** part h. 
     * this method returns the birthday
     * 
     * */
public String getBirthday(){
    return birthday;
}
 /** part g. 
     * this method prints the birthday
     * 
     * */
public void printBirthday(){
    System.out.println("Birthday:"+birthday);
}
}

Recommended Answers

All 6 Replies

I would suggest to put all of your components JtextFields, Jlabel as global variables and have them all visible.

When you click add, you will take the text from all the fields and add a new person.

When the use clicks search, even though all the fields would be visible, you will take the value only of the 'name' field, search that person and then display its info at the other fields.

The same for delete or update.
One easy solution, like I said is to have all of them visible and depending on what was clicked to take the text only from the fields that you need them.

For showing all of the persons, you can add a JTextArea and when the button 'show all' is clicked you can show all of those persons there.
Since you have overridden the toString method, which is a good thing you can also use a JList to display all the persons.

oh god it's too easy for u to say that, but i am so confused now can u pls explain me in more details? my problem is i don't understand how the computer works when i press the button. what about the code of name and surname . thanks and sorry for the incovinience

public class Agenda1 extends JFrame implements ActionListener {
   searchButton.addActionListener(this);
}

If you want the actionPerformed method to be called when the button is clicked you need to add a class that implements the ActionListener interface:searchButton.addActionListener(this) Agenda1 implements the ActionListener, show you add this.

The method actionPerformed makes sense only if it is implemented by a class that implements the ActionListener. Now when searchButton is clicked the actionPerformed method will execute

nice ! ok as u said right the method will execute! so the programm now should appear two jlabels asking:
Give name : ........ (jtext)
Give: surname:......(jtext):
the code for these two texts and labels where should i write it? inside the actionlistener method? like this?

public class Agenda1 extends JFrame implements ActionListener {
   
      searchButton.addActionListener(this);
give name=new jlabel ("Give name:  ")
contetpane.add();
   
      }

One suggestion was to display everything and get the value you want from them according to what was clicked.

But given your questions, it is best to search some tutorials about gui and try simpler things.

i read many tutorials and i understood many things so i ahve a problem now. this is my new code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener.*;
public class GUI {
public static void main(String []args)
{
  JFrame frame=new JFrame("Agenda");
  frame.setVisible(true);

  frame.setSize(500,500);
  JPanel panel=new JPanel();
  frame.add(panel);
  JButton addbutton=new JButton("ADD Contact");
  panel.add(addbutton);
  JButton delbutton=new JButton("Remove Contact");
  panel.add(delbutton);
  JButton findbutton=new JButton("Find Contact");
  panel.add(findbutton);
  JButton upbutton=new JButton("Update Contact");
  panel.add(upbutton);
  JButton exbutton=new JButton("Exit ");
  panel.add(exbutton);
  addbutton.addActionListener(new Action());
}
  static class Action implements ActionListener
{
     public void actionPerfomed    (ActionEvent e)

{
JFrame frame2=new Jframe("Add Person");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label=new JLabel("Give name");
JPanel panel=new JPanel();
frame2.add(panel);
panel.add(label);


}
        
}

       
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.