Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


kirankumar
java
2 Answer(s)      2 years and 8 months ago
Posted in : Development process


using java how to show password in stars (**). but not in JSP. plz im waiting for ur reply eagerly. thanks in advance.


Advertisement
View Answers

May 4, 2012 at 1:32 PM


Java creating password field

In the given code,a separate thread i.e ShowThread is created to erase the echoed characters as they are being entered, and replaces them with asterisks. This class is then called by main class PasswordField and prompts the user to enter the password. The ShowThread class then put the input with aestricks.

import java.io.*;

class ShowThread implements Runnable {
   private boolean stop;
    public ShowThread(String prompt) {
       System.out.print(prompt);
   }
    public void run () {
      stop = true;
      while (stop) {
         System.out.print("\010*");
     try {
        Thread.currentThread().sleep(1);
         } catch(InterruptedException ie) {
            ie.printStackTrace();
         }
      }
   }
   public void stopMasking() {
      this.stop = false;
   }
}
 class PasswordField {
    public static String readPassword (String prompt) {
      ShowThread et = new ShowThread(prompt);
      Thread mask = new Thread(et);
      mask.start();

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String password = "";

      try {
         password = in.readLine();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
      et.stopMasking();
      return password;
   }
   public static void main(String argv[]) {
      String password = readPassword("Enter password: ");
      System.out.println("The password entered is: "+password);
   }
}


May 4, 2012 at 1:44 PM


In case, if you are creating code in java swing, then you can use the class JPasswordField to create a password in aestricks.

Here is an example that will allow the user to enter username and password using swing components. If user will enter correct username and password 1.e roseindia and roseindia respectively, then it will show you welcome message, otherwise, it will show you error message.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
  Login()
  {
  label1 = new JLabel();
  label1.setText("Username:");
  text1 = new JTextField(15);

  label2 = new JLabel();
  label2.setText("Password:");
  text2 = new JPasswordField(15);

  SUBMIT=new JButton("SUBMIT");

  panel=new JPanel(new GridLayout(3,1));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(SUBMIT);
  add(panel,BorderLayout.CENTER);
  SUBMIT.addActionListener(this);
  setTitle("LOGIN FORM");
  }
 public void actionPerformed(ActionEvent ae)
  {
  String value1=text1.getText();
  String value2=text2.getText();
  if (value1.equals("roseindia") && value2.equals("roseindia")) {
  JOptionPane.showMessageDialog(null,"Welcome "+value1);
  }
  else{
  System.out.println("enter the valid username and password");
  JOptionPane.showMessageDialog(this,"Incorrect login or password",
  "Error",JOptionPane.ERROR_MESSAGE);
  }
}

  public static void main(String arg[])
  {
  try
  {
  Login frame=new Login();
  frame.setSize(300,100);
  frame.setVisible(true);
  }
  catch(Exception e)
  {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  is java purely object oriented language
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  sample code for RARP using java
java
java  RARP implementation using java socket
Java
Java  how to draw class diagrams in java
java
java  explain object oriented concept in java
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  why to set classpath in java
 

 

 

DMCA.com