Solved

Error catching and condition statements help needed

Posted on 2011-03-23
6
289 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 500 total points
ID: 35200333
Instead of

>>JOptionPane.showOptionDialog(null,

try

JOptionPane.showOptionDialog(newFrame


0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now