I have this rather complex program that creates a GUI and retrieves the users contact information. He/she can store multiple aspects of a particular contact's information, which utilizes a custom Linked List class. The first and last name of each contact is supposed to be displayed on the right side of the application screen. For some reason, my addJButton is not adding the contact's information to the screen. Instead, I get a laundry list of exceptions. The same goes for my States JComboBox. Any ideas? The program compiles and executes properly, just can't add anything.
import java.util.InputMismatchException; import java.util.Iterator; public class FinalContact extends LinkedList{ public static String fname; public static String min; public static String lname; public static String street; public static String city; public static String state; public static int zip; public static String dob; public static String email; public static String cell; public static String home; //constructor public FinalContact(int _zip) throws Exception { setZip(_zip); } //constructor for properties public FinalContact(String _fname, String _lname, String _min, String _street, String _city, String _state, String _dob, String _email, String _cell, String _home, int _zip) { fname = _fname; min = _min; lname = _lname; street = _street; city = _city; state = _state; zip = _zip; dob = _dob; cell = _cell; home = _home; email = _email; } public String getFName() { return fname; } public static void setFName(String _fname) { fname = _fname; } public static String getMIN() { return min; } public static void setMIN(String _min) { min = _min; } public static String getLName() { return lname; } public static void setLName(String _lname) { lname = _lname; } public static String getCity() { return city; } public static void setCity(String _city) { city = _city; } public static int getZip() { return zip; } public static void setZip(int _zip) throws Exception{ if(zip > 4) { InputMismatchException ex; } else { zip = (char) _zip; } } public static String getDOB(){ return dob; } public static void setDOB(String _dob) { dob = _dob; } public static String getStreet() { return street; } public static void setStreet(String _street) { street = _street; } public static String getState() { return state; } public static void setState(String _state) { state = _state; } public static String getEMail() { return email; } public static void setEMail(String _email) { email = _email; } public static String getCell() { return cell; } public static void setCell(String _cell) { cell = _cell; } public static String getHome(){ return home; } public static void setHome(String _home) { home = _home; } public int compareTo(FinalContact data) { return 0; } public static Iterator iterator() { // TODO Auto-generated method stub return null; } }
public class LinkedList { class Node { private FinalContact data; private Node next; public Node(FinalContact data) { this(data,null); } public Node(FinalContact data, Node node) { this.data = data; next = node; } public FinalContact getData() { return data; } public void setData(FinalContact data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } private Node head; private Node last; private int size = 0; public LinkedList() { head = last = new Node(null); } public void add(FinalContact s) { last.setNext(new Node(s)); last = last.getNext(); size++; } public boolean remove(FinalContact data){ Node current = head.getNext(); Node previous = null; while (current != null) { FinalContact dataOld = current.getData(); if ((dataOld == null && data == null) || (dataOld != null && dataOld.equals(data))) { Node afterRemoved = current.getNext(); if (previous == null) { head.setNext(afterRemoved); } else { previous.setNext(afterRemoved); } if (afterRemoved.getNext() == null) { last = afterRemoved; } size--; return true; } else { previous = current; current = current.getNext(); } } return false; } public int size() { return size; } public boolean isEmpty() { return true; } public FinalContact getData(int index) { if(index <= 0) { return null; } Node current = head.getNext(); for(int i = 1;i < index;i++) { if(current.getNext() == null) { return null; } current = current.getNext(); } return current.getData(); } public boolean contains(String s) { for(int i = 1;i<=size();i++) { if(getData(i).equals(s)) { return true; } } return false; } public String toString() { Node current = head.getNext(); String output = "["; while(current != null) { output += current.getData()+","; current = current.getNext(); } return output+"]"; } public Node getHead() { return head; } public void print(Node n) { if(n == null) { return; }else { System.out.println(n.getData()); print(n.getNext()); } } public void sortList() { Node first, second, temp; first = head; while(first != null) { second = first.next; while (second != null) { if(first.data.compareTo(second.data) < 0) { temp = new Node(null); temp.data = first.data; first.data = second.data; second.data = temp.data; } second = second.next; } } } }
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.TitledBorder; public class FinalDriver extends JFrame { private static final long serialVersionUID = 1L; private JLabel contactListJLabel; private JTextField contactListJTextField; private JLabel FirstNameJLabel; private JTextField FirstNameJTextField; private JLabel MidNameJLabel; private JTextField MidNameJTextField; private JLabel LastNameJLabel; private JTextField LastNameJTextField; private JLabel DateOfBirthJLabel; private JTextField DateOfBirthJTextField; private JLabel StreetJLabel; private JTextField StreetJTextField; private JLabel CityJLabel; private JTextField CityJTextField; private JLabel StateJLabel; private JComboBox StateJComboBox; private JLabel ZIPJLabel; private JTextField ZIPJTextField; private JLabel HomeJLabel; private JTextField HomeJTextField; private JLabel CellJLabel; private JTextField CellJTextField; private JLabel EmailJLabel; private JTextField EmailJTextField; private JPanel StateJPanel; private JComboBox StateComboBox; private JList ContactJList; private JScrollPane ContactJScrollPane; private JButton scanNewJButton; private JButton addJButton; private JButton removeJButton; private JButton editJButton; private JButton updateJButton; private JButton backJButton; private JButton nextJButton; private String[] states = {"AL", "FL" , "KY" , "MS", "NC" , "NY" , "IL" , "GA" , "WY", "WV" , "TN" , "CA" , "IA" , "OK" , "TX" , "ND" , "SD" , "OR" , "AK" , "MN" , "MA" , "DE" }; private FinalContact newFinalContact; //should contain objects entered by user private LinkedList linkedlist = new LinkedList(); private ArrayList arraylist = new ArrayList(); private ArrayList StateArrayList = new ArrayList(); //no-argument constructor public FinalDriver() { createUserInterface(); } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); FirstNameJLabel = new JLabel(); FirstNameJLabel.setBounds(10, 25, 200, 100); FirstNameJLabel.setText("Enter your first name"); contentPane.add(FirstNameJLabel); FirstNameJTextField = new JTextField(); FirstNameJTextField.setBounds(220, 65, 150, 20); FirstNameJTextField.setEditable( false ); contentPane.add(FirstNameJTextField); MidNameJLabel = new JLabel(); MidNameJLabel.setBounds(10, 90, 150, 26); MidNameJLabel.setText("Enter your middle initial"); contentPane.add(MidNameJLabel); MidNameJTextField = new JTextField(); MidNameJTextField.setBounds(220, 93, 30, 20); MidNameJTextField.setEditable( false ); contentPane.add(MidNameJTextField); LastNameJLabel = new JLabel(); LastNameJLabel.setBounds(10, 120, 150, 30); LastNameJLabel.setText("Enter your last name"); contentPane.add(LastNameJLabel); LastNameJTextField = new JTextField(); LastNameJTextField.setBounds(220, 125, 150, 20); LastNameJTextField.setEditable( false ); contentPane.add(LastNameJTextField); DateOfBirthJLabel = new JLabel(); DateOfBirthJLabel.setBounds(10, 150, 150, 32); DateOfBirthJLabel.setText("Enter your date of birth"); contentPane.add(DateOfBirthJLabel); DateOfBirthJTextField = new JTextField(); DateOfBirthJTextField.setBounds(220, 155, 75, 20); DateOfBirthJTextField.setEditable( false ); contentPane.add(DateOfBirthJTextField); StreetJLabel = new JLabel(); StreetJLabel.setBounds(10, 180, 500, 35); StreetJLabel.setText("Enter your street address"); contentPane.add(StreetJLabel); StreetJTextField = new JTextField(); StreetJTextField.setBounds(220, 185, 150, 20); StreetJTextField.setEditable( false ); contentPane.add(StreetJTextField); CityJLabel = new JLabel(); CityJLabel.setBounds(10, 210, 90, 40); CityJLabel.setText("Enter your city"); contentPane.add(CityJLabel); CityJTextField = new JTextField(); CityJTextField.setBounds(220, 220, 150, 20); CityJTextField.setEditable( false ); contentPane.add(CityJTextField); StateJPanel = new JPanel(); StateJPanel.setBounds(470, 10, 220, 440); StateJPanel.setBorder( new TitledBorder("Choose your State from the menu")); StateJPanel.setLayout( null ); contentPane.add( StateJPanel ); ZIPJLabel = new JLabel(); ZIPJLabel.setBounds(10, 240, 120, 40); ZIPJLabel.setText("Enter your zip code"); contentPane.add(ZIPJLabel); ZIPJTextField = new JTextField(); ZIPJTextField.setBounds(220, 250, 70, 20); ZIPJTextField.setEditable( false ); contentPane.add(ZIPJTextField); HomeJLabel = new JLabel(); HomeJLabel.setBounds(10, 278, 190, 28); HomeJLabel.setText("Enter your home phone number"); contentPane.add(HomeJLabel); HomeJTextField = new JTextField(); HomeJTextField.setBounds(220, 280, 80, 20); HomeJTextField.setEditable( false ); contentPane.add(HomeJTextField); CellJLabel = new JLabel(); CellJLabel.setBounds(10, 310, 190, 32); CellJLabel.setText("Enter your cell phone number"); contentPane.add(CellJLabel); CellJTextField = new JTextField(); CellJTextField.setBounds(220, 315, 80, 20); CellJTextField.setEditable( false ); contentPane.add(CellJTextField); EmailJLabel = new JLabel(); EmailJLabel.setBounds(10,340, 190, 40); EmailJLabel.setText("Enter your email address"); contentPane.add(EmailJLabel); EmailJTextField = new JTextField(); EmailJTextField.setBounds(220, 350, 150, 20); EmailJTextField.setEditable( false ); contentPane.add(EmailJTextField); StateJComboBox = new JComboBox( states ); StateJComboBox.setBounds( 70, 30, 70, 21); StateJComboBox.setEnabled( true ); StateJPanel.add( StateJComboBox ); StateJComboBox.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { StateJComboBoxActionPerformed( event ); } } ); ContactJList = new JList(); ContactJScrollPane = new JScrollPane( ContactJList ); ContactJScrollPane.setBounds( 20, 70, 175, 350 ); StateJPanel.add( ContactJScrollPane ); scanNewJButton = new JButton(); scanNewJButton.setBounds( 9, 420, 95, 26 ); scanNewJButton.setText("Scan New"); scanNewJButton.setMnemonic( KeyEvent.VK_S); contentPane.add( scanNewJButton ); scanNewJButton.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent event ) { scanNewJButtonActionPerformed( event ); } } ); addJButton = new JButton(); addJButton.setBounds( 109, 420, 85, 26); addJButton.setText( "Add"); addJButton.setMnemonic( KeyEvent.VK_A ); addJButton.setEnabled( false ); contentPane.add( addJButton ); addJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { addJButtonActionPerformed( event ); } } ); removeJButton = new JButton(); removeJButton.setBounds( 199, 420, 85, 26); removeJButton.setText( "Remove"); removeJButton.setMnemonic( KeyEvent.VK_R ); removeJButton.setEnabled( false ); contentPane.add( removeJButton ); removeJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { addJButtonActionPerformed( event ); } } ); editJButton = new JButton(); editJButton.setBounds( 289, 420, 85, 26); editJButton.setText( "Edit"); editJButton.setMnemonic( KeyEvent.VK_E ); editJButton.setEnabled( false ); contentPane.add( editJButton ); editJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { removeJButtonActionPerformed( event ); } } ); updateJButton = new JButton(); updateJButton.setBounds( 379, 420, 85, 26); updateJButton.setText( "Update"); updateJButton.setMnemonic( KeyEvent.VK_U ); updateJButton.setEnabled( false ); contentPane.add( updateJButton ); updateJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { updateJButtonActionPerformed( event ); } } ); setTitle( "Contact List"); setSize( 700, 600); setVisible( true ); } private void scanNewJButtonActionPerformed( ActionEvent event ) { clearComponents(); setJButtons( false ); addJButton.setEnabled( true ); InformationJPanelEditable( true ); FinalContact.setFName( FirstNameJTextField.getText()); } private void addJButtonActionPerformed( ActionEvent event ) { addJButton.setEnabled ( false ); InformationJPanelEditable( false ); FinalContact.setFName( FirstNameJTextField.getText()); setJButtons( true ); ContactJScrollPane.setToolTipText(FirstNameJTextField.getText()); StateJComboBox.setSelectedIndex ( StateJComboBox.getSelectedIndex()); } private void removeJButtonActionPerformed( ActionEvent event ) { clearComponents(); setJButtons( true ); addJButton.requestFocusInWindow(); } private void editJButtonActionPerformed( ActionEvent event ) { setJButtons( false ); updateJButton.setEnabled( true ); InformationJPanelEditable( true ); StateJComboBox.setSelectedIndex ( StateJComboBox.getSelectedIndex()); } private void updateJButtonActionPerformed( ActionEvent event ) { setFinalContactData(); setJButtons( true ); updateJButton.setEnabled( false ); InformationJPanelEditable( false ); StateJComboBox.setSelectedIndex( StateJComboBox.getSelectedIndex()); } private void StateJComboBoxActionPerformed( ActionEvent event) { String state = (String) StateJComboBox.getSelectedItem(); Iterator finalcontactIterator = FinalContact.iterator(); StateArrayList.clear(); while (finalcontactIterator.hasNext()) { FinalContact currentContact = (FinalContact) finalcontactIterator.next(); } ContactJList.setListData( StateArrayList.toArray()); } private void setFinalContactData() { FinalContact.setFName( FirstNameJTextField.getText()); FinalContact.setLName( LastNameJTextField.getText()); FinalContact.setMIN(MidNameJTextField.getText()); FinalContact.setCity( CityJTextField.getText()); FinalContact.setState(states[StateJComboBox.getSelectedIndex()]); FinalContact.setDOB( DateOfBirthJTextField.getText()); try { FinalContact.setZip( Integer.parseInt(ZIPJTextField.getText())); } catch (NumberFormatException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } FinalContact.setHome(HomeJTextField.getText()); FinalContact.setCell(CellJTextField.getText()); FinalContact.setEMail(EmailJTextField.getText()); } private void clearComponents() { FirstNameJTextField.setText(""); MidNameJTextField.setText(""); LastNameJTextField.setText(""); DateOfBirthJTextField.setText(""); StreetJTextField.setText(""); HomeJTextField.setText(""); CellJTextField.setText(""); CityJTextField.setText(""); EmailJTextField.setText(""); } private void setJButtons( boolean state ) { scanNewJButton.setEnabled( state ); removeJButton.setEnabled( state ); editJButton.setEnabled( state ); if( arraylist.size() == 0) { editJButton.setEnabled( false ); updateJButton.setEnabled( false ); removeJButton.setEnabled( false ); } } private void InformationJPanelEditable( boolean editable ){ FirstNameJTextField.setEditable( editable ); MidNameJTextField.setEditable( editable ); LastNameJTextField.setEditable( editable ); StreetJTextField.setEditable( editable ); CityJTextField.setEditable( editable ); DateOfBirthJTextField.setEditable( editable ); ZIPJTextField.setEditable( editable ); HomeJTextField.setEditable( editable ); CellJTextField.setEditable( editable ); EmailJTextField.setEditable( editable ); } public static void main(String[] args) { FinalDriver application = new FinalDriver(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } public JLabel getContactListJLabel() { return contactListJLabel; } public void setContactListJLabel(JLabel contactListJLabel) { this.contactListJLabel = contactListJLabel; } public JTextField getContactListJTextField() { return contactListJTextField; } public void setContactListJTextField(JTextField contactListJTextField) { this.contactListJTextField = contactListJTextField; } }