Solved

JDialog question

Posted on 2002-07-10
3
252 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 51
web services creation SOAP vs REST 5 57
jdbc error in jsp application 20 64
Which non-HTML GUI front end to use with Java? 3 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 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