Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JDialog question

Posted on 2002-07-10
3
Medium Priority
?
269 Views
Last Modified: 2010-03-31
Hi,
in my swing application, when a button is clicked, I would like to pop-up a dialog, asking the user to fill in login and password, if it is right, then close the dialog,
and the parent screen will go to another new screen.

How to do this?
0
Comment
Question by:stanleyhuen
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 800 total points
ID: 7145262
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {
  public test() {
    init();
  }
 
  private void init() {
    setSize(300, 200);
    JButton button = new JButton("Press me!");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        (new PasswordChecker(test.this)).setVisible(true);
      }
    });
    getContentPane().add(button);
    setVisible(true);
  }
 
 
  class PasswordChecker extends JDialog {
    JTextField user;
    JPasswordField password;
    JButton ok;
    JButton cancel;
   
    public PasswordChecker(JFrame parent) {
      super(parent, "User check", true);
      initDlg();
      initListeners();
    }
   
    protected void initDlg() {
      setSize(300, 150);
      Container c = this.getContentPane();
      c.setLayout(new BorderLayout());
     
      user = new JTextField();
      password = new JPasswordField();
      ok = new JButton("Ok");
      cancel = new JButton("Cancel");
     
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(2, 2, 5, 5));
      p.add(new JLabel("User : "));
      p.add(user);
      p.add(new JLabel("Password : "));
      p.add(password);
     
      JPanel p1 = new JPanel();
      p1.add(ok);
      p1.add(cancel);
     
      c.add(p, BorderLayout.CENTER);
      c.add(p1, BorderLayout.SOUTH);
    }
   
    protected void initListeners() {
      ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
       String usr = user.getText();
       String pwd = new String(password.getPassword());
       if(usr.equalsIgnoreCase("test") && pwd.equalsIgnoreCase("test")) {
         JOptionPane.showMessageDialog(test.this, "User accepted!");
         closeDlg();
         return;
       }
          JOptionPane.showMessageDialog(test.this, "Authentication failed!");
     }
      });
      cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
          closeDlg();
     }
      });
    }
   
    protected void closeDlg() {
      setVisible(false);
      dispose();
    }
  }
 
  public static void main(String[] args) {
    new test();
  }
}
0
 

Author Comment

by:stanleyhuen
ID: 7168916
How can I pass a boolean value from the PasswordChecker class to the main class?

I want to do some thing if it is successfully Authentication, do other thing if it is not.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7169427
By implementing something similar to JOptionPane, like (code is not tested but should work fine) :
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {
  public test() {
    init();
  }
 
  private void init() {
    setSize(300, 200);
    final JButton button = new JButton("Press me!");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        button.setText(PasswordChecker.showDialog(test.this));
      }
    });
    getContentPane().add(button);
    setVisible(true);
  }
 
 
  class PasswordChecker extends JDialog {
    static boolean accepted = false;
    JTextField user;
    JPasswordField password;
    JButton ok;
    JButton cancel;
   
    public PasswordChecker(JFrame parent) {
      super(parent, "User check", true);
      initDlg();
      initListeners();
    }
   
    protected void initDlg() {
      setSize(300, 150);
      Container c = this.getContentPane();
      c.setLayout(new BorderLayout());
     
      user = new JTextField();
      password = new JPasswordField();
      ok = new JButton("Ok");
      cancel = new JButton("Cancel");
     
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(2, 2, 5, 5));
      p.add(new JLabel("User : "));
      p.add(user);
      p.add(new JLabel("Password : "));
      p.add(password);
     
      JPanel p1 = new JPanel();
      p1.add(ok);
      p1.add(cancel);
     
      c.add(p, BorderLayout.CENTER);
      c.add(p1, BorderLayout.SOUTH);
    }
   
    protected void initListeners() {
      ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
       String usr = user.getText();
       String pwd = new String(password.getPassword());
       if(usr.equalsIgnoreCase("test") && pwd.equalsIgnoreCase("test")) {
         JOptionPane.showMessageDialog(test.this, "User accepted!");
         accepted = true;
         closeDlg();
         return;
       }
          JOptionPane.showMessageDialog(test.this, "Authentication failed!");
       accepted = false;
     }
      });
      cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
          accepted = false;
          closeDlg();
     }
      });
    }

    public static final boolean showDialog(JFrame parent) {
      accepted = false;
      (new PasswordChecker(parent)).setVisible(true);
      return(accepted);
    }
   
    protected void closeDlg() {
      setVisible(false);
      dispose();
    }
  }
 
  public static void main(String[] args) {
    new test();
  }
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

963 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