views:

455

answers:

1

I have a JFrame with a JPanel on it.

I want to add another JPanel which is a preconfigured component onto the Jpanel inside my JFrame.

If I do this:

    subPanel.setLayout(new BorderLayout());
    subPanel.add(preconfiguredPanel,BorderLayout.CENTER);

my Panel will show.

If I do this:

    subPanel.add(preconfiguredPanel);

my JPanel will not show. The documentation says when using add(Component) it will use the default Layout FlowLayout. Ok fine, but why won't my component display inside that JPanel when using the default FlowLayout???

+3  A: 

Probably because your panel doesn't have a preferred size.

When you add a panel to a BorderLayout the default is to place it in the center, so the panel will automatically be resized to the size of the frame.

When you add the panel to a FlowLayout, the flow layout repsects the size of the panel.

If you need more help then post your SSCCE.

camickr
I'm using NetBeans to Layout my various components. Doesn't my component have the preferred size that I've sized it to using NetBeans? This is the exact size it shows up as when using BorderLayout, or when adding it directly into a JFrame and doing pack(). Can you be more specific on the return value of a function I should be checking for the component JPanel?
Zak
If you are adding Swing components to a Swing panel that uses a layout then yes the panel should have a preferred size. If you are using a JPanel without a layout manager or maybe to do custom painting, then it will not have a preferred size. I can't guess the exact cause because you haven't posted a SSCCE.
camickr