Tuesday, December 18, 2018

Input ID and Password Text Boxes using Java Swing

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class getIdAndPassword {

public String[] getUserDetails() {
String[] getUserDetails = new String[3];
JTextField userId = new JTextField(15);
JPasswordField password = new JPasswordField(15);
password.setEchoChar('*');

//the below piece of code ensures that the user cannot enter greater than 15 characters

userId.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (userId.getText().length() >= 15) {
e.consume();
JOptionPane.showMessageDialog(null, "Maximum characters: 15 ", "Max Limit Reached", JOptionPane.WARNING_MESSAGE);

}
}
});

//the below piece of code ensures that the user cannot enter greater than 15 characters
password.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (password.getPassword().length >= 15) {
e.consume();
JOptionPane.showMessageDialog(null, "Maximum characters: 15 ", "Max Limit Reached", JOptionPane.WARNING_MESSAGE);
}
}
});
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("User-ID:"));
myPanel.add(userId);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("Password:"));
myPanel.add(password);

//set your own image. Default image is "?".
//Remove the image code if you would like to use default image

ImageIcon icon = new ImageIcon("myImage.jpg");
Image image = icon.getImage();
Image newimg = image.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter User-ID and Password",
JOptionPane.OK_CANCEL_OPTION,JOptionPane.NO_OPTION,icon);
if (result == JOptionPane.OK_OPTION) {
getUserDetails[0] = userId.getText();
getUserDetails[1] = String.valueOf(password.getPassword());
}
if (result == JOptionPane.CANCEL_OPTION) {
getUserDetails[0] = " ";
getUserDetails[1] = " ";
getUserDetails[2] = "CANCELLED";
System.out.println("Operation Cancelled");
}
if (result==JOptionPane.CLOSED_OPTION) {
getUserDetails[0] = " ";
getUserDetails[1] = " ";
getUserDetails[2] = "CANCELLED";
System.out.println("Operation Cancelled");
}
return getUserDetails;
}

}


This class will return the string array "getUserDetails".

Create a java main class to test the above class. Copy and paste the below code into your main method and test.

  getIdAndPassword ip=new getIdAndPassword();
  String userDetails[]=ip.getUserDetails();
  if (userDetails[0].trim().isEmpty() || userDetails[1].trim().isEmpty() || userDetails[2]=="CANCELLED")
  {
   if(userDetails[2]=="CANCELLED")
   {
    JOptionPane.showMessageDialog(null, "Aborting application ", "Abort", JOptionPane.WARNING_MESSAGE); 
    return;
   }
   else
   {
    JOptionPane.showMessageDialog(null, "Either User-ID or Password is not provided", "Insuffient Input", JOptionPane.WARNING_MESSAGE);
    return;
   }
  }
  System.out.println("User-ID  : "+userDetails[0]);
  System.out.println("Password : "+userDetails[1]);


The result would be as follows:


Input ID and Password Text Boxes using Java Swing

No comments:

Post a Comment

Featured Post

REXX Skeleton: Submitting jobs through Rexx

Submitting jobs through REXX and reading the spool through REXX gives us immense potential to automate many manual activities in mainframes....