Results 1 to 5 of 5
Thread: CardLayout Help
- 08-06-2011, 12:43 AM #1
Member
- Join Date
- Apr 2011
- Location
- Kansas
- Posts
- 26
- Rep Power
- 0
[SOLVED] CardLayout Help
In my JPanel classes I have keyListeners checking for certain keyPressed() events. However, after I switch from my first view to a second one I get no keyPressed responses. Any ideas on why this is? Does setFocusable() get set to false or something?Java Code:import javax.swing.JFrame; import java.awt.CardLayout; public class Layout extends JFrame { /** Create instances of our panels */ final StartMenu start = new StartMenu(this); final GameBoard game = new GameBoard(this); final ContactInfo contact = new ContactInfo(this); final HighScores scores = new HighScores(this); final UpgradeMenu upgrade = new UpgradeMenu(this); final InstructionMenu instructions = new InstructionMenu(this); CardLayout myLayout = new CardLayout(); public Layout() { /** Set the layout and add the menus. Assign them names */ setLayout(myLayout); add(start, "Start Menu"); add(game, "Game"); add(contact, "Contact Information"); add(scores, "High Scores"); add(upgrade, "Upgrades"); add(instructions, "Instructions"); setSize(500, 500); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); swapView("Start Menu"); } public void swapView(String name) { myLayout.show(getContentPane(), name); } public static void main(String[ ] args) { new Layout(); } }Last edited by David M.; 08-06-2011 at 03:28 AM.
- 08-06-2011, 01:23 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
- 08-06-2011, 02:04 AM #3
Member
- Join Date
- Apr 2011
- Location
- Kansas
- Posts
- 26
- Rep Power
- 0
Is there a way I can do it with default CardLayout? I'd rather not have to rely on a seperate subclass.
- 08-06-2011, 02:15 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
As I said in the link the basic solution is to add an AncestorListener to every panel. Then the AncestorListener can request focus on the panel as it is displayed.
There is no reason you can't create your own AncestorListener to do this (all you need is the first couple of lines of the ancestorAdded method). Then you manually add the listener to every panel before you add the panel to the card layout.
- 08-06-2011, 02:57 AM #5
Member
- Join Date
- Apr 2011
- Location
- Kansas
- Posts
- 26
- Rep Power
- 0
So I really only need this much:
of the ancestorAdded and a new AncestorListener() which checks for ancestorAdded() and reports to the Layout class?Java Code:public void ancestorAdded(AncestorEvent event) { currentCard = event.getComponent(); if (isRequestFocusOnCard) currentCard.transferFocus();
Do you have any idea why Cards are not focused by default in CardLayout?
Edit: Nevemind, figured it out. It's working great now. Thanks a lot! Marked as solved.Last edited by David M.; 08-06-2011 at 03:27 AM.
Similar Threads
-
CardLayout Trouble
By eLancaster in forum AWT / SwingReplies: 2Last Post: 06-23-2011, 11:04 AM -
Refresh CardLayout
By soulmed in forum AWT / SwingReplies: 2Last Post: 05-03-2011, 02:51 PM -
Help with CardLayout
By Kyle227 in forum New To JavaReplies: 4Last Post: 05-28-2010, 01:03 AM -
Regarding CardLayout
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 07:52 PM -
Using previous with CardLayout
By uncopywritable in forum New To JavaReplies: 2Last Post: 08-05-2007, 09:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks