Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JDialog question

Posted on 2002-07-10
3
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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