-2

I am trying to create a simple login system and i want the user to be able to proceed to the next part when they hit the enter button, and ive managed to get it working on the registration part, so that when the user has finished putting in their details they can hit enter and the program goes to the next part but i want it so that when the user is entering their login details they can hit enter and then the program will execute the next part of the code.

    card3 = new JPanel();
    usernameFieldone = new JTextField(10);
    usernameLabel = new JLabel("Username:");
    usernameLabel.setLabelFor(usernameFieldone);
    passwordFieldone = new JPasswordField(10);
    passwordLabel = new JLabel("Password:");
    passwordLabel.setLabelFor(passwordFieldone);
    passwordFieldre = new JPasswordField(10);
    passwordLabelre = new JLabel("Confirm Password: ");
    passwordLabelre.setLabelFor(passwordFieldre);
    passwordFieldre.addKeyListener(this);
    OKButton = new JButton("OK");
    OKButton.addActionListener(this);
    backButton = new JButton("Back");
    backButton.addActionListener(this); 

public void keyReleased(KeyEvent e){
    CardLayout cl = (CardLayout)(cards.getLayout());

    if(e.getKeyCode()==KeyEvent.VK_ENTER){
        if(
        Register();
    }
}`
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

Here is an exemple:

(...)
//register a Key listener event
myComponent.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            enter_Pressed(evt);
        }
    });

(...)

private void enter_Pressed(java.awt.event.KeyEvent evt){                                        
    //if ENTER is pressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        //do someting
    }
}
nms
  • 325
  • 1
  • 14
  • 2
    No, `KeyListener` is for low-level handling. He should rather use Keybindings. And maybe, he could simply use JRootPane default button if he has some kind of "OK" button. – Guillaume Polet Dec 18 '12 at 14:41
  • ive got the program to execute the next part of the code when the enter button is pressed but i want it to do multiple actions when the enter key is pressed, for example, when they have typed in their registration details they can hit enter and it executes the next part of the code, but i want it so that when they are logging in they can hit enter also and it will enter the system instead of doing the peice of code already specified when they hit enter on the registration – NightStorm Dec 18 '12 at 14:47