JOptionPane and JDialog


JOptionPane and JDialog
Hi guys,
I've been scooting around the net trying to find an answer to this problem. Basically I have a JDialog that users enter registration details. If they haven't completed all fields or if the password confirmation is wrong then a JOptionPane.showMessageDialog appears telling users what has happened. However if they were to then click ok in the OptionPane it closes both the option pane and the original Dialog window. Have you guys any idea?
A code snippet below:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Register extends JDialog implements ActionListener {
     //declare components
     JLabel lblHeading;
     JLabel lblUserName;
     JLabel lblUserPwd;
     JLabel lblCnfUserPwd;
     JLabel lblFrstName;
     JLabel lblLstName;
     JLabel lblAge;
     JLabel lblEmpId;
     JLabel lblSex;
     JLabel lblEmail;
     String usrName;
     String strUsrPwd;
     String strCnfPwd;
     String frstName;
     String lstName;
     String age;
     String empid;
     String email;
     String sex;
     Socket toServer;
     ObjectInputStream streamFromServer;
     PrintStream streamToServer;
     JComboBox listSex;
     JTextField txtUserName;
     JPasswordField txtUsrPwd;
     JPasswordField txtCnfUsrPwd;
     JTextField txtFrstName;
     JTextField txtLstName;
     JTextField txtAge;
     JTextField txtEmpId;
     JTextField txtEmail;
     Font f;
     Color r;
     JButton btnSubmit;
     JButton btnCancel;
     public Register() {
          this.setTitle("Registration Form");
        JPanel panel=new JPanel();
          //apply the layout
           panel.setLayout(new GridBagLayout());
           GridBagConstraints gbCons=new GridBagConstraints();
          //place the components
          lblHeading=new JLabel("Please register below");
           Font f = new Font("Monospaced" , Font.BOLD , 12);
          Color c=new Color(0,200,0);
          lblHeading.setForeground(new Color(131,25,38));
          panel.add(lblHeading, gbCons);
          gbCons.gridx = 0;
          gbCons.gridy = 1;
          lblUserName = new JLabel("Enter Username");
          panel.add(lblUserName, gbCons);
          txtUserName=new JTextField(15);
          panel.add(txtUserName, gbCons);
          lblUserPwd=new JLabel("Enter Password ");
          panel.add(lblUserPwd, gbCons);
          gbCons.gridx = 1;
          gbCons.gridy = 2;
          txtUsrPwd = new JPasswordField(15);
          panel.add(txtUsrPwd, gbCons);
          lblCnfUserPwd=new JLabel("Confirm Password ");
          panel.add(lblCnfUserPwd, gbCons);
          txtCnfUsrPwd=new JPasswordField(15);
          panel.add(txtCnfUsrPwd, gbCons);
          lblEmpId=new JLabel("Employee ID");
          panel.add(lblEmpId, gbCons);
          txtEmpId=new JTextField(15);
          panel.add(txtEmpId, gbCons);
          lblFrstName=new JLabel("First Name");
          panel.add(lblFrstName, gbCons);
          txtFrstName=new JTextField(15);
          panel.add(txtFrstName, gbCons);
          lblLstName=new JLabel("Last Name");
          panel.add(lblLstName, gbCons);
          gbCons.gridx = 1;
          gbCons.gridy = 6;
          txtLstName=new JTextField(15);
          panel.add(txtLstName, gbCons);
          lblAge=new JLabel("Age");
          panel.add(lblAge, gbCons);
          txtAge=new JTextField(3);
          panel.add(txtAge, gbCons);
          lblEmail=new JLabel("Email address");
          panel.add(lblEmail, gbCons);
          txtEmail=new JTextField(20);
          panel.add(txtEmail, gbCons);
          lblSex=new JLabel("Sex");
          panel.add(lblSex, gbCons);
          gbCons.gridx = 1;
          String [] sex= {"Male", "Female"};
          listSex = new JComboBox(sex);
          panel.add(listSex, gbCons);
          JPanel btnPanel=new JPanel();
          btnSubmit=new JButton("Submit");
          btnSubmit.addActionListener(this); //add listener to the Submit button
          btnCancel=new JButton("Cancel");
          btnCancel.addActionListener(this); //add listener to the Cancel button
          panel.add(btnPanel, gbCons);
         setDefaultCloseOperation(DISPOSE_ON_CLOSE); //get rid of this window only on closing
         }//end Register()
          public void actionPerformed(ActionEvent ae) {
               Object o = ae.getSource(); //get the source of the event
               if(o == btnCancel)
               if(o == btnSubmit){
                    usrName = txtUserName.getText();
                    strUsrPwd = txtUsrPwd.getText();
                    strCnfPwd = txtCnfUsrPwd.getText();
                    frstName = txtFrstName.getText();
                    lstName = txtLstName.getText();
                    age = txtAge.getText();
                    empid = txtEmpId.getText();
                    email = txtEmail.getText();
                    sex = (String)listSex.getItemAt(0);
                    if ((usrName.length() == 0) || (strUsrPwd.length() == 0) ||
                    (strCnfPwd.length() == 0) || (frstName.length() == 0) ||
                    (lstName.length() == 0) || (age.length() == 0) ||
                    (empid.length() == 0) || (email.length() == 0))
                    "One or more entry is empty. Please fill out all entries.", "Message", JOptionPane.ERROR_MESSAGE);
                    if ((!strUsrPwd.equals(strCnfPwd))){
                         "Passwords do not match. Please try again", "Message", JOptionPane.ERROR_MESSAGE);
try something like this:
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class About extends JDialog {
/** Creates new form About */
public About(JFrame parent) {
Rectangle parentBounds = parent.getBounds();
Dimension size = getSize();
// Center in the parent
int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
setLocation(new Point(x, y));
and from the main dialog:
new About(this).setVisible(true);
Read More: The other 3 answers