JDialog

Dear sir,

   I've created a program (using swing)that when user click
Reg->Login ,a JDialog box will be shown,and his username and password are asked for enter.However,after the dialog box is poped up,if the user switch to other application and then return to my application,the dialog box disapper and the application is hang(I think it is due to the dialog box is modal.However,for my application,it have to be modal)So,how can I do such that when I switch back to my application,user can continue entering?Thanks!
Here are the codes
.....
JFrame frame = new JFrame("LOGIN");
CustomDialog customDialog =new CustomDialog(frame);
......
if (e.getActionCommand()=="Log In")      
{customDialog.pack();
customDialog.setLocationRelativeTo(frame);
customDialog.setVisible(true);
 String user = customDialog.getValidatedText();
            if (user != null) {......
*******************************
class CustomDialog extends JDialog {
    private String user = null;
    private String pass = null;
    private String magicWord;
    private JOptionPane optionPane;

    public String getValidatedText() {
        return user;
    }

   
public CustomDialog(Frame aFrame) {

        setTitle("LOGIN");

        final String msgString1 = "Username";
        final String msgString2 = "Password";
        final JTextField textField = new JTextField(10);
        final JPasswordField passwordfield = new         JPasswordField(10);
            
            Object[] array = {msgString1, textField,msgString2,passwordfield};

        final String btnString1 = "Login";
        final String btnString2 = "Cancel";
        Object[] options = {btnString1, btnString2};

        optionPane = new JOptionPane(array,
                                    JOptionPane.QUESTION_MESSAGE,
                                    JOptionPane.YES_NO_OPTION,
                                    null,
                                    options,
                                    options[0]);
        setContentPane(optionPane);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {

                    optionPane.setValue(new Integer(
                                        JOptionPane.CLOSED_OPTION));
            }
        });

        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                optionPane.setValue(btnString1);
            }
        });
            passwordfield.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                optionPane.setValue(btnString1);
            }
        });
        optionPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                String prop = e.getPropertyName();

                if (isVisible()
                 && (e.getSource() == optionPane)
                 && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
                     prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
                    Object value = optionPane.getValue();

                    if (value == JOptionPane.UNINITIALIZED_VALUE) {
                        //ignore reset
                        return;
                    }

                    // Reset the JOptionPane's value.
                    // If you don't do this, then if the user
                    // presses the same button next time, no
                    // property change event will be fired.
                    optionPane.setValue(
                            JOptionPane.UNINITIALIZED_VALUE);

                    if (value.equals(btnString1)) {
                            user = textField.getText();
                                        pass = passwordfield.getText();
                                          
                        if (pass.equals("4180")) {
                                          
                                          // we're done; dismiss the dialog
                            setVisible(false);
                        } else {
                            // text was invalid
                            textField.selectAll();
                            JOptionPane.showMessageDialog(
                                            CustomDialog.this,
                                            "Wrong Password",
                                            "Try again",
                                            JOptionPane.ERROR_MESSAGE);
                            user = null;
                        }
                    } else { // user closed dialog or clicked cancel
                        /*dd.setLabel("It's OK.  "
                                 + "We won't force you to type "
                                 + magicWord + ".");*/
                        user = null;
                        setVisible(false);
                    }
                }
            }
        });
    }
}

LVL 2
hpchong7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hpchong7Author Commented:
Thank you very very very very much!
0
hpchong7Author Commented:
Edited text of question.
0
OviCommented:
JDialog(Frame owner, String title, boolean modal)
          Creates a modal or non-modal dialog with the specified title and the specified owner frame.

....So make this your first line in your CustomDialog class constructor :

super(aFrame,"LOGIN",true);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

hpchong7Author Commented:
I am sorry that it fails.Problem still exists.
I think the dialog box should have its icon under the window toolbar when runs,but it did not.
0
mbormannCommented:
ovi is absolutely rigth ,but in a more detailed way.

add lines shown by //***
public CustomDialog(Frame aFrame)
{
                      //***
                      super(aFrame,"LOGIN",true);
                      //***
                      setTitle("LOGIN");

                      final String msgString1 = "Username";
                      final String msgString2 = "Password";
                      //more code
}
for confirmation see
http://forum.java.sun.com/forum?13@68.8qI3aXa5c2A^0@.ee9c042/0
0
hpchong7Author Commented:
Thank you very much,i've understanded more!
However ,actually my parent application is a JApplet.So when I call the constructor,should I write
   super(japplet,"Login",true)
?
//but the complier complains.....
0
mbormannCommented:
JOptionPane.createDialog(Component parentComponent,String title)
???

each JOptionPane dialog is modal

see http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm
0
mbormannCommented:
i think ur problem is now solved ,right? Yup u had a special case in that ur top level component was not a frame,sorry for a moment i thought that u were not understtanding the code.

u shuld have clarified in the beginning itslef then u wouldn't have been stuck like this.
invoke it like creatDialog(this,"LOGIN");

bye and enjoy
:-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hpchong7Author Commented:
Thank you very much!
However....where should I add?In the class CustomDialog below the line setContentPane(...)?
0
mbormannCommented:
Dialog is a Temporary Window for displaying information or                                                    requesting keystrokes. It requires a parent Frame, thus it cannot be used inside an Applet which has no Frame. It can be modal, which means it blocks input to all other Windows until it is dismissed.
0
hpchong7Author Commented:
thnak you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.