🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #11981

Re: How to set default ENTER and ESCAPE actions?

#11981
From: Carlos Javier Castro Pena
Date: Wed, 03 Mar 1999 00:00
38 lines
1694 bytes
What I did:


1) Implement :  implements java.awt.event.ActionListener

2) Create the following method:

  public void actionPerformed(ActionEvent e)
  {
    if (e.getActionCommand().equals("ESCAPE"))
keyPressedActivate(KeyEvent.VK_ESCAPE);
    if (e.getActionCommand().equals("ENTER"))
keyPressedActivate(KeyEvent.VK_ENTER);
  }

  /**
  * Performs actions, based on the key pressed.
  */
  private void keyPressedActivate(int e)  {
     switch (e)  {
        case KeyEvent.VK_ESCAPE: cancelButton.doClick(); break;
        case KeyEvent.VK_ENTER:  loginButton.doClick() ; break;
     }
  }

3) Add the following lines:

this.registerKeyboardAction(this,"ENTER",KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),WHEN_IN_FOCUSED_WINDOW);


this.registerKeyboardAction(this,"ESCAPE",KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0),WHEN_IN_FOCUSED_WINDOW);



Is there a better way to do it?

When a textfield gains the focus, the default 'ENTER' does not work , but
'ESCAPE' does. I used the keyPressed event to detect when ENTER is pressed.
Technical Details
Message-ID: <36DDA248.2A756647@ittc.ukans.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36DB4561.F2B1F55C@ittc.ukans.edu> <36dce7d0.2286062@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,3b20cc5014a3aa77
X-Google-Attributes: gid111d45,public
From: Carlos Javier Castro Pena <jcaspen@ittc.ukans.edu>
Subject: Re: How to set default ENTER and ESCAPE actions?
Date: 1999/03/03
Message-ID: <36DDA248.2A756647@ittc.ukans.edu>
X-Deja-AN: 450906735
Content-Transfer-Encoding: 7bit
References: <36DB4561.F2B1F55C@ittc.ukans.edu> <36dce7d0.2286062@forums.borland.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: ITTC - University Of Kansas
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing