Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Array of contacts (address book)

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Array of contacts (address book)

    Hi,

    I'm creating a Java Swing application that lets the user enter the usual details one would expect in an address book then click the save button. The current JPanel will then be hidden while another JPanel is shown. This second panel is used for viewing all the contacts added to the address book so will have buttons to flick forward and backward through the book. I have the GUI part done for the new contact JPanel but I'm struggling in getting what is entered, stored into an array for use later when viewing the contacts. I have numerous classes but the ones I think are needed for help with the array is my GUI class and contacts class files so I will include some code snippets for visual aid as a means to point me in the right direction. I thank you in advance for any help provided :-).

    GUI Class -
    public void actionPerformed(ActionEvent e) 
    	{
    		if (e.getSource() == saveButton){
     
    			addContactPanel.setVisible(false);
    			viewAllPanel.setVisible(true);
    		}
    			if (e.getSource() == quitButton){
    			System.exit(0);
    			}
    		}
     
     
    	public static void main(String[] args) {
     
    		GUIClass form = new GUIClass();
    		form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		form.setVisible(true);
     
     
    	}
    	}

    Contacts class -
    import java.io.*;
     
    public class contacts implements Serializable
    {
    	private contacts list[];
    	private int current = 0;
    	private int count;
    	private int maxSize;
    	private contacts outputFile;
     
    	public contacts(int max)
    	{
    		list = new contacts[max];
    		maxSize = max;
    		count = 0;
     
    	}
     
    	public void add (contacts c)
    	{
    		if (!isFull() && !isIn(p))
    		{
    			list[count] =p;
    			count++;
    		}
    		else
    		{
    			System.out.println("Contact list is full!.");
    			System.out.println("");
    		}
    	}
     
    	public boolean isFull()
    	{
    		return count == maxSize;
    	}
     
    	public boolean isEmpty()
    	{
    		return count==0;
    	}
     
    	public boolean isIn(contacts c)
    	{
    		for (int index = 0; index < count; index++)
    		{
    			if(list[index].equals(p))
    			{
    				return true;
    			}
    		}
    		return false;
    	}
     
    	public int getCount()
    	{
    		return count;
    	}
     
    	public contacts currentRecord()
    	{
    		return list[current];
    	}
     
    	public void incrementCurrentPointer()
    	{
    		this.current++;
    		if (current>=count)
    		{
    			current=0;
    		}
     
    	}
     
    	public void decrementCurrentPointer()
    	{
    		this.current--;
    		if(current<0)
    		{
    			current=count-1;
    		}
    	}
     
    	public void displayAll()
    	{
    		for (int index=0; index < count; index++)
    			System.out.println(list[index]);
     
    	}
     
     
    }

    I also have a class with the following code in which was provided for me but I am not entirely sure of its purpose. If anyone could also explain this to me I would be forever grateful.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.Serializable;
     
    public class MainForm extends JFrame implements ActionListener, Serializable 
    {
    contacts book = new contacts(50);
     
    public MainForm() 
    {
    super("Address Book");
    contact contact1= new Person ("Ryan", 'M', new Date (2 ,05, 58));
    contact contact2= new Person ("David", 'M', new Date (17 ,09, 71));
    book.add(contact1);
    book.add(contact2);
    }
    public void actionPerformed(ActionEvent e) {
     
     
    }
    }


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    Hrmm. Is this forum still alive or is it full of people with questions but nobody here to answer them?

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,097
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Array of contacts (address book)

    Quote Originally Posted by smush View Post
    Hrmm. Is this forum still alive or is it full of people with questions but nobody here to answer them?
    People are here, but your original post was looking for general advice rather than a specific question - the latter of which generates responses much quicker. Now back to the problem at hand...
    The contacts class is a bit strange, in that it seems recursive in nature and creates some possibly unused arrays (unless you want to create a web-like contact program where you can add contacts to contacts, that's a while different story). I'd recommend either a) making a new class (named say ContactList) that stores your list of contacts or b) creating an instance list in another class to do so. In addition, as opposed to using an array to store the list, you can use a resizable storage object like ArrayList or OrderedHashSet.
    I also have a class with the following code in which was provided for me but I am not entirely sure of its purpose. If anyone could also explain this to me I would be forever grateful.
    Looks like an incomplete GUI interface to me...extends from a JFrame and implements ActionListener, if you are unfamiliar with any of this see Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    I know I need to use an ArrayList but the problem is I need to get what is entered into the 'New contact' form into an array then access that array on another JPanel but I do not know how I go about adding a contact along with their details into an array and which class to put it in. Does it go in the GUI class? MainForm class or Contacts class?

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    I knew this forum was poor by how the website isn't correctly set-up didn't think it was going to be this bad. I don't think you have enough returning visitors to make a success of this. Save your money and close it down, there are many other more successful forums on the internet already. :-)

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: Array of contacts (address book)

    Recommended reading for smush:
    How To Ask Questions The Smart Way

    Good questions get answered.

    db

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    Quote Originally Posted by smush View Post
    I knew this forum was poor by how the website isn't correctly set-up didn't think it was going to be this bad. I don't think you have enough returning visitors to make a success of this. Save your money and close it down, there are many other more successful forums on the internet already. :-)
    You didn't give a very specific question, you asked people to explain your own code.

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    Yes I did. I said I need an ArrayList but don't know where it should go and how would I get started....

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    Quote Originally Posted by Darryl.Burke View Post
    Recommended reading for smush:
    How To Ask Questions The Smart Way

    Good questions get answered.

    db
    I asked nicely and just wished for help but when people are too full of themselves to help new comers then a forum fails in it's duty.

  10. #10
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,097
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Array of contacts (address book)

    As with any forum of this sort most people who contribute do so out of their own spare time. It should be respected not considered a right. This for free. Fails in its duty? I don't recall when it was this forum's duty or any contributor's duty to spend their free time trying to help. This is a resource...whether it helps you or not, pisses you off or not, solves your problems or not: its just a resource.

    Now, I'm a nice guy and despite the tension being thrown around still willing to help you with your problem. Here's how I would do it: Remove the array from your contacts class its confusing and recursive (in fact I'd rename it Contact so the plural lends no confusion). In your GUI class - which I assume is a JFrame - create an ArrayList of type Contact. You can directly add or remove from the List in your actionPerformed method. If you have other GUI objects that rely on access to the ArrayList, either a) pass a reference of the List to those Object so they can access it or b) make those inner classes of 'GUI class' (in which case they have direct access)...All these suggestions - in my free time after a long day at work, not my duty.
    Last edited by copeg; April 2nd, 2010 at 09:05 PM.

  11. The Following User Says Thank You to copeg For This Useful Post:

    smush (April 4th, 2010)

  12. #11
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    Thank you Copeg. At least someone helps around here.

  13. #12
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array of contacts (address book)

    Quote Originally Posted by copeg View Post
    As with any forum of this sort most people who contribute do so out of their own spare time. It should be respected not considered a right. This for free. Fails in its duty? I don't recall when it was this forum's duty or any contributor's duty to spend their free time trying to help. This is a resource...whether it helps you or not, pisses you off or not, solves your problems or not: its just a resource.

    Now, I'm a nice guy and despite the tension being thrown around still willing to help you with your problem. Here's how I would do it: Remove the array from your contacts class its confusing and recursive (in fact I'd rename it Contact so the plural lends no confusion). In your GUI class - which I assume is a JFrame - create an ArrayList of type Contact. You can directly add or remove from the List in your actionPerformed method. If you have other GUI objects that rely on access to the ArrayList, either a) pass a reference of the List to those Object so they can access it or b) make those inner classes of 'GUI class' (in which case they have direct access)...All these suggestions - in my free time after a long day at work, not my duty.
    I agree with your idea

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 11:49 AM
  2. Replies: 3
    Last Post: December 22nd, 2011, 08:46 AM
  3. How to get your computer name and IP address?
    By JavaPF in forum Java Networking Tutorials
    Replies: 7
    Last Post: December 8th, 2011, 11:36 AM
  4. JSP E-BOOK URGENT PLEASE
    By YUSUFOZTURK in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: December 1st, 2009, 05:58 PM
  5. Help with JAVA (Grade Book)
    By Sara_21 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2009, 09:16 PM

Tags for this Thread