Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ClassCastException on Cardlayout

im on that stage of Java learning where you get to make gui’s, for this program, im using CardLayout for swapping between panels, the thing is, i get this error over and over, i’ve searched everywhere and i still cannot solve it, the error as shown on console is:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class java.awt.FlowLayout cannot be cast to class java.awt.CardLayout (java.awt.FlowLayout and java.awt.CardLayout are in module java.desktop of loader ‘bootstrap’)

The console said my problem is on this part of the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public FrameMenuPrincipal() {
        initComponents();
        deffbg = new JPanel();
        deffbg.add(pane1, "panewelc");
        deffbg.add(pane2, idcreate);
        deffbg.add(pane3, idsite);
        
        cl = (CardLayout)(deffbg.getLayout());
        cl.show(deffbg, idmenu);
    }

deffbg being the parent panel;

For adding more context:
I’ve been using this tutorial
And I’m programming on NetBeans, using the Gui builder

Also, sorry if this question is badly made, i’m new to stack overflow and im also Hispanic so, sorry if there’s any grammar errors here

>Solution :

By default, JPanel used a FlowLayout. If you want to use something different, you can provide it in the constructor. So in this case, if you wanted to use a CardLayout you could instantiate it like this:

deffbg = new JPanel(new CardLayout());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading