Results 1 to 5 of 5
  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Question Help with CardLayout

    i've never used the CardLayout so this is my first time with it. im creating a program were it shows 3 pages. you click next or previous to go to the different pages. When i run it and click next it shows a error.

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.GridLayout cannot be cast to java.awt.CardLayout

    i created a JFrame that has a CardLayout and 3 JPanels that have GridLayout.
    i don't know if that is causing the problem.


    there is a lot more to the code but i didn't want to put 1064 lines. so i put the important pieces.
    Java Code:
    private void Switcher(java.awt.event.ActionEvent evt) {                          
            CardLayout cards = new CardLayout();
            if(evt.getSource()==jButton2) {
                 cards = (CardLayout)jPanel2.getLayout();
       cards.next(jPanel2);
    
    
            }else if (evt.getSource()==jButton3) {
               cards = (CardLayout)jPanel1.getLayout();
       cards.next(jPanel1);
            }else if(evt.getSource()==jButton4) {
                 cards = (CardLayout)jPanel3.getLayout();
       cards.next(jPanel3);
            }else if(evt.getSource()==jButton5) {
                cards = (CardLayout)jPanel2.getLayout();
       cards.next(jPanel2);
            }
    
        }                         
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new frame().setVisible(true);
                }
            });
        }

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

    Default

    Read the section from the Swing tutorial on How to Use Card Layout for a working example. Compare the code and I'm sure you will find your mistake.

  3. #3
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    i looked at the link and tried everything but it still doesn't work

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You seem to be trying to extract the layouts from JPanels jPanel1, jPanel2, jPanel3 which are all (I believe) GridLayout and trying to cast this layout object to be a CardLayout, and this is obviously not working. To extract the true CardLayout, either make it an instance variable and thus is visible in the class, or call getLayout on the container that actually has the CardLayout.

    Though mostly, I concur with Rob Camick, that you should go through the tutorial and the code contained again as you're doing it all wrong.

    BTW, there's no way anyone can give you the correct code as we have no idea what the code looks like in the rest of your program. Also, do yourself and us a favor and rename your variables so that their names make sense. Who knows what jButton1 or jButtonFoo signifies?

    Suerte, amigo!

  5. #5
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

Similar Threads

  1. cardlayout question
    By newbie123 in forum New To Java
    Replies: 1
    Last Post: 04-05-2010, 11:53 PM
  2. Populating CardLayout JPanels with External classes
    By fred2482 in forum New To Java
    Replies: 5
    Last Post: 12-21-2009, 06:08 AM
  3. Replies: 3
    Last Post: 06-28-2009, 07:18 PM
  4. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 09: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
  •