Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error catching and condition statements help needed

Posted on 2011-03-23
6
Medium Priority
?
299 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

772 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