CardLayout usage

This code:

package test;

import java.awt.CardLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

publicclass Main{

privatestaticvoid createAndShowGUI(){

JFrame frame =new JFrame("Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new CardLayout());

frame.add(new JLabel("Label"));

frame.pack();

frame.setVisible(true);

}

publicstaticvoid main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

crashes with :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string

at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190)

at java.awt.Container.addImpl(Container.java:1068)

at java.awt.Container.add(Container.java:935)

at javax.swing.JFrame.addImpl(JFrame.java:545)

at java.awt.Container.add(Container.java:352)

at test.Main.createAndShowGUI(Main.java:27)

................................................

With the line

frame.add(new JLabel("Label"));

changed to

frame.add(new JLabel("Label"),"");

everything works OK.

Am I always supposed to provide string identifiers for components while using CardLayout or is it a bug?

I'm using jdk 1.6.0 on WinXP.

[2553 byte] By [deema] at [2007-11-27 3:45:46]
# 1
The Strings are mandatory, as you have found out. Once you have more than one "card", you'll see those strings are handy, too.Take the tutorial: http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html
DrLaszloJamfa at 2007-7-12 8:49:30 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks!
deema at 2007-7-12 8:49:30 > top of Java-index,Java Essentials,Java Programming...