0

I have situation that I use CardLayout with several JPanels, because I want to see only on at the moment. BUT, I need to make JPanel's (each of them) components auto resizable. I googled and found that GridLayout or GridBagLayout (as I figured out) can do that components change shapes when resizing JFrame.

Can anyone help me how to combine these two Layouts and make things work together.

P.S. All JPanels are in separate classes and main class is what contains JFrame with CardLayout. Also contains menubar. All JPanels has absolute Layout (I suppose it is also a problem), but I am not very friendly with Layouts.

Aleksandar
  • 1,163
  • 22
  • 41
  • `All JPanels has absolute Layout (I suppose it is also a problem)` - yes that is the problem. Components will not resize when you use absolute layout. `but I am not very friendly with Layouts.` - well that is the solution you need to use, so you need to spend some time learning how to use each of the different layout to get your desired look. Start by reading the section from the Swing tutorial on [How to Use Layouts](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more information and working examples. – camickr May 15 '14 at 14:41

1 Answers1

1

In this example, the JPanel named cards has CardLayout, but each instance of CardPanel that is added to cards has the default JPanel layout, FlowLayout. You can change the layout and add additional components in the CardPanel constructor to see the effect.

public CardPanel(String name) {
    this.name = name;
    this.setLayout(new GridLayout(0, 1); // a one column grid
    this.setBackground(new Color(random.nextInt()));
    this.add(new JLabel(name));
    this.add(new …);
    …
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045