0

I have a card layout that can switch between Panels with a JComboBox. The problem is when i re-size the window my panel inside never changes size. When ever I would use SpringLayout I could just tell something to stay x pixels away from the border and it would re-size itself. How could I mimic that in CardLayout?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
toxicgrunt
  • 13
  • 3

1 Answers1

3

When ever I would use SpringLayout I could just tell something to stay x pixels away from the border

You can add a Border to the panel that you use for the CardLayout.

JPanel cards = new JPanel(new CardLayout());
cards.setBorder( new EmptyBorder(10, 10, 10, 10) );
cards.add(card1, "card1");
cards.add(card2, "card2");

Now each child panel will have a Border of 10 pixels around the panel. You can also add Borders to the individual card panels. Read the section from the Swing tutorial on How to Use Borders for more information and working examples.

... and it would re-size itself. How could I mimic that in CardLayout?How could I mimic that in CardLayout?

A CardLayout just holds other panels. You still need to set the layout of each panel you add to the card panel, such that the components resize.

The Swing tutorial also has a section on How to Use CardLayout with a working demo that shows components locations changing as the frame is resized. If you want the component to resize, then you would need to use a different layout manager for each panel you add to the CardLayout.

camickr
  • 321,443
  • 19
  • 166
  • 288