Solved

JDialog question

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
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:

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now