🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #18290

Re: Capturing keyevent

#18290
From: Chris Hill
Date: Thu, 16 Jul 1998 00:00
50 lines
1930 bytes
I would add the same KeyListener to every control on the panel.  With
40 controls, I would probably do this by hand rather than with the
designer to prevent many unnecessary classes (and instances at
runtime) from being generated.

    KeyListener kl = new java.awt.event.KeyAdapter()
    {
      public void keyPressed(KeyEvent e)
      {
        // common processing code here
        // respond(); is what you indicated in the first example
      }
    };

    button1.addKeyListener(kl);
    button2.addKeyListener(kl);
    // etc...

On Thu, 16 Jul 1998 16:17:47 +0200, Dag Welinder
<dag.x.welinder@telia.se> wrote:

>I wrote an applet with a panel containing lots of components (panels,
>buttons...) and I want the whole panel (or the whole applet) to respond
>when the enter-key is pressed. I've tried 
>
>class myApplet xtends Applet implements KeyListener {
>...
>public void keyPressed (KeyEvent e) {
>	respond ();
>}
>...
>}
>
>but it won't work. I also tried to add an keyListener to the panel (and
>the applet) with
>
>panel1.addKeyListener (new KeyAdapter() {
>        public void keyPressed (KeyEvent e) {
>          System.out.println ("Knapp tryckt.");
>        }
>      });
>
>with no positive effect... Do I have to add keyListeners to every single
>component in my panel (>40 of them>, since it seems like only the one
>with focus can respond, or how would I solve it?
>
>Regards, Dag Welinder

Chris Hill
hillca@cs.purdue.edu
Technical Details
Message-ID: <35ae219f.123229384@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <35AE0B8B.186C89C1@telia.se>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,d272ee6b40f022d
X-Google-Attributes: gidf74b3,public
From: hillca@cs.purdue.edu (Chris Hill)
Subject: Re: Capturing keyevent
Date: 1998/07/16
Message-ID: <35ae219f.123229384@forums.borland.com>
X-Deja-AN: 372051634
References: <35AE0B8B.186C89C1@telia.se>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language