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() { } } }