TextField pwd = new TextField(someInt);
pwd.setEchoChar('*');
JPasswordField pwd = new JPasswordField(someInt);
pwd.setEchoChar('*');
import java.io.Console;
public class Password
{
public static void main (String args[])
{
Console c = System.console();
String id = c.readLine("ID: ");
char [] pass = c.readPassword("Pass: ");
}
}
redhat said:@Shirish_nagar: Thanks for your reply.
But, I still have a problem...
I worked exactly as the Tutorial on the link said.(I am using Command Line)
But, the loop that adds the mask, always enters an infinite loop, even before the user can enter input.
Please help me with this...
import java.io.Console;
public class Password
{
public static void main (String args[])
{
Console c = System.console();
String id = c.readLine("ID: ");
char [] pass = c.readPassword("Pass: ");
String pasw = new String(pass);
if(id.equals("Hello")&&pasw.equals("World"))
{
System.out.println("\nCorrect.\nWelcome to the Matrix (Cliche'd, I know)");
}
else
{
System.out.println("\nWrong.\nIn Soviet Russia, the Password has YOU.");
}
}
}
#Correct Password try "World"
harsh@harsh-workstation:~$ java Password
ID: Hello
Pass:
Correct.
Welcome to the Matrix (Cliche'd, I know)
#Wrong password try "Wrong"
harsh@harsh-workstation:~$ java Password
ID: Hello
Pass:
Wrong.
In Soviet Russia, the Password has YOU.
# Console doesn't allow echoing. That's being smart. Than letting people count your asterixes (*)
// Demonstrate text field. - Herbert Schildt
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Password" width=380 height=150>
</applet>
*/
public class Password extends Applet
implements ActionListener {
TextField name, pass;
public void init() {
Label namep = new Label("Name: ", Label.RIGHT);
Label passp = new Label("Password: ", Label.RIGHT);
name = new TextField(12);
pass = new TextField(8);
pass.setEchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);
// register to receive action events
name.addActionListener(this);
pass.addActionListener(this);
}
// User pressed Enter.
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
g.drawString("Name: " + name.getText(), 6, 60);
g.drawString("Selected text in name: "
+ name.getSelectedText(), 6, 80);
g.drawString("Password: " + pass.getText(), 6, 100);
}
}
C:\prog\java\pass>java TestApp
Enter password:****************a***********************************************j
************i****************************************************i*************h
*******j***************************h*********************d*********************c
****d****************************j*********************************************f
*******************i************************************k*********k************g
***************************n****************
The password entered is: sudlsdklasjkldjasdwiorjlwejiopds fjhdsfk./asm;ldfwjekul
rhqpwajajkaaskdklsf;ioefiwejfdklfkjdfds;ljfjdsgfjshedskjkslskskskjasidquwiorywer
yuwehrdhsfjldkjlcdjvhjlfhds;klihgfidshfioshaglihgidhighprugoegkldsjklfhdsklcn;ln
ckldshckjldhsfioydhsaifueifew;kljfre;kljtrutopreugidsjgkldj;klfjds;klfjds;ojfoes
ufdajfkdsnvklbdkjbkugeutiret0rkhgfhgjfdslhgdsiahf8oaydfwiejf;klesfldsm;klvcdbkjv
kldjgds;lojfiafjfkesjaf;lasjffknbgireugpjregrejg
C:\prog\java\pass>