Results 1 to 3 of 3
  1. #1
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Question KeyListener works with JFrame but not with JApplet

    First off, I thought that this would go in the AWT/Swing section because it's about the KeyListener in the java.awt.event package. In any case, I'm making a pong game and I'm trying to move towards using Panels more, however, my panel is not detecting the KeyListener. Because of this, I made a standalone version of my game (with a JFrame) and it works just fine. I think it might have to do with focus, but I don't see where I could possibly be going wrong. I'm using the same JPanel class, and the JFrame and JApplet do basically the same thing. They're short, so here they are:

    JFrame class:
    Java Code:
    package gnp;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     * @author Nicolás A. Ortega
     * @copyright DeathsbreedGames
     * @license GNU GPLv3
     * @year 2014
     * 
     */
    public class GNPStandalone extends JFrame {
    	// Basic dimensions
    	public final int WIDTH = 500;
    	public final int HEIGHT = 500;
    
    	private JPanel game;
    
    	public GNPStandalone() {
    		super("GNP2");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game = new Game(WIDTH, HEIGHT);
    		add(game);
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    	}
    
    	public static void main(String[] args) { new GNPStandalone(); }
    }
    JApplet class:
    Java Code:
    package gnp;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    /**
     * @author Nicolás A. Ortega
     * @copyright DeathsbreedGames
     * @license GNU GPLv3
     * @year 2014
     * 
     */
    public class GNPApplet extends JApplet {
    	// Basic dimensions
    	public final int WIDTH = 500;
    	public final int HEIGHT = 500;
    
    	private JPanel game;
    
    	@Override
    	public void init() {
    		game = new Game(WIDTH, HEIGHT);
    		add(game);
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    	}
    }

    Here's the constructor of my JPanel class:
    Java Code:
    public Game(int w, int h) {
    		this.width = w;
    		this.height = h;
    		setSize(width, height);
    		setVisible(true);
    		setBackground(Color.WHITE);
    		setFocusable(true);
    		requestFocusInWindow();
    
    		addKeyListener(this);
    }
    Also, just in case you were wondering, I did implement KeyListener.

    I certainly hope you understand more of what's going on than I do. Thanks!

    UPDATE: I moved the KeyListener to a separate class called InputHandler. So now the constructor addKeyListener looks like this: addKeyListener(new InputHandler). The circumstance is still the same. If you need to see the code yourself it's here: https://github.com/DeathsbreedGames/GNP2
    Last edited by Vampire5; 06-13-2014 at 07:11 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: KeyListener works with JFrame but not with JApplet

    Remove requestFocusInWindow().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: KeyListener works with JFrame but not with JApplet

    A component needs to have focus in order to receive KeyEvents. I guess a JApplet and JFrame work differently in determining which component receives focus.

    The requestFocusInWindow() method only works once the window is visible, so you can't use it in the constructor of your Game class.

    In any case you should not be using a KeyListener. Swing was designed to be used with Key Bindings. See Motion Using the Keyboard « Java Tips Weblog for more information and examples.

Similar Threads

  1. adding keylistener to JFrame
    By Alkor in forum New To Java
    Replies: 3
    Last Post: 03-08-2012, 12:02 PM
  2. JApplet works in eclipse but refuses to show online
    By Reskaillev in forum New To Java
    Replies: 14
    Last Post: 09-10-2011, 01:36 PM
  3. JApplet in JFrame...
    By Rolf83 in forum Java Applets
    Replies: 8
    Last Post: 06-03-2011, 05:15 PM
  4. Replies: 2
    Last Post: 05-11-2011, 06:29 PM
  5. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM

Tags for this Thread

Posting Permissions

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