Search code examples
javaswinglayout-managercardlayout

Adding panel in center of container


I have a JPanel which is a BorderLayout and I have set a new panel in the top and container (JDesktopPane) in center and another panel in bottom. Now I want to set panel (card layout) dynamically in center of container as I am showing hierarchy below

           top --> panel
panel(BorderLayout)center --> container(JDesktopPane) --> panel(CardLayout)
            bottom --> panel

Here card layout contain different panel as card. problem is that I'm unable to show each card in center of container.


Solution

  • how to show them center of container?

    Put them in a panel with a GridBagLayout, add them as the only component with no constraint, and they will be centered. See this answer for an example.