JWindow KeyListener

Discussion in 'Java' started by Max, Oct 29, 2005.

  1. Max

    Max Guest

    I have a JWindow inside a JFrame and I'm trying to read key input. I've
    tried adding key listeners to both the JWindow and the JFrame, but
    neither responds to my input. Any suggestions?
    Max, Oct 29, 2005
    #1
    1. Advertising

  2. Max

    Chris Smith Guest

    Max <> wrote:
    > I have a JWindow inside a JFrame and I'm trying to read key input. I've
    > tried adding key listeners to both the JWindow and the JFrame, but
    > neither responds to my input. Any suggestions?


    What do you mean? You actually tried to add a JWindow as a component in
    a JFrame? That would cause an IllegalArgumentException. So that's
    probably not right. Now, can you tell us what you really did?

    Note that most of the time, adding a KeyListener to a JFrame is a bad
    idea. AWT events don't propogate through the component hierarchy, so
    it's unlikely to have any effect. Instead, you should look up the
    methods in JComponent called getActionMap() and getInputMap(int), and
    use them instead.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Oct 29, 2005
    #2
    1. Advertising

  3. Max

    Max Guest

    myWindowOwner = new JFrame();
    myWindow = new JWindow(myWindowOwner);

    then later on

    myWindowOwner.addKeyListener(this);
    myWindow.addKeyListener(this);

    None of those are able to detect key input. Mouse input works just fine
    with mouse listener.
    Max, Oct 29, 2005
    #3
  4. Max

    Chris Smith Guest

    Max <> wrote:
    > myWindowOwner = new JFrame();
    > myWindow = new JWindow(myWindowOwner);
    >
    > then later on
    >
    > myWindowOwner.addKeyListener(this);
    > myWindow.addKeyListener(this);
    >
    > None of those are able to detect key input. Mouse input works just fine
    > with mouse listener.


    Okay, back to the second part of my response, then. Both JFrame and
    JWindow contain a JRootPane, which contains such things as a content
    pane, a glass pane, etc. If one of those child components has focus
    instead of the top-level window, then the KeyEvent will be lost.
    Instead, use getInputMap and getActionMap to associate your action with
    a KeyStroke, and specify a more generic condition to getInputMap.

    The API docs for getInputMap and getActionMap are in JComponent.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Oct 29, 2005
    #4
  5. Max

    Max Guest

    Ah. Thanks for your help!
    Max, Oct 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AJ
    Replies:
    0
    Views:
    366
  2. AJ

    JWindow HELP!

    AJ, Apr 7, 2004, in forum: Java
    Replies:
    3
    Views:
    572
    Steve W. Jackson
    Apr 8, 2004
  3. Re: JWindow Handle

    , May 24, 2004, in forum: Java
    Replies:
    0
    Views:
    724
  4. Lee

    JWindow on the desktop

    Lee, Oct 6, 2004, in forum: Java
    Replies:
    3
    Views:
    876
    Andrew Thompson
    Oct 6, 2004
  5. Mahdi Dabestani

    keyListener in JWindow

    Mahdi Dabestani, Jul 22, 2006, in forum: Java
    Replies:
    2
    Views:
    523
    Andrew Thompson
    Jul 22, 2006
Loading...

Share This Page