I’ve a mainPanel with VerticalLayout (org.jdesktop.swingx.VerticalLayout). main panel has several sub panels. One of them is a dynamically changing panel according to the user selection. So, I’ve set it’s layout as CardLayout which I think is the easiest (maybe best?) way to achieve that.
I’ll call that panel elasticPanel. As the name says, it should be elastic. Which means, it should be capable of both expanding and contracting. Let’s say it behave like this. If the user select 1, the elasticPanel should display one , say, JComboBox. If the user select 2 then, two JComboBoxs…
Ok, it works perfectly up to now. Now the user select 1 again when the elasticPanel is showing two JComboBoxs. What i need to happen now is the elasticPanel should display one JComboBox with it’s normal size. But since the elasticPanel is already expanded, what happens is it shows the JComboBox stretched to fit it’s size. So it gives a weird look.
Following screenshots show the problem i’m having with my interface.
Before the selection. NONE is selected.

An element is selected

NONE is selected again

I need the elasticPanel (Location of Fault) in the last screenshot to be as in the first screenshot. This is just a simple case. Imagine the look when going back to NONE after displaying about 5, 6 sub components.
I’ve tried the setSize() method. It doesn’t do anything..So how to fix the issue?
Any help is appreciated. Thanks!
It’s hard to tell you are referring
CardLayoutto what thingy. Since CardLayout works in a different way. What you can do is simply place oneJPanelsay basePanel havingGridLayout(0, 1)and place thisJPanelon top of anotherJPanelsay contentPanel, and now set this as the content pane for theJFrameand calling pack() when you add or remove an element from the view. Here is one example showing you what I mean.*Latest Update : *
More insight by adding more components and placing the elastic panel at some other location, and not on top of content pane.