Results 1 to 5 of 5
Thread: Help with CardLayout
- 05-27-2010, 03:12 AM #1
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); } }); }
- 05-27-2010, 03:34 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 05-28-2010, 12:50 AM #3
i looked at the link and tried everything but it still doesn't work
- 05-28-2010, 01:00 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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!
- 05-28-2010, 01:03 AM #5
Similar Threads
-
cardlayout question
By newbie123 in forum New To JavaReplies: 1Last Post: 04-05-2010, 11:53 PM -
Populating CardLayout JPanels with External classes
By fred2482 in forum New To JavaReplies: 5Last Post: 12-21-2009, 06:08 AM -
Unexpected behavior Thread.sleep() is used within CardLayout
By mityay in forum AWT / SwingReplies: 3Last Post: 06-28-2009, 07:18 PM -
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