Results 1 to 5 of 5

Thread: CardLayout Help

  1. #1
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default [SOLVED] CardLayout Help

    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();
        }
    }
    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?
    Last edited by David M.; 08-06-2011 at 04:28 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

  3. #3
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    Is there a way I can do it with default CardLayout? I'd rather not have to rely on a seperate subclass.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  5. #5
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    So I really only need this much:

    Java Code:
    public void ancestorAdded(AncestorEvent event)
    
    	{
    
    		currentCard = event.getComponent();
    
    
    
    		if (isRequestFocusOnCard)
    
    			currentCard.transferFocus();
    of the ancestorAdded and a new AncestorListener() which checks for ancestorAdded() and reports to the Layout class?

    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 04:27 AM.

Similar Threads

  1. CardLayout Trouble
    By eLancaster in forum AWT / Swing
    Replies: 2
    Last Post: 06-23-2011, 12:04 PM
  2. Refresh CardLayout
    By soulmed in forum AWT / Swing
    Replies: 2
    Last Post: 05-03-2011, 03:51 PM
  3. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 02:03 AM
  4. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 08:52 PM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:43 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
  •