The Artima Developer Community
Sponsored Link

Java Answers Forum
Address Book Help

3 replies on 1 page. Most recent reply: Apr 21, 2003 4:37 PM by Singh M.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Address Book Help Posted: Apr 19, 2003 6:02 PM
Reply to this message Reply
Advertisement
I am trying to make an address book that stores the normal stuff. I have everything done except I can't get the buttons to work, ie, Add, Delete, Search, View. If you can take some time and look over my code and help me out it would be greatly appreciated...Thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;

public class AddressBook
{
	// Use these class variables to store values for searching
	private Person person[] = new Person[50];
	private String guiLastName;
	private String guiFirstName;
	private int curpos;
 
	public AddressBook()
	{
		buildGui();
	} // end AddressBook constructor
 
	public TextField firstName,
					 lastName,
					 phone,
					 email,
					 city,
					 state,
					 zip;
 
	public Component buildGui()
	{
 
		final JLabel firstLabel = new JLabel("First Name");
		final JLabel lastLabel = new JLabel("Last Name");
		final JLabel phoneLabel = new JLabel("Phone Number");
		final JLabel emailLabel = new JLabel("E-mail Address");
		final JLabel cityLabel = new JLabel("City");
		final JLabel stateLabel = new JLabel("State");
		final JLabel zipLabel = new JLabel("Zip");
		final JButton add = new JButton(new AddAction());
		final JButton delete = new JButton(new DeleteAction());
		final JButton search = new JButton(new SearchAction());
		final JButton view = new JButton("View Address Book");
		firstName = new TextField(15);
		lastName = new TextField(15);
		phone = new TextField(15);
		email = new TextField(15);
		city = new TextField(15);
		state = new TextField(15);
		zip = new TextField(15);
		JPanel pane1 = new JPanel();
		pane1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)
		);
		pane1.setLayout(new GridLayout(10, 2, 10, 10));
		pane1.add(firstLabel);
		pane1.add(firstName);
		pane1.add(lastLabel);
		pane1.add(lastName);
		pane1.add(phoneLabel);
		pane1.add(phone);
		pane1.add(emailLabel);
		pane1.add(email);
		pane1.add(cityLabel);
		pane1.add(city);
		pane1.add(stateLabel);
		pane1.add(state);
		pane1.add(zipLabel);
		pane1.add(zip);
		pane1.add(add);
		pane1.add(delete);
		pane1.add(search);
		pane1.add(view);
 
		return pane1;
 
		// put addressbook gui componenets in here
		// create frame/panels etc..
		// example
 
	} // end buildGui
 
	public static void main(String args[])
	{
	try
		{
			UIManager.setLookAndFeel(
			UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) { }
 
		//Create the top-level container and add contents to it.
		JFrame frame = new JFrame("AddressBook");
		AddressBook app = new AddressBook();
		Component contents = app.buildGui();
		frame.getContentPane().add(contents, BorderLayout.CENTER);
		//Finish setting up the frame, and show it.
 
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		frame.pack();
		frame.setVisible(true);
	} // end main method
 
	private int search(String s)
	{
		//JOptionPane.showMessageDialog(null, "Seach Was Clicked!");
		int i= 0;
		//put search array here - return the value of index of found item or null address
		while (!person[i].getLastName().equals(guiLastName) && person[i] != null)
		{
			i++;
		}
		return i;
	}
 
	private class SearchAction extends AbstractAction
	{
		public SearchAction()
		{
			putValue(NAME, "Search");
			putValue(SHORT_DESCRIPTION, "Search");
		}
		public void actionPerformed(ActionEvent e)
		{
			int i = 0;
			long z = 0;
			curpos = search(guiLastName);
		while (!person[i].getLastName().equals(guiLastName) && person[i] != null)
			{
				i++;
			}
			firstName.setText(person[i].getFirstName());
			lastName.setText(person[i].getLastName());
			phone.setText(person[i].getPhone());
			email.setText(person[i].getEmail());
			city.setText(person[i].getCity());
			state.setText(person[i].getState());
			phone.setText(person[i].getPhone());
			z = person[i].getZip();
			zip.setText(Long.toString(z));
			// put code for searching here using curpos as index position
		}
	}
	private class AddAction extends AbstractAction
	{
 
		public AddAction()
		{
			putValue(NAME, "Add");
			putValue(SHORT_DESCRIPTION, "Add");
		}
		public void actionPerformed(ActionEvent e)
		{
			//JOptionPane.showMessageDialog(null, "Add Was Clicked!");
			curpos = search(guiLastName);
			person[curpos] = new Person();
			// put code adding here
		}
	}
	private class DeleteAction extends AbstractAction
	{
 
		public DeleteAction()
		{
			putValue(NAME, "Delete");
			putValue(SHORT_DESCRIPTION, "Delete");
		}
		public void actionPerformed(ActionEvent e)
		{
			//JOptionPane.showMessageDialog(null, "Delete Was Clicked!");
			curpos = search(guiLastName);
			person[curpos] = null;
			// put code for deleting here
		}
	}
} // end AddressBook class
 
class Person
{
	private String firstName;
	private String lastName;
	private String email;
	private String city;
	private String state;
	private String phoneNumber;
	private long zip;
 
	private String firstNam;
	private String lastNam;
	private String mail;
	private String city1;
	private String stat;
	private String phoneNumb;
	private long zip1;
 
 
 
	//My constructor for the person class
	public Person(String a, String b, String c,
	String e, String f, String g, long h)
	{
		setFirstName(a);
		setLastName(b);
		setEmail(c);
		setCity(e);
		setState(f);
		setPhone(g);
		setZip(h);
	}
		public Person()
		{
		}
	//These are my methods to try to get and send the text you enter
	public String getFirstName()
	{
		return firstNam;
	}
 
	public void setFirstName(String firstNam)
	{
		firstName = firstNam;
	}
 
	public String getLastName()
	{
		return lastNam;
	}
 
	public void setLastName(String lastNam)
	{
		lastName = lastNam;
	}
 
	public String getEmail()
	{
		return mail;
	}
 
	public void setEmail(String mail)
	{
		email = mail;
	}
 
	public String getCity()
	{
		return city1;
	}
 
	public void setCity(String city1)
	{
		city = city1;
	}
 
	public String getState()
	{
		return stat;
	}
 
	public void setState(String stat)
	{
		state = stat;
	}
 
	public String getPhone()
	{
		return phoneNumb;
	}
 
	public void setPhone(String phoneNumb)
	{
		phoneNumber = phoneNumb;
	}
 
	public long getZip()
	{
		return zip1;
	}
 
	public void setZip(long zip1)
	{
		zip = zip1;
	}
}


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Address Book Help Posted: Apr 20, 2003 3:30 PM
Reply to this message Reply
To make the buttons work, you will need to implement the ActionListener...

private class SearchAction extends AbstractAction implements ActionListener


This will make the search button work. You can do the same for other buttons.

AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Re: Address Book Help Posted: Apr 20, 2003 11:44 PM
Reply to this message Reply
After i changed what you told me to it still does not seem to work for me...is there something else that i need to do as well?

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Address Book Help Posted: Apr 21, 2003 4:37 PM
Reply to this message Reply
Well, I said that the program will work. I did not say that it will execute correctly. I downloaded your source code and found out that there are too many mistakes in your program.

for eg.

in the actionPerformed method of AddAction, you are doing a search :
curpos = search(lastName.getText());


Your search method throws NullPointerException all the time,
and there are plenty of other issues. It will be better if you reorganize your code and have a clear objective in your mind as to what you want to achieve.

Good luck.

MS

Flat View: This topic has 3 replies on 1 page
Topic: calculate reflection Previous Topic   Next Topic Topic: Writing to an existing file without changing the contents

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use