Reputation: 5259
Can anyone help me with this code as I am a newbie in Java. I found this code on the web and I want to understand what it does?
pass = new JPasswordField(10);
pass.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_ENTER){
OKButton.doClick();
}
}
@Override
public void keyPressed(KeyEvent e) {
//Do Nothing
}
@Override
public void keyReleased(KeyEvent e) {
//Do Nothing
}
});
As I understand it creates a text where everything I type is not visible, and I see bullets instead. Whats the purpose of the KeyListener? To identify the letters pressed?
Upvotes: 1
Views: 5328
Reputation: 49577
This if(e.getKeyChar()==KeyEvent.VK_ENTER)
checks whether the Key pressed
is ENTER
key or not.
If user pressed ENTER KEY
the java code automatically presses OK Button
.
For further understanding take a look at How to Write a Key Listener.
Upvotes: 1
Reputation: 19
This code describes a field in which the user would put in a password. The point of the implementation of the KeyListener interface is to check if the user has pressed the Enter key on their keyboard. If the user does, the program clicks the OK button for him.
More on KeyListener: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html
Upvotes: 1