Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's so special about CardLayout vs manual adding/removal of JPanels?

There have been many times on StackOverflow where a user asks a question like this...

I have a main JPanel that contains a child JPanel. When the user clicks a button, the child JPanel should change to a different JPanel. How can I achieve this.

More often than not, the user has actually tried to implement this problem, but can't get it working.

Whenever I answer this question, I tell them to do something like this (put simply)...

JPanel myFrame = new JPanel();
myFrame.remove(oldPanel);
myFrame.add(newPanel);

I see this as quite a legitimate answer, and I personally have used this in many of my own Java projects without problem. However, I always get downvotes for my answer, and everyone just says "Use a CardLayout".

So my question is, why is everyone so fascinated with CardLayout, to the point where my answer deserves a downvote? Why should I choose to use a CardLayout rather than adding/removing panels using my code above?

As a further question, would you still be suggesting CardLayout for interfaces that have dynamic JPanels. For example, most of my programs implement a custom plugin framework where there could be many hundreds of JPanels, but I only load and display the panels as they are actually required. For the normal use of the program, most of the panels would never actually be loaded or required. For this type of scenario, would my coding approach be the best solution, as I understand that CardLayout would require me to actually create all of the JPanels even though most will never be used?

like image 694
wattostudios Avatar asked May 22 '12 01:05

wattostudios


People also ask

What is CardLayout?

A CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.

Where is the CardLayout used?

The CardLayout class manages two or more components (usually JPanel instances) that share the same display space. When using the CardLayout class, let the user choose between the components by using a combo box.

Which layout manager can be used to create the deck of cards?

The Java CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout.

How do you switch between panels in Java?

If you want to switch between two panels add those panels into another JPanel and use cardLayout. Then, before adding a JPanel remove the current one. Like this: parentPanel.


2 Answers

  • With CardLayout, it's easier to have loose coupling (though not impossible with roll your own)
  • With CardLayout, the preferredSize of the card-holder is that of the largest card it holds.
  • CardLayout is harder to fark-up, and allows almost trivial contiguous component swapping its next() and prev() methods.
  • You can easily associate the desired component with a constant -- no need to have to create a Map<String, Component> for this purpose as it's already there for you. I've not infrequently used enums for this.
  • No need to remember to call repaint() and revalidate() when swapping components.
  • It's built for and allows for easy re-use of components.

I can't explain the reason for a down-vote though, unless they're upset you didn't mention the need to remember to call repaint() and revalidate() when swapping components. You'll have to ask the down-voter if they are brave enough to respond.

like image 163
Hovercraft Full Of Eels Avatar answered Oct 05 '22 23:10

Hovercraft Full Of Eels


CardLayout has been thoroughly tested and proven to work. It correctly acquires the component-tree lock and performs component validation in order to ensure that nothing can go wrong. Your solution, while it may work most of the time, will fail under certain circumstances.

This all boils to reinventing the wheel: Why would you want to when such a time-tested class is already available?

like image 32
Jeffrey Avatar answered Oct 05 '22 23:10

Jeffrey