I've looked at other stackoverflows and many other examples with JLayeredPane but I can't resolve my issue.
Frame class -> Main JPanel class -> 2 JPanel classes
I have Main JPanel set as Border layout and I have 2 JPanels set as North and South in the Main JPanel. The North JPanel has setPreferredSize.
The issue I have is when I reduce the window size, South Panel where I have buttons declared gets hidden. Even if window is reduced, I still want buttons (south panel) to be visible to users.
JPanel mainPanel = new JPanel(new BorderLayout());
//North panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridBagLayout());
northPanel.setPreferredSize(new Dimension(600, 350));
northPanel.add(example..)
......
//South panel
JPanel southPanel = new JPanel(new BorderLayout()););
JButton button1 = new JButton();
JButton button2 = new JButton();
southPanel.add(button1, BorderLayout.WEST);
southPanel.add(button2, BorderLayout.EAST);
//Add north and south panels to main panel
this.mainPanel.add(northPanel, BorderLayout.NORTH);
this.mainPanel.add(southPanel, BorderLayout.SOUTH); //want these buttons
//to be visible even when window size is reduced.
//but now the buttons gets hidden behind the north panel.
How can I make sure the South panel is always visible regardless of the window size?
Hope my explanation makes sense and thank you!
The issue I have is when I reduce the window size, South Panel where I have buttons declared gets hidden.
A component on a panel is painted based on its ZOrder. As a component is added to a panel it is given a higher ZOrder. Swing actually paints components with the highest ZOrder first.
So instead of doing:
panel.setLayout( new BorderLayout() );
panel.add( new JButton("NORTH"), BorderLayout.NORTH );
panel.add( new JButton("SOUTH"), BorderLayout.SOUTH );
You can do:
panel.setLayout( new BorderLayout() );
panel.add( new JButton("SOUTH"), BorderLayout.SOUTH );
panel.add( new JButton("NORTH"), BorderLayout.NORTH );
Now the "South" button will be painted over top of the "North" button. Of course this will only work if the south component is opaque.