Wierd problem with KeyListener and contentPane in applet.

Discussion in 'Java' started by apchar, Feb 2, 2004.

  1. apchar

    apchar Guest

    Consider the following 2 applets. The first one works. The second
    doesnt. It apparently cant get focus. The only difference is the use
    of contentPanes. Do I need to do something special for JContainers
    within contentPanes?
    I'm using Java 1.4.2_02-b03 for linux. Incidently, both work fine on
    Mac OSX.

    /* <applet code="KeyTest2" width="500" height="300"></applet> */
    // this one works
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class KeyTest2 extends JApplet implements KeyListener
    {
    public void init() { addKeyListener(this); }
    public boolean isFocusable() { return true; }
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {
    System.out.println("ding!"); }
    }


    /* <applet code="KeyTest" width="500" height="300"></applet> */
    // this one does not work in Linux.
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class KeyTest extends JApplet
    {
    public void init()
    {
    Container contentPane = getContentPane();
    contentPane.add(new KeyTestPanel());
    }
    }
    class KeyTestPanel extends JPanel implements KeyListener
    {
    public KeyTestPanel() { addKeyListener(this); }
    public boolean isFocusable() { return true; }
    public void keyTyped(KeyEvent e) { System.out.println("ding");
    }
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    }
     
    apchar, Feb 2, 2004
    #1
    1. Advertising

  2. apchar

    Andrew Hobbs Guest

    Not sure why you call it weird?

    "apchar" <> wrote in message
    news:...
    > Consider the following 2 applets. The first one works. The second
    > doesnt. It apparently cant get focus.


    True

    > The only difference is the use
    > of contentPanes. Do I need to do something special for JContainers
    > within contentPanes?


    You proved to yourself in your first example that the Applet frame gets the
    focus by default. Why are you expecting the panel to get the focus in your
    second example.

    Cheers

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    12 Ashover Grove
    Carine W.A.
    Australia 6020

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************
     
    Andrew Hobbs, Feb 2, 2004
    #2
    1. Advertising

  3. apchar

    apchar Guest

    But even when I explicitly set the focus of the JPanel with
    setFocus(true) it still wont respond. Also, replacing JPanel with
    Canvas makes it work. Also, the JPanel has no such trouble with the
    MouseListener. That qualiifies as wierd doesn't it? How do I force the
    JPanel to gain focus? What should I be doing differently?

    "Andrew Hobbs" <> wrote in message news:<401df5d9$0$1749$>...
    > Not sure why you call it weird?
    >
    > "apchar" <> wrote in message
    > news:...
    > > Consider the following 2 applets. The first one works. The second
    > > doesnt. It apparently cant get focus.

    >
    > True
    >
    > > The only difference is the use
    > > of contentPanes. Do I need to do something special for JContainers
    > > within contentPanes?

    >
    > You proved to yourself in your first example that the Applet frame gets the
    > focus by default. Why are you expecting the panel to get the focus in your
    > second example.
    >
    > Cheers
    >
    > Andrew
    >
    >
    > --
    > ********************************************************
    > Andrew Hobbs PhD
    >
    > MetaSense Pty Ltd - www.metasense.com.au
    > 12 Ashover Grove
    > Carine W.A.
    > Australia 6020
    >
    > 61 8 9246 2026
    > metasens AntiSpam @iinet dot net dot au
    >
    >
    > *********************************************************
     
    apchar, Feb 3, 2004
    #3
  4. apchar

    Andrew Hobbs Guest

    "apchar" <> wrote in message
    news:...
    > But even when I explicitly set the focus of the JPanel with
    > setFocus(true) it still wont respond.


    The commands need to be given at the correct step in the sequence otherwise
    the focus will be set back to the frame.

    > Also, replacing JPanel with
    > Canvas makes it work.


    I am not sure about Canvas. That is an AWT component and the AWT components
    probably don't behave and interact in the same way in terms of focus. (Sun
    warn you not to mix the Swing and AWT components since it can have
    unintended consequences).

    > Also, the JPanel has no such trouble with the
    > MouseListener.


    Mouse Listeners do not require focus. Clicking on a component inherently
    indicates which components should recieve the event anyway. In fact if you
    implement a mouse listener on the JPanel, and get it to requestFocus(), then
    clicking on it with the mouse will give it the focus and it will then
    respond to key clicks.

    > That qualiifies as wierd doesn't it? How do I force the
    > JPanel to gain focus? What should I be doing differently?


    Well on my system I can simply press Tab to traverse the focus onto the
    panel and it then responds to key clicks.

    If you want to do it programatically then I would suggest

    http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html

    Cheers

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    12 Ashover Grove
    Carine W.A.
    Australia 6020

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************
     
    Andrew Hobbs, Feb 3, 2004
    #4
    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. Scott Steiner

    Setting insets for a contentPane?

    Scott Steiner, Jul 19, 2005, in forum: Java
    Replies:
    2
    Views:
    6,705
    Vova Reznik
    Jul 19, 2005
  2. Tom Parson
    Replies:
    4
    Views:
    749
    Knute Johnson
    Nov 15, 2005
  3. Replies:
    0
    Views:
    2,353
  4. lm
    Replies:
    10
    Views:
    1,218
    Knute Johnson
    Jun 3, 2006
  5. phil89
    Replies:
    25
    Views:
    1,127
    John B. Matthews
    Jul 6, 2008
Loading...

Share This Page