Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:09:28+00:00 2026-06-14T22:09:28+00:00

I have the following code (I do frame.add(new LevelPanel()) in my main method that

  • 0

I have the following code (I do frame.add(new LevelPanel()) in my main method that extends JFrame. For some reason, this code for the KeyListener is exactly how it is for other people and yet it doesn’t recognize anything.

It should at least print out “Got here” when I press a key (and it doesn’t). The code does print the panel, but the panel has no reaction to any of the keys I press.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * Items represent the things in the level that can be picked up.
 * Subclasses: Gem
 * 
 * @author Tim Ochsner
 * @version 1.0 11/18/2012
 */

public class LevelPanel extends JPanel {
private Game game;
private Level level;
private Player player;
private char[][] table =
        {{'p', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'w', 'w', 'w', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
        {'w', 'f', 'f', 'f', 'w', 'w', 'w', 'w', 'w', 'f', 'f', 'f', 'f', 'f', 'f'},
        {'w', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'},
        {'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'},
        {'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'},
        {'f', 'f', 'f', 'f', 'f', 'f', 'w', 'w', 'w', 'w', 'f', 'f', 'w', 'w', 'f'},
        {'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'w', 'f'},
        {'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'},
        {'w', 'w', 'w', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'}};

public LevelPanel(Game game)
{
    this.game = game;
    player = game.getPlayer();
    level = new Level(game, table);
    setBackground(Color.WHITE);
    addKeyListener(new ArrowListener());

}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    level.draw(g);
}

public class ArrowListener implements KeyListener {

    public void keyPressed(KeyEvent e) {
        System.out.println("Got here");
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if (!(player.getCol() == 14) && level.getTileLayout()[player.getRow() + 1][player.getCol()].isPassable()) {
                System.out.println("Got here");
                player.move(player.getRow() + 1, player.getCol());
                table[player.getRow()][player.getCol()] = 'f';
                table[player.getRow() + 1][player.getCol()] = 'p';
            }
        }

    }

    public void keyReleased(KeyEvent e) {
        System.out.println("Got here");

    }

    public void keyTyped(KeyEvent arg0) {
            System.out.println("Got here");         
    }

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:09:30+00:00Added an answer on June 14, 2026 at 10:09 pm

    KeyListener is a fickle mistress, I would recommend that you avoid them in favor of key bindings

    KeyListener will only fire an event if 1- The component is focusable and 2- the component has focus. Key bindings allow you to get around this restriction.

    If you MUST use a KeyListener, first call setFocusable(true) on your LevelPanel, followed by requestFocusInWindow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: JFrame frame = new JFrame(); JScrollPane scrollPane = new
I have following Java code: JFrame frame = new JFrame(); frame.setTitle(Sudoku); frame.setLocationRelativeTo(null); frame.setSize(500, 500);
I have the following code which should add a MovieClip and add a frame
I have the following code in the top frame of a two frame page:
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for loading image from url in xml parsing endElement method
I have following code that I am compiling in a .NET 4.0 project namespace
I have a UIViewController subclass called TripViewController. This class has the following method: -
Ok, I have the following code. public class MyProgressBar extends JPanel implements MyData, Serializable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.