2
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
    CardLayout card = (CardLayout)main_panel.getLayout();
    card.show(main_panel, "jPanel1");
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    CardLayout card = (CardLayout)main_panel.getLayout();
    card.show(main_panel, "jPanel2");
}

hey guys I needed to use CardLayout to work with one of my existing project, but it never work! so I tried making a new form instead following the tutorial from How to use CardLayout with Netbeans GUI Builder but I still can't get it to work... After clicking those buttons nothing will happen. Any help please?

Community
  • 1
  • 1
Kent Ong
  • 97
  • 1
  • 7

1 Answers1

4
card.show(main_panel, "cardname");

you should pass card name not the variable name of sub panels in card layout. you have currently passed panels names not card names.normally netbeans set card names as card1,card2...etc . you can see card name by selecting your sub panels[jpanel1,2..] from navigate and in the property window there is a row cardname in the layout category.

see this image

enter image description here

in this example (picture) you can see cardname is card2 so if you want to show this selected button you should use

card.show(main_panel, "card2");

but not

card.show(main_panel, "jButton1");

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • 1
    OMG THX SO MUCH! FINALLY!!!!! I'VE BEEN STUCK FOR 4 WHOLE HOUR......... sorry for my stupidity... THX SO MUCH Fast Snail! – Kent Ong Sep 13 '15 at 13:11