Results 1 to 5 of 5
- 12-11-2010, 09:15 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
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?
-
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.
- 12-11-2010, 09:25 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
Well, I thought it's clear but since I'm the writer I cannot judge other views, sorry.
This is the code for cardLayoutPanel. But it doesn't work because Game JPanel has the focus from here:Java Code:addKeyListener( new KeyAdapter() { @Override public void keyPressed( KeyEvent e) { int keyCode = e.getKeyCode(); if( keyCode == KeyEvent.VK_ESCAPE) layout.show( cardPanel, "Menu"); }
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.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);
-
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.
- 12-11-2010, 10:31 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
How to dispatchEvent(KeyEvent evt) to an applet
By crikey in forum Java AppletsReplies: 17Last Post: 09-24-2010, 10:43 PM -
Help in KeyEvent
By chyrl in forum AWT / SwingReplies: 15Last Post: 05-26-2010, 08:12 AM -
JOptionPane KeyEvent
By mine0926 in forum NetBeansReplies: 5Last Post: 05-05-2010, 02:03 AM -
How can i use keyevent even tho the program isnt selected?
By Addez in forum New To JavaReplies: 11Last Post: 12-25-2009, 10:30 PM -
Brainbox needed -- KeyEvent VK_* constants stable??!
By Spiller in forum AWT / SwingReplies: 0Last Post: 08-17-2009, 03:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks