Switching Between Cards in a CardLayout using getParent()

Posted by plutoisaplanet on Stack Overflow See other posts from Stack Overflow or by plutoisaplanet
Published on 2010-06-08T15:00:26Z Indexed on 2010/06/08 15:02 UTC
Read the original article Hit count: 177

Filed under:
|

Hey everyone,

I am writing an application where I am using the CardLayout to swap between two panels that are placed right on top of one another.

There's a JPanel called Top, and it's layout is the CardLayout. Inside this JPanel is a JPanel called CMatch. Whenever the user clicks the submit button in the CMatch panel, I want a new JPanel added to Top that is custom built based on what the user types in, and it will be shown instead of the original CMatch panel. All of this done using the CardLayout.

These are all different classes in different files, however (the panel Top with CardLayout, the panel CMatch that is inside the Top panel, and the custom built panel). So i tried using the following to add the new panel to the Top panel and then have it shown:

(this code takes place in the CMatch class):


    private void submitButtionActionPerformed(ActionEvent e) {
        CardLayout cl = (CardLayout)(this.getParent().getLayout());
        cl.addLayoutComponent(new CChoice(), "college_choices");
        cl.show(this.getParent(), "college_choices");
    }

However, this didn't work. So i was wondering, what am I doing wrong? Any advice is greatly appreciated, thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about swing