I was following the How to use CardLayout Java Swing tutorial and I got to the point where the panel is added to the layout:
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
//Create the "cards".
JPanel card1 = new JPanel();
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
Also they say:
To add a component to a container that a CardLayout object manages, specify a string that identifies the component being added. For example, in this demo, the first panel has the string "Card with JButtons"
So if that string is used to identify the component being added I was wondering if there was a way to get a specific panel in the layout from an AssertJ Swing FrameFixture
or directly from the JFrame
object, passing the string.
I see that the add
method is inherited from java.awt.Container
. I was expecting to find a method that would allow me to do something like frame.getComponent(BUTTONPANEL)
but that method expects an index as parameter. Did I overlook something?
Also I'm aware of the fact that I could just do card1.setName(BUTTONPANEL)
and then in my tests retrieve it with:
window = new FrameFixture(view);
window.panel(BUTTONPANEL);
But what's the point of setting that string in the add
method if there is no use for it.
Thanks.
Edit:
I had missed the fact that of course the string BUTTONPANEL
it it is used to change the panel from the card layout, like this:
cardLayout.show(getContentPane(), BUTTONPANEL);
And so obviously that string has a use.
However I was looking for a way to be able to get the JPanel
object by calling a method on the JFrame
or FrameFixture
object, passing the string. Something like this:
JPanel buttonPanelPanel = frame.getPanelFromName(BUTTONPANEL);
This is because within the tests I don't have a direct access to the panels, but only to the frame. In order to make assertions on a particular panel I wanted to be able to perform a get of the panel with the string used in add
. But maybe this is not possible.