JOptionPane and JDialog

Advertisement

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 java.io.*;
import java.net.*;
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
          gbCons.gridx=0;
          gbCons.gridy=0;
          lblHeading=new JLabel("Please register below");
           Font f = new Font("Monospaced" , Font.BOLD , 12);
          lblHeading.setFont(f);
          Color c=new Color(0,200,0);
          lblHeading.setForeground(new Color(131,25,38));
          lblHeading.setVerticalAlignment(SwingConstants.TOP);
          gbCons.anchor=GridBagConstraints.EAST;
          panel.add(lblHeading, gbCons);
          gbCons.gridx = 0;
          gbCons.gridy = 1;
          lblUserName = new JLabel("Enter Username");
          gbCons.anchor=GridBagConstraints.WEST;
          panel.add(lblUserName, gbCons);
          gbCons.gridx=1;
          gbCons.gridy=1;
          txtUserName=new JTextField(15);
          panel.add(txtUserName, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=2;
          lblUserPwd=new JLabel("Enter Password ");
          panel.add(lblUserPwd, gbCons);
          gbCons.gridx = 1;
          gbCons.gridy = 2;
          txtUsrPwd = new JPasswordField(15);
          panel.add(txtUsrPwd, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=3;
          lblCnfUserPwd=new JLabel("Confirm Password ");
          panel.add(lblCnfUserPwd, gbCons);
          gbCons.gridx=1;
          gbCons.gridy=3;
          txtCnfUsrPwd=new JPasswordField(15);
          panel.add(txtCnfUsrPwd, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=4;
          lblEmpId=new JLabel("Employee ID");
          panel.add(lblEmpId, gbCons);
          gbCons.gridx=1;
          gbCons.gridy=4;
          txtEmpId=new JTextField(15);
          panel.add(txtEmpId, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=5;
          lblFrstName=new JLabel("First Name");
          panel.add(lblFrstName, gbCons);
          gbCons.gridx=1;
          gbCons.gridy=5;
          txtFrstName=new JTextField(15);
          panel.add(txtFrstName, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=6;
          lblLstName=new JLabel("Last Name");
          panel.add(lblLstName, gbCons);
          gbCons.gridx = 1;
          gbCons.gridy = 6;
          txtLstName=new JTextField(15);
          panel.add(txtLstName, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=7;
          lblAge=new JLabel("Age");
          panel.add(lblAge, gbCons);
          gbCons.gridx=1;
          gbCons.gridy=7;
          txtAge=new JTextField(3);
          panel.add(txtAge, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=8;
          lblEmail=new JLabel("Email address");
          panel.add(lblEmail, gbCons);
          gbCons.gridx=1;
          gbCons.gridy=8;
          txtEmail=new JTextField(20);
          panel.add(txtEmail, gbCons);
          gbCons.gridx=0;
          gbCons.gridy=9;
          lblSex=new JLabel("Sex");
          panel.add(lblSex, gbCons);
          gbCons.gridx = 1;
          gbCons.gridy=9;
          String [] sex= {"Male", "Female"};
          listSex = new JComboBox(sex);
          listSex.setSelectedIndex(0);
          panel.add(listSex, gbCons);
          JPanel btnPanel=new JPanel();
          btnSubmit=new JButton("Submit");
          btnPanel.add(btnSubmit);
          btnSubmit.addActionListener(this); //add listener to the Submit button
          btnCancel=new JButton("Cancel");
          btnPanel.add(btnCancel);
          btnCancel.addActionListener(this); //add listener to the Cancel button
          gbCons.gridx=0;
          gbCons.gridy=10;
          gbCons.anchor=GridBagConstraints.EAST;
          panel.add(btnPanel, gbCons);
          getContentPane().add(panel);
         setDefaultCloseOperation(DISPOSE_ON_CLOSE); //get rid of this window only on closing
          setVisible(true);
          setSize(450,400);
         }//end Register()
          public void actionPerformed(ActionEvent ae) {
               Object o = ae.getSource(); //get the source of the event
               if(o == btnCancel)
                    this.dispose();
               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))
                    JOptionPane.showMessageDialog(null,
                    "One or more entry is empty. Please fill out all entries.", "Message", JOptionPane.ERROR_MESSAGE);
                    if ((!strUsrPwd.equals(strCnfPwd))){
                         JOptionPane.showMessageDialog(null,
                         "Passwords do not match. Please try again", "Message", JOptionPane.ERROR_MESSAGE);
                    Thanks,
Chris
Repaly
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) {
super(parent,true);
initComponents();
pack();
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