0

I am creating a Java app project that something similar to the one I post below

  1. Has a single frame with a label.
  2. A Mastercardpanel just below the label.
  3. An array of panels to be embedded onto the Mastercardpanel

Code snippet

CardLayout card=new CardLayout(500,500);
 JPanel mastercardpanel=new JPanel();
    JPanel[] cardpanel;
    cardpanel = new JPanel[50];
     mastercardpanel.setLayout(card);

Also for each of the card panel's I would use group layout to add some Swing elements.

Code snippet

for(t=0;t<50;t++)
    {


    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(cardpanel[t]);
    cardpanel[t].setLayout(layout);
     .
     .
     .

My problem is that GroupLayout would only accept cardpanel as a non-null value. Please suggest how to go about it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Creative_Cimmons
  • 255
  • 1
  • 2
  • 11
  • As near as I can tell, you've not actually added anything to the array, yes you've initialised, but you've not filled it... – MadProgrammer Mar 08 '14 at 11:04
  • I am new to java programming, so excuse me for my noobness but could you tell how to add content to array of panels. how do you fill it even before grouplayout which was supposed to do the filling with swing elements please give an example thanks – Creative_Cimmons Mar 08 '14 at 11:06
  • Try adding `cardpane[t] = new JPanel();` before you create the layout – MadProgrammer Mar 08 '14 at 11:07

1 Answers1

3

Try to initialize every cardpanel in array:

for(t=0;t<50;t++)
{

cardpanel[t]=new JPanel(null);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(cardpanel[t]);
cardpanel[t].setLayout(layout);
 .
 .
 .
AndreaTaroni86
  • 549
  • 12
  • 27