Solved

JDialog question

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 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