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 7402079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:39:00+00:00 2026-05-29T04:39:00+00:00

Hi hope someone can tell me what I am doing wrong with my Key

  • 0

Hi hope someone can tell me what I am doing wrong with my Key Event.

I am using a Card Layout to navigate through two of my JPanels atm. To do so I use Action Events as well as Key Events. The action events will toggle between JPanels when a button is pressed while the key events will hide away the buttons when a key is pressed. All good with the key events, it does what I want (call a method on one of the panels to set the bounds of the buttons placed inside it eq: button.setBounds(-1, -1, 150, 40); but when I press any of the buttons the key event wont respond despite not having any event on the buttons that I press. Below is my code, for simplicity I removed the non relevant parts of the panels like what they are meant to do.

Thanks in advance and please let me know if I need to provide more clues or to edit the code more, I will try my best to make the code clearer.

public class PanelContainer extends JPanel implements ActionListener, KeyListener{
    GamePanel game = new GamePanel();
    MainMenuPanel mainMenu = new MainMenuPanel();
    CardLayout cards = new CardLayout();
    public PanelContainer(){
        setLayout(cards);
        this.setFocusable(true);
        this.addKeyListener(this);
        mainMenu.newGameButton.addActionListener(this);
        add(mainMenu, "Card1");
        add(game, "Card2");
    }
    @Override
    public void actionPerformed(ActionEvent aevt){
        cards.show(this, "Card2");
        game.action();
    }
    @Override
    public void keyTyped(KeyEvent kevt){

    }
    @Override
    public void keyPressed(KeyEvent kevt){

    }
    @Override
    public void keyReleased(KeyEvent kevt){
        if(kevt.getKeyCode() == KeyEvent.VK_ESCAPE || kevt.getKeyChar() == 'O' ||    kevt.getKeyChar() == 'o'){
            game.shw(); //shw() is a method inside GamePanel that sets the bounds of the buttons
        }
        else if (kevt.getKeyChar() == 'h' || kevt.getKeyChar() == 'H'){game.hid();}
    }
}





public class MainMenuPanel extends JPanel
{
    private URL workingDir = this.getClass().getResource("imgresources/brick_wall.png") ;
    private ImageIcon icon = new ImageIcon(workingDir) ;
    private Image img = icon.getImage();
    //create and initiate buttons;
    JButton newGameButton = new JButton("New Game"); 
    JButton highScoreButton = new JButton("High Scores");
    JButton controlsButton = new JButton("Controls");
    Point[] points = new Point[1000];

    public MainMenuPanel(){
        add(newGameButton);
        add(highScoreButton);
        add(controlsButton);
        setPoints();
    }

    public void setButtonsBounds(){
        newGameButton.setBounds(450, 200, 200, 40);
        highScoreButton.setBounds(450, 250, 200, 40);
        controlsButton.setBounds(450, 300, 200, 40);
    }

    @Override
    public void paintComponent(Graphics g){
        try{
            super.paintComponent(g);
            Graphics2D d2 = (Graphics2D) g;
            d2.setColor(new Color(0, 0, 0));
            d2.fillRect(0, 0, this.getWidth(), this.getHeight());

            setButtonsBounds();
            for(int i=0; i<275; i++){
                d2.drawImage(img, points[i].x +200, points[i].y, this);
            }
        }catch(Exception e){}
    }
}





public class GamePanel extends JPanel implements Runnable{
    JButton button = new JButton("Main Menu");
    JButton button2 = new JButton("Exit Game");
    void shw(){
       add(button);
       add(button2);
       button.setBounds(400, 200, 150, 20);
       button2.setBounds(400, 240, 150, 20);

    }
    void hid(){
       button.setBounds(1, 1, 150, 20);
       button2.setBounds(1, 40, 150, 20);
    }
}
  • 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-05-29T04:39:02+00:00Added an answer on May 29, 2026 at 4:39 am

    It’s a focus issue. Use Key Bindings instead of a KeyListener so you don’t have to worry about this issue (and for other benefits as well — check the Key Bindings tutorial for details).

    Here’s my SSCCE that demonstrates what I’m talking about. Note that both KeyListener and key bindings work until you press a button, and then only bindings work:

    import java.awt.event.*;
    import javax.swing.*;
    
    public class KeyBindingsEg {
       private static void createAndShowGui() {
          PanelContainer mainPanel = new PanelContainer();
    
          JFrame frame = new JFrame("KeyBindingsEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class PanelContainer extends JPanel {
    
       public PanelContainer() {
          this.setFocusable(true);
          this.addKeyListener(new MyKeyListener());
          JButton newGameButton = new JButton("New Game");
          newGameButton.addActionListener(new MyActionListener());
          add(newGameButton);
          setKeyBindings();
       }
    
       private void setKeyBindings() {
          Action hideAction = new BindingAction(BindingAction.HIDE);
          Action showAction = new BindingAction(BindingAction.SHOW);
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          InputMap inputMap = getInputMap(condition);
          ActionMap actionMap = getActionMap();
    
          actionMap.put(BindingAction.HIDE, hideAction);
          inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_H, 0), BindingAction.HIDE);
          inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.SHIFT_DOWN_MASK),
                BindingAction.HIDE);
    
          int[] showKeys = { KeyEvent.VK_O, KeyEvent.VK_ESCAPE };
          actionMap.put(BindingAction.SHOW, showAction);
          for (int key : showKeys) {
             inputMap.put(KeyStroke.getKeyStroke(key, 0), BindingAction.SHOW);
             inputMap.put(KeyStroke.getKeyStroke(key, KeyEvent.SHIFT_DOWN_MASK),
                   BindingAction.SHOW);
          }
    
       }
    
       private class MyActionListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent aevt) {
             System.out.println("button pressed");
          }
       }
    
       private class MyKeyListener extends KeyAdapter {
          public void keyReleased(KeyEvent kevt) {
             if (kevt.getKeyCode() == KeyEvent.VK_ESCAPE
                   || kevt.getKeyChar() == 'O' || kevt.getKeyChar() == 'o') {
                System.out.println("KeyListener: show");
             } else if (kevt.getKeyChar() == 'h' || kevt.getKeyChar() == 'H') {
                System.out.println("KeyListener: hide");
             }
          }
       }
    
       private class BindingAction extends AbstractAction {
          public static final String HIDE = "Hide";
          public static final String SHOW = "Show";
    
          public BindingAction(String text) {
             super(text);
             putValue(ACTION_COMMAND_KEY, text);
          }
    
          @Override
          public void actionPerformed(ActionEvent evt) {
             String actionCommand = evt.getActionCommand();
             if (actionCommand.equals(HIDE)) {
                System.out.println("key bindings: hide");
             } else if (actionCommand.equals(SHOW)) {
                System.out.println("key bindings: show");
             }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope someone can tell me why I'm wrong. Here is a snippet of
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
Hope someone can tell .. Table A Table E Id | Date Id |
hope someone can answer this. Here is some sample code. namespace std { #ifdef
Hope someone can help i cant seem to get my head around this, i
Hope someone can help me out with this problem I am having. I just
I hope someone can help me here. I'm having trouble loading this variable. Right
I hope someone can help me with this, as I'm unable to find the
I hope someone can help here, we're having an incredibly annoying time with Visual
i hope someone can point me in the right direction On a existing html

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.