• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Login Dialog box using frames...

can somebody provide me the sample code for login dialog box using frames.i want both the dialog box and the listener action as well i,e capturing the user name and password .Also how to show a message in the same dialog box if the user leaves one of the fields blank.
0
anuj_roy
Asked:
anuj_roy
1 Solution
 
Venci75Commented:
try this
0
 
Venci75Commented:
import java.awt.*;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
public class LoginBox extends Frame {
  Label message = new Label();
  TextField user = new TextField();
  XYLayout xYLayout1 = new XYLayout();
  TextField pwd = new TextField();
  Button ok = new Button();
  Button cancel = new Button();

  public LoginBox() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    message.setText("Enter username/password");
    this.setLayout(xYLayout1);
    ok.setLabel("ok");
    ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ok_actionPerformed(e);
      }
    });
    cancel.setLabel("cancel");
    cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cancel_actionPerformed(e);
      }
    });
    pwd.setEchoChar('*');
    this.add(user,    new XYConstraints(38, 39, 151, -1));
    this.add(message,  new XYConstraints(37, 8, 180, -1));
    this.add(pwd,  new XYConstraints(38, 68, 151, -1));
    this.add(ok,   new XYConstraints(35, 98, 67, -1));
    this.add(cancel,   new XYConstraints(118, 98, -1, -1));
  }
  void ok_actionPerformed(ActionEvent e) {
      if (user.getText() == null || user.getText().length() == 0) {
          message.setText("The user field is empty");
          return;
      }
      if (pwd.getText() == null || pwd.getText().length() == 0) {
          message.setText("The password field is empty");
          return;
      }
      this.hide();
  }

  void cancel_actionPerformed(ActionEvent e) {
      this.hide();
  }
}
0
 
anuj_royAuthor Commented:
thanx venci for u'r reply but i want to use the dialogbox class for the problem.can u provide the code for the same using jdialogbox
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
s_lavieCommented:
Homework???
0
 
Venci75Commented:
import javax.swing.*;
import javax.swing.event.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class LoginBox extends JFrame {
  JLabel message = new JLabel();
  JTextField user = new JTextField();
  XYLayout xYLayout1 = new XYLayout();
  JPasswordField pwd = new JPasswordField();
  JButton ok = new JButton();
  JButton cancel = new JButton();

  public LoginBox() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    message.setText("Enter username/password");
    this.getContentPane().setLayout(xYLayout1);
    ok.setLabel("ok");
    ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ok_actionPerformed(e);
      }
    });
    ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ok_actionPerformed(e);
      }
    });
    cancel.setLabel("cancel");
    cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cancel_actionPerformed(e);
      }
    });
//    pwd.setEchoChar('*');
    this.getContentPane().add(user,    new XYConstraints(38, 39, 151, -1));
    this.getContentPane().add(message,  new XYConstraints(37, 8, 180, -1));
    this.getContentPane().add(pwd,  new XYConstraints(38, 68, 151, -1));
    this.getContentPane().add(ok,   new XYConstraints(35, 98, 67, -1));
    this.getContentPane().add(cancel,   new XYConstraints(118, 98, -1, -1));
  }

  void ok_actionPerformed(ActionEvent e) {
      if (user.getText() == null || user.getText().length() == 0) {
          message.setText("The user field is empty");
          return;
      }
      if (pwd.getText() == null || pwd.getText().length() == 0) {
          message.setText("The password field is empty");
          return;
      }
      this.hide();
  }

  void cancel_actionPerformed(ActionEvent e) {
      this.hide();
  }
}
0
 
Venci75Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Venci75
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 
NetminderCommented:
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now