Small Java GUI question

Sharky Forums


Results 1 to 4 of 4

Thread: Small Java GUI question

  1. #1
    Catfish
    Join Date
    Oct 2000
    Posts
    131

    Question Small Java GUI question

    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

  2. #2
    Reef Shark mefisto3's Avatar
    Join Date
    Jul 2001
    Location
    Melbourne, Vic, Australia
    Posts
    429
    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.

  3. #3
    Catfish arcane_III's Avatar
    Join Date
    Apr 2002
    Location
    East Coast, USA
    Posts
    151
    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.

  4. #4
    Catfish
    Join Date
    Oct 2000
    Posts
    131
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •