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 8 of 8

Thread: Please Help?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Please Help?

    Hi all,

    I'm working on this assignment I have due in a few days and I'm stuck. I have to create a simple contacts program where on the left side I input a name and phone number that then gets added to an ArrayList that keeps all the contacts that are entered and have the ability to scroll through contacts entered.

    Here's what it should look like:

    program1.jpg

    EDIT: Just a quick clarification: the trouble I'm having is first and foremost with how to use ArrayLists. I've Googled and looked in my textbook but I'm not understanding how to use one.

    I added the following but I'm not sure that's even correct.
    private ArrayList contactsArrayList = new ArrayList();

    Here's where I'm stuck:

    1) for my "Add Contact" JButton I'm not sure what I should put in the ActionPerformed to take the Name and Phone Number from the JTextFields on the left and add them to the ArrayList and and the respective Name and Phone text fields on the right.

    addContactJButton = new JButton();
          addContactJButton.setBounds( 16, 130, 120, 23 );
          addContactJButton.setText( "Add Contact" );
          container.add( addContactJButton );
          addContactJButton.addActionListener(
     
          new ActionListener() 
             {
                public void actionPerformed( ActionEvent event )
                {
     
                }
     
             } 
     
          );

    2) How do I make my "Back" and "Next" JButtons scroll through the contacts added to the ArrayList?

    backJButton = new JButton();
          backJButton.setBounds( 242, 130, 75, 23 );
          backJButton.setText( "Back" );
          container.add( backJButton );
          backJButton.addActionListener(
     
          new ActionListener() 
             {
                public void actionPerformed( ActionEvent event )
                {
     
                }
          }
     
    );
     
          nextJButton = new JButton();
          nextJButton.setBounds(320,130,75,23);
          nextJButton.setText("Next");
          container.add(nextJButton);
          nextJButton.addActionListener(
     
          new ActionListener() 
             {
                public void actionPerformed( ActionEvent event )
                {
     
                }
          }
     
    );

    Can someone point me in the right direction, please?

    Here's all of my code:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
     
    public class Contacts extends JFrame 
    {
        private JLabel nameJLabel;
        private JTextField nameJTextField;
     
        private JLabel contactNameJLabel;
        private JTextField contactNameJTextField;
     
        private JLabel phoneJLabel;
        private JTextField phoneJTextField;
     
        private JLabel contactPhoneJLabel;
        private JTextField contactPhoneJTextField;
     
        private JButton addContactJButton;
        private JButton backJButton;
        private JButton nextJButton;
     
        private Contact newContact;
        private ArrayList contactsArrayList = new ArrayList();
     
       public Contacts()
       {
          createUserInterface();
       }
     
       public void createUserInterface()
       {
          Container container = getContentPane();
          container.setLayout( null );
     
          nameJLabel = new JLabel();
          nameJLabel.setBounds( 16, 16, 64, 21 );
          nameJLabel.setText( "Name:" );
          container.add(nameJLabel );
     
          nameJTextField = new JTextField();
          nameJTextField.setBounds( 16, 40, 134, 21 );
          nameJTextField.setText( "" );
          container.add( nameJTextField );
     
          contactNameJLabel = new JLabel();
          contactNameJLabel.setBounds( 242, 16, 64, 21 );
          contactNameJLabel.setText( "Name:" );
          container.add( contactNameJLabel );
     
          contactNameJTextField = new JTextField();
          contactNameJTextField.setBounds( 242, 40, 134, 21 );
          contactNameJTextField.setText( "Doe" );
          container.add( contactNameJTextField );
     
          phoneJLabel = new JLabel();
          phoneJLabel.setBounds( 16, 70, 40, 21 );
          phoneJLabel.setText( "Phone:" );
          container.add( phoneJLabel );
     
          phoneJTextField = new JTextField();
          phoneJTextField.setBounds( 16, 92, 112, 21 );
          phoneJTextField.setText( "" );
          container.add( phoneJTextField );
     
          contactPhoneJLabel = new JLabel();
          contactPhoneJLabel.setBounds( 242, 70, 50, 21 );
          contactPhoneJLabel.setText( "Phone: " );
          container.add( contactPhoneJLabel );
     
          contactPhoneJTextField = new JTextField();
          contactPhoneJTextField.setBounds( 242, 92, 176, 21 );
          contactPhoneJTextField.setText( "" );
          container.add( contactPhoneJTextField );
     
          addContactJButton = new JButton();
          addContactJButton.setBounds( 16, 130, 120, 23 );
          addContactJButton.setText( "Add Contact" );
          container.add( addContactJButton );
          addContactJButton.addActionListener(
     
          new ActionListener() 
             {
                public void actionPerformed( ActionEvent event )
                {
     
                }
     
             } 
     
          ); 
     
          backJButton = new JButton();
          backJButton.setBounds( 242, 130, 75, 23 );
          backJButton.setText( "Back" );
          container.add( backJButton );
          backJButton.addActionListener(
     
          new ActionListener() 
             {
                public void actionPerformed( ActionEvent event )
                {
     
                }
          }
     
    );
     
          nextJButton = new JButton();
          nextJButton.setBounds(320,130,75,23);
          nextJButton.setText("Next");
          container.add(nextJButton);
          nextJButton.addActionListener(
     
          new ActionListener() 
             {
                public void actionPerformed( ActionEvent event )
                {
     
                }
          }
     
    );
          setTitle( "Contacts" ); 
          setSize( 472, 280 );        
          setVisible( true );      
     
       } 
     
     
       public static void main( String[] args )
       {
          Contacts application = new Contacts();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
       } 
     
        private static class Contact {
     
            public Contact() {
            }
        }
     
    }
    Last edited by texasPI; December 1st, 2013 at 01:23 AM. Reason: Got rid of GUI created stuff and manually created it instead.

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Please Help?

    Most here prefer to code what you've done so far "by hand" rather than using Netbeans' GUI Builder. Your question isn't really "How do I code this in Java?" but "How do I use the GUI Builder?" and not many here can or will help with that, because it's not a Java question.

    Someone able to help may come along, but don't be surprised if the responses are few.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please Help?

    Thank you. That's what I figured. I may just do it by hand since the GUI builder confuses me.

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    Most here prefer to code what you've done so far "by hand" rather than using Netbeans' GUI Builder. Your question isn't really "How do I code this in Java?" but "How do I use the GUI Builder?" and not many here can or will help with that, because it's not a Java question.

    Someone able to help may come along, but don't be surprised if the responses are few.
    Ok, I went ahead and got rid of the stuff I did with the Netbeans GUI builder. I re-did it by hand. Hopefully someone can help me.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Please Help?

    And I should have requested that in the future you write better thread titles. Everyone wants help but no one will find this topic by searching for "contact list app" or similarly useful searches because your thread title won't support it.

    That took some effort. Good job. I've noticed some things in the code I'd recommend you change, but let's work on your questions first.

    1) Using ArrayList to add contact info. I recommend you create another class to contain the contact info. (For discussion, I'll call it ContactInfo.) It'd be real simple, just a couple fields: name and phoneNumber. Then when the add contact button is pressed, the info from the text fields will be used to create a new instance of ContactInfo and the new instance will be added to an ArrayList<ContactInfo>. The code pieces you'll need include the following (not necessarily in the right order, place, or using the correct names):
    // an ArrayList to hold ContactInfo objects
    List<ContactInfo> contactInfoList;
     
    // then in the constructor
    contactInfoList = new ArrayList<ContactInfo>();
     
    // then when the add contacts button is pressed
    contactInfoList.add( new ContactInfo( nameTextField.getText(), phoneTextField.getText() ) );

    2. Scrolling through ArrayList with Next/Back buttons: This might be easier to imagine using a simple Array as an example: create a new field, int currentIndex, initialize it to zero. Assuming each new contact is displayed on the right side of your GUI when it is created, increment currentIndex and display the name and phone number on the right side each time a new contact is added. Then when next is pressed, currentIndex is incremented, the object from the array at the new currentIndex is retrieved, and the name/phoneNumber from that object is displayed in the text fields on the right side. The code will react similarly when the back button is pressed, but the currentIndex is decremented. In a simple Array, you would have to wrap from index 0 to the end of the array when back is pressed and from the end of the array to index 0 when next is pressed. Using an ArrayList of ContactInfo objects is similar to using an Array, but different. Start by reading the ArrayList API page for the available methods, code what you can, and then come back for help. You'll also have to "reset" the currentIndex to the next available ArrayList<ContactInfo> index each time the add contact button is pressed, but that's getting into the finer (but important) details.

    That's enough to think about and work on for now.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    texasPI (December 1st, 2013)

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please Help?

    Thanks for the help! It's starting to make sense. I'm a little confused about creating another class for ContactInfo.

    Would I do something like:
    public class ContactInfo
    {
    nameJTextField;
    phoneJTextField;
    }

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Please Help?

    The ContactInfo class looks more like:
    public class ContactInfo
    {
        private String name;
        private String phoneNumber;
     
        // a constructor that accepts name and phone number parameters
     
     
        // appropriate getters and setters
     
    } // end class ContactInfo

  8. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please Help?

    I really appreciate your help, Greg. I'm beginning to understand this better.

    So I created the new public class for ContactInfo. I now have two files, my original Contacts.java and ContactInfo.java. That sound right?

    Here's what I did with ContactInfo

    public class ContactInfo {
        private String name;
        private String phoneNumber;
     
        public final void setName( String n )
        {
            name = n;
        }
     
        public void setPhone (String p)
        {
            phoneNumber = p;
        }
     
        public String getName( )
        {
            return name;
        }
     
        public String getPhone( )
        {
            return phoneNumber;
        }
     
        public ContactInfo (String n, String p)
        {
            setName(n);
            setPhone(p);
        }
     
    }

    I'm still not sure where I would insert the arraylist syntax you suggested.

    // an ArrayList to hold ContactInfo objects
    List<ContactInfo> contactInfoList;
     
    // then in the constructor
    contactInfoList = new ArrayList<ContactInfo>();

    I tried this to add the ArrayList but it's not working either:

        private String[] contacts = {"name", "phone"};
        private Contact newContact;
        private ArrayList contactsArrayList = new ArrayList();
        private int position = 0;

    Here's my Add Contact button:

    private void addContactJButtonActionPerformed( ActionEvent event )
       {
     
            String inputName = nameJTextField.getText();
            String inputPhone = phoneJTextField.getText();
            contactNameJTextField.setText(inputName);
            contactPhoneJTextField.setText(inputPhone);
            contactsArrayList.add( new ContactInfo ( nameJTextField.getText(), phoneJTextField.getText() ) );
     
     
       }

    Here's what I did with the back and next buttons to scroll through contacts added to the ArrayList. They're not doing anything right now because I don't think anything is being added to the ArrayList.

    private void backJButtonActionPerformed (ActionEvent event)
       {
          if (position > 0)
          {
              position--;
          }
          else
          {
              position = contactsArrayList.size() -1;
          }
       }
     
       private void nextJButtonActionPerformed (ActionEvent event)
       {
          if (position < contactsArrayList.size() -1)
          {
              position++;
          }
          else
          {
              position = 0;
          }
     
       }

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Please Help?

    This is an unorthodox constructor:
    public ContactInfo (String n, String p)
    {
        setName(n);
        setPhone(p);
    }
    This is an orthodox constructor:
    public ContactInfo (String name, String phoneNumber)
    {
        this.name = name;
        this.phoneNumber = phoneNumber;
    }
    I don't see where you're obtaining the name and phoneNumber from the contactInfo instance stored in the ArrayList pointed to by the currentIndex and setting the text fields to those values. It should be something like (fix the textfield names to those on the right):
    (untested code, but close
    nameTextField.setText( contactList.get( currentIndex ).getName() );
    phoneNumberTextField.setText( contactList.get( currentIndex ).getPhoneNumber() );

Tags for this Thread