Results 1 to 5 of 5
  1. #1
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Question Listening KeyEvent from two JPanel

    Ok, from my previous post, I managed what I wanted to do for inner issues.

    But now I switched to CardLayout for a different game. I again have many JPanels and a game which itself is a JPanel too. When I switch to Game panel, due to hierarchy I cannot go to previous panel. This time I have to hold Game panel for changing settings and save/load operations too. With CardLayout, I can hold all created Panels present.

    However, when I want to go back to menu, I press ESC and since only Game panel hold focus, and I cannot reach cardLayoutPanel to give the show menu command.

    Is it possible to share focus?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't know about the others, but myself, I'd be greatly helped by your creating and posting a small example program that demonstrates your problem, an SSCCE.

  3. #3
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Well, I thought it's clear but since I'm the writer I cannot judge other views, sorry.

    Java Code:
               addKeyListener( new KeyAdapter() {
    
                @Override
                    public void keyPressed( KeyEvent e)
                    {
                        int keyCode = e.getKeyCode();
                        if( keyCode == KeyEvent.VK_ESCAPE)
                            layout.show( cardPanel, "Menu");
                    }
    This is the code for cardLayoutPanel. But it doesn't work because Game JPanel has the focus from here:

    Java Code:
                    public void actionPerformed( ActionEvent e) {
                        game.initialize( new Protagonist(), 1);
                        layout.show( cardPanel, "Game");
                        game.requestFocus();
                        isGameStarted = true;
                        contin.setVisible( true);
                        play.setVisible( false);
    Game JPanel needs keyboard inputs, but when it holds the focus, I can't get back. If cardLayoutPanel holds the focus, then everything works great except Game panel doesn't take any input to advance in game.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's somewhat clear, but it is very difficult to debug a problem like this from afar without code to run, test, and modify -- hence my request for an SSCCE. I believe though that there may be focus issues with CardLayout (you may want to search on this). One solution which immediately comes to mind is using key binding rather than a key listener, but again, without an SSCCE, it's hard to give specific help that I am confident is fully accurate.

  5. #5
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Well I solved the issue with adding a FocusListener to the Game JPanel. When ESC is pressed, focus lost and necessary action taken.

Similar Threads

  1. How to dispatchEvent(KeyEvent evt) to an applet
    By crikey in forum Java Applets
    Replies: 17
    Last Post: 09-24-2010, 11:43 PM
  2. Help in KeyEvent
    By chyrl in forum AWT / Swing
    Replies: 15
    Last Post: 05-26-2010, 09:12 AM
  3. JOptionPane KeyEvent
    By mine0926 in forum NetBeans
    Replies: 5
    Last Post: 05-05-2010, 03:03 AM
  4. Replies: 11
    Last Post: 12-25-2009, 11:30 PM
  5. Replies: 0
    Last Post: 08-17-2009, 04:59 PM

Posting Permissions

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