
-----------------------------------
_Dan_
Thu Sep 11, 2008 9:09 pm

Simple Card Layout Problem
-----------------------------------
I'm just starting to learn about layouts and I can't understand why the following code doesn't execute correctly. It's supposed to display Page 2 but instead it Displays Page 0 (the first page that was added to the applet), can anyone tell me what's wrong :S? Also, how do I implement a key listener that will detect when I press a key down in the active JPanel. Help is much appreciated!

public class LayoutTest extends java.applet.Applet
{
    //Sets up the blank pages
    final int noOfPages = 3;
    JPanel[] pages = new JPanel [noOfPages];
    public void init ()
    {
        JLabel[] text = {new JLabel ("One"), new JLabel ("Two"), new JLabel ("Three") };

        for (int i = 0 ; i < noOfPages ; i++)
        {
            //Creates a page
            pages [i] = new JPanel ();

            //Adds text to the created page
            pages [i].add (text [i]);

            //Adds the created page to the applet
            add ("Page " + i, pages [i]);
        }

        //Sets the layout to the card layout
        CardLayout card = new CardLayout ();
        setLayout (card);

        //Shows the first page
        card.show (this, "Page 2");

        requestFocus ();
    }
}

-----------------------------------
TheFerret
Fri Sep 12, 2008 2:02 am

RE:Simple Card Layout Problem
-----------------------------------
Take a look here

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

-----------------------------------
_Dan_
Fri Sep 12, 2008 3:45 pm

Re: Simple Card Layout Problem
-----------------------------------
EDIT: {Prolly best to learn these things yourself as I did, anyways, resolved}

Still doesn't work, now it displays none of the labels :