Click to See Complete Forum and Search --> : Small Java GUI question
Evan8
04-10-2002, 10:24 PM
sup all,
i have a small Dialog box with a JPasswordField, so the user can type in a password. How do i set it so that after they type in their password and press ENTER, it generates an ActionEvent (or just calls another function i have)? I can't figure it out!!
Thanks!
-Evan
mefisto3
04-10-2002, 10:59 PM
you need to add an action listener to your button, eg:
class FormActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
// if click on ENTER
if (obj == enterButton)
{
doWhatever();
}
}
}
this listener class should be a class in your jdialog.
then in the function where you add buttons, u will need:
enterButton.addActionListener(FormActionListener);
Hope this helps.
arcane_III
04-11-2002, 11:24 AM
If you have buttons on the form you need an actionlistener as mefisto3 said. You then set up the actionlistener as said and an actionevent will be sent when the user clicks the button. If you want it to work when they press enter all you do is add the following line in the JDialog subclass that you have created.
getContentPane().setDefaultButton(enterButton);
then whenever the user hits enter it will be as if they had clicked the button.
If you are not using buttons, and just using textfields all you have to do is set up the actionlistener for the field.
myPasswordField.setActionCommand("ENTER");
myPasswordField.addActionListener(this);
and then define JDialog as:
PasswordDialog extends JDialog implements ActionListener
and set up the actionPerformed function.
Hope this helps.
Evan8
04-11-2002, 12:23 PM
Thanks for all the help.
I got it to work by adding a KeyListener to the passwordField, but adding an ActionListener and setting its action command is much simpler and easier
Thanks again!
-ev