I got the following error when I tried to do the following.
(Names in Brackets indicate the names of the variables in the
code below)
1) An applet with a GridBagLayout has two Panels
(MainPanel and AnotherPanel).
2) I set the Layout in one of the panels(MainPanel) to
CardLayout(cards)
3) Added two more Panels(OnePanel and SecondPanel) to the
CardLayout (cards)
4) When I set the second card to display I am getting the following
error. What is wrong with my program.
ERROR:
Applet exception: exception: java.lang.IllegalArgumentException: wrong parent fo
r CardLayout
java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(CardLayout.java:159)
at java.awt.CardLayout.show(CardLayout.java:254)
at small.init(small.java:39)
at netscape.applet.EmbeddedAppletFrame.run(EmbeddedAppletFrame.java:323)
at java.lang.Thread.run(Thread.java:296)
Code follows
----------------------------Code--------------------------------------------
import java.awt.*;
import java.applet.*;
public class small extends java.applet.Applet{
CardLayout cards;
public void init(){
GridBagLayout gbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gbag);
/* Have two Panels
*/
Panel MainPanel = new Panel();
Panel AnotherPanel = new Panel();
AnotherPanel.setBackground(Color.white);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridheight = 4;
c.fill = GridBagConstraints.BOTH;
gbag.setConstraints(MainPanel,c);
add(MainPanel);
gbag.setConstraints(AnotherPanel,c);
add(AnotherPanel);
/* Set CardLayout for the MainPanel
* and then add two cards to it
*/
cards = new CardLayout();
MainPanel.setLayout(cards);
Panel OnePanel = new Panel();
OnePanel.setBackground(Color.blue);
MainPanel.add("onePanel",OnePanel);
/* Add the second card to it
*/
Panel SecondPanel = new Panel();
SecondPanel.setBackground(Color.green);
MainPanel.add("secondPanel",SecondPanel);
/* sleep for some time and then
* try changing the cards
*/
try{
Thread.sleep(300);
}catch (Exception e){ };
cards.show(SecondPanel,"secondPanel");
}
}
----------------------------------------Code-----------------------------------
Thanks In advance,
Cheers
Mathan
>SNIP<
>Code follows
> CardLayout cards;
> gbag.setConstraints(AnotherPanel,c);
> add(AnotherPanel);
The problem is the last line:
cards.show(SecondPanel,"secondPanel");
should be
cards.show(MainPanel,"secondPanel");
since MainPanel is the parent.
***********************************************************
* Jere McDevitt * My thoughts are my own - *
* Applications Developer * but I get paid to think. *
***********************************************************