Link to home
Start Free TrialLog in
Avatar of g_currier
g_currierFlag for Germany

asked on

Error catching and condition statements help needed

I have two methods that are giving me a bit of trouble.
One method creates a window with labels and text fields (a form)
and the other gets the info from that window and inserts it all as an object to an ArrayList.

The problem I am having is the use of a JOptionDialog.

What I want is for the dialog to pop up if certain fields are empty (in the window) and then, once an acknowledgement is given in the dialog (ok/cancel), the methods act accordingly.

When OK is pressed I want that the dialog window drops and the form window remains to accept input.

If cancel, then the form window is disposed and focus is returned to the main GUI.

Here are the methods

addContact()
    public void addContact() {
        int response;
        try {
            String lName = getTxtLastName();
            String mName = getTxtMiddleName();
            String fName = getTxtFirstName();
            String streetName = getTxtStreet();
            String cityName = getTxtCity();
            String stateName = getTxtState();
            String zipCode = getTxtZipCode();
            String hPhone = getTxtHomePhone();
            String cPhone = getTxtCellPhone();
            //CONSTRUCT OBJECTS
            address = new Address(streetName, cityName, stateName, zipCode);
            friend = new Friend(lName, fName, mName, hPhone, cPhone, streetName, cityName, stateName, zipCode);
            try {
                c.addToList(friend);
                c.setNumContacts();
                getLastContact();
                abg.setHelpText("Contact Added!\n" + abg.toString());
                //entry is added to end of list so, get the last entry and display it
            } catch (Exception e) {
                System.out.println(e);
            }
            
            if (getTxtLastName().isEmpty() && (getTxtHomePhone().isEmpty() || getTxtCellPhone().isEmpty())) {
                response = JOptionPane.showOptionDialog(null,
                        "To add a contact you must at least add\n"
                        + "a Last Name and a Phone Number", "Empty Contact Entry Error",
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
                if (response == JOptionPane.OK_OPTION) {
                } else if (response == JOptionPane.CANCEL_OPTION) {
                    newFrame.dispose();
                    clearAll();
                }
            }
        } catch (NullPointerException npe) {
            System.out.println(npe + " while adding a contact in BookActions.addContact() method.");
            npe.printStackTrace();
        } catch (Exception e) {
            System.out.println("Error in BookActions.addContact().");
            e.printStackTrace();
        }
    }

Open in new window



and the form window
addNew()
    public void addNew() {
        ListenerClass listen = new ListenerClass();
        newFrame = new JFrame("Add New");
        newFrame.setSize(230, 250);
        newFrame.setResizable(false);
        //newFrame.setIconImage(img);
        JPanel centerPane = new JPanel();
        JPanel bottomPane = new JPanel();

        JLabel lblLastname = new JLabel(" Last Name: ");
        txtLastName = new JTextField("", 25);
        centerPane.add(lblLastname);
        centerPane.add(txtLastName);

        JLabel lblFirstName = new JLabel(" First Name: ");
        txtFirstName = new JTextField("", 25);
        centerPane.add(lblFirstName);
        centerPane.add(txtFirstName);

        JLabel lblMiddleName = new JLabel(" Middle NAme: ");
        txtMiddleName = new JTextField("", 25);
        centerPane.add(lblMiddleName);
        centerPane.add(txtMiddleName);

        JLabel lblStreet = new JLabel(" Street Address: ");
        txtStreet = new JTextField("", 25);
        centerPane.add(lblStreet);
        centerPane.add(txtStreet);

        JLabel lblCity = new JLabel(" City: ");
        txtCity = new JTextField("", 25);
        centerPane.add(lblCity);
        centerPane.add(txtCity);

        JLabel lblState = new JLabel(" State: ");
        txtState = new JTextField("", 25);
        centerPane.add(lblState);
        centerPane.add(txtState);

        JLabel lblZipCode = new JLabel(" Zipcode: ");
        txtZipCode = new JTextField("", 25);
        centerPane.add(lblZipCode);
        centerPane.add(txtZipCode);

        JLabel lblHomePhone = new JLabel(" Home phone: ");
        txtHomePhone = new JTextField("", 25);
        centerPane.add(lblHomePhone);
        centerPane.add(txtHomePhone);

        JLabel lblCellPhone = new JLabel(" Cell phone");
        txtCellPhone = new JTextField("", 25);
        centerPane.add(lblCellPhone);
        centerPane.add(txtCellPhone);

        btnAdd = new JButton("Add Contact");
        btnAdd.addActionListener(listen);

        centerPane.setLayout(new GridLayout(0, 2));
        bottomPane.add(btnAdd);

        newFrame.getContentPane().add(centerPane, BorderLayout.CENTER);
        newFrame.getContentPane().add(bottomPane, BorderLayout.SOUTH);
        newFrame.setLocationRelativeTo(null);
        newFrame.setVisible(true);

    }

Open in new window


Accessor and mutator methods are already created and it works without the conditions.  However, mistakenyl adding a blank form throws errors and its pointless to add a blank contact anyway (in my opinion).  I do not want to add "null" to represent an empty field so I thought that adding conditions to avoid this would be appropriate.  Unfortunately I haven't gotten it to work, and have had to revert everything to working status in order to complete the rest of the application.

Any help here would be appreciated.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>Unfortunately I haven't gotten it to work

In what way does it not work?
Avatar of g_currier

ASKER

when I add the method addNew() to the nested condition
response == JOptionPane.OK_OPTION

Open in new window


the dialog box will close but the form is unresponsive
i.e.:
 
public void addContact(){...

if (getTxtLastName().isEmpty() && (getTxtHomePhone().isEmpty() || getTxtCellPhone().isEmpty())) {
                response = JOptionPane.showOptionDialog(null,
                        "To add a contact you must at least add\n"
                        + "a Last Name and a Phone Number", "Empty Contact Entry Error",
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
                if (response == JOptionPane.OK_OPTION) {
                    addNew();
                } else if (response == JOptionPane.CANCEL_OPTION) {
                    newFrame.dispose();
                    clearAll();
                }
            }
...}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I could use some help finishing up what I have to do.  Have you had a chance to read through the specs I posted?

I have only to set up ListIteration, searching and deleting.  Would you mind taking a look at what I have completed so far?
>>Would you mind taking a look at what I have completed so far?


I don't mind, no. What i DO mind is when you ignore suggestions i make ;) I've already shown you how to do the iteration and mentioned that Iterator/ListIterator is not appropriate for your  use case