Solved

Error catching and condition statements help needed

Posted on 2011-03-23
6
295 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:g_currier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35200127
>>Unfortunately I haven't gotten it to work

In what way does it not work?
0
 

Author Comment

by:g_currier
ID: 35200195
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

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35200333
Instead of

>>JOptionPane.showOptionDialog(null,

try

JOptionPane.showOptionDialog(newFrame


0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Assisted Solution

by:g_currier
g_currier earned 0 total points
ID: 35200450
Looking at the API a little more closely, I figured that this is what I needed (for the moment, anyway):
 
if (getTxtLastName().isEmpty() && (getTxtHomePhone().isEmpty() || getTxtCellPhone().isEmpty())) {
                JOptionPane.showMessageDialog(newFrame,
                        "To add a contact you must at least add\n"
                        + "a Last Name and a Phone Number", "Empty Contact Entry Error",
                        JOptionPane.ERROR_MESSAGE);
               
            }

Open in new window


Although this kind of answers my problem for me, I appreciate that you looked at it.
0
 

Author Closing Comment

by:g_currier
ID: 35230078
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35230221
>>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
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question