Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error catching and condition statements help needed

Posted on 2011-03-23
6
Medium Priority
?
298 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 2000 total points
ID: 35200333
Instead of

>>JOptionPane.showOptionDialog(null,

try

JOptionPane.showOptionDialog(newFrame


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

705 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