Misbehaving CardLayout

Posted by devon on May 25, 2012 in Java |

CardLayout works great as long as the “cards” are all about the same size. However, when a panel using CardLayout contains a very large component and a very small component, there can be some unpleasant behaviors.

When added to a JScrollPane, the the scroll bars work very well when displaying the large JTable. But, a small component (ex: a single button) is also displayed with scroll bars. Even worse, if there is enough difference in size, it is possible to entirely scroll the smaller content off screen.

This class provides a specific variation on a JPanel with a CardLayout that solves this behavior issue.

When using CardLayout, the size of the panel is the height of the tallest and width of the widest child components. The problem StackedPanel is designed to solve is the case where this is placed in a JScrollPane. If a CardLayout contains a very large component (a large table for example) and a very small component (a single button), the small component will be displayed with scroll bars. If there is enough differance in size, it may be possible to entirely scroll the smaller content off screen.

StackedPanel addresses this by using a BorderLayout to display the current component and effectively resizing the panel to reflect the size of the currently displayed component, but otherwise behaving as though it was using CardLayout.

Copyright © 2011-2017 Experimental.Jar All rights reserved.
This site is using the Desk-Mess-Mine Child-Theme, v1.0.0, on top of
the Parent-Theme Desk Mess Mirrored, v2.5, from BuyNowShop.com