Link to home
Start Free TrialLog in
Avatar of hpchong7
hpchong7

asked on

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);
                    }
                }
            }
        });
    }
}

Avatar of hpchong7
hpchong7

ASKER

Thank you very very very very much!
Edited text of question.
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);
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.
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
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.....
JOptionPane.createDialog(Component parentComponent,String title)
???

each JOptionPane dialog is modal

see http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm
ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much!
However....where should I add?In the class CustomDialog below the line setContentPane(...)?
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.
thnak you!