2

So it's a pretty simple problem, but I can't find a simple way to implement it. I basically have 3 instances of JPanel: north, south and center. Normally, if you play with window height, the center panel will be variable. What I would like, is to have the south panel height being variable instead of the center one.

Here is the code:

public class TestCode {

public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(300, 300);

    JPanel panelUp = new JPanel();
    JPanel panelCenter = new JPanel();
    JPanel panelDown = new JPanel();

    window.add(panelUp, BorderLayout.NORTH);
    window.add(panelCenter, BorderLayout.CENTER);
    window.add(panelDown, BorderLayout.SOUTH);

    panelUp.setBackground(new Color(200, 240, 200));
    panelCenter.setBackground(new Color(240, 200, 200));
    panelDown.setBackground(new Color(200, 200, 240));

    panelUp.setPreferredSize(new Dimension(panelUp.getWidth(), 50));
    panelCenter.setPreferredSize(new Dimension(panelCenter.getWidth(), 100));
    panelDown.setPreferredSize(new Dimension(panelDown.getWidth(), window.getHeight() - 150));

    window.setVisible(true);
}

}
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225

4 Answers4

3
***************BorderLayout************************
*  ********BorderLayout*in*PAGE_START************ *
*  *                                            * *
*  *   Component in PAGE_START                  * *
*  *                                            * *
*  *   Component in PAGE_END                    * *
*  *                                            * *
*  ********************************************** *
*                                                 *
*      Component in CENTER                        *
*                                                 *
***************************************************

See the Nested Layout Example for more ideas about how to combine layouts to create the required layout.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Sorry but I don't exactly see the point, thanx for your help but in my opinion this is a too much general case, as what I asked was a precise thing – Matei Focseneanu Jun 17 '13 at 08:15
2

What I would like, is to have the south panel height being variable instead of the center one.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I'll have a look at these layouts, I guess the only original one (included in Swing) is GridBagLayout, the others are additional components, true? – Matei Focseneanu Jun 13 '13 at 10:17
  • doesn't care if is there JPanel or JComponent, LayoutManager works very similair for both, for container and components, note don't forger that JFrame (JDialof/JWindow) has BorderLayout and JPanel has FlowLayout implemented in API as default LayoutManagers, rest of JComponents haven't any LayoutManager (forgot for JToolbar - BoxLayout) – mKorbel Jun 13 '13 at 10:41
  • why dont you use BorderLayout CENTER and TOP, and then in the TOP, put two flowlayout panels? Not necessary to use GBL or SL – Oliver Watkins Jun 13 '13 at 11:26
  • @Oliver Watkins but JPanels aren't continuously resized with container and by keeps ratio, then BorderLayout, FlowLayout, and BoxLayout aren't ..... – mKorbel Jun 13 '13 at 11:43
1

Simply add the top and center into a panel that is itself a border layout :

public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(300, 300);


    JPanel innerPanel = new JPanel(new BorderLayout());


    JPanel panelUp = new JPanel();
    JPanel panelCenter = new JPanel();
    JPanel panelDown = new JPanel();

    window.add(innerPanel, BorderLayout.NORTH);
    window.add(panelDown, BorderLayout.CENTER);

    innerPanel.add(panelUp, BorderLayout.NORTH);
    innerPanel.add(panelCenter, BorderLayout.SOUTH);

    panelUp.setBackground(new Color(200, 240, 200));
    panelCenter.setBackground(new Color(240, 200, 200));
    panelDown.setBackground(new Color(200, 200, 240));

    panelUp.setPreferredSize(new Dimension(panelUp.getWidth(), 50));
    panelCenter.setPreferredSize(new Dimension(panelCenter.getWidth(), 100));
    panelDown.setPreferredSize(new Dimension(panelDown.getWidth(), window.getHeight() - 150));

    window.setVisible(true);
}

This code should do exactly what you want while hardly altering the code

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • 1
    *"..FLOWLAYOUT. That should do the trick"* a) There is no need to SHOUT at us. b) It will only 'fulfill' the requirement if the programmer manually sets the width of the frame (to force one component to the next line). That is very fragile programming. – Andrew Thompson Jun 13 '13 at 11:51
  • then... in the NORTH panel put another Borderlayout Panel. This inner borderlayout Panel should then have its two panels set at NORTH and then SOUTH. I that should do the trick – Oliver Watkins Jun 13 '13 at 12:06
  • @Andrew Thompson: I don't get what you're trying to say, though it looks interesting. Could you please develop what you mean by "manually setting the width of the frame", and the fact it's fragile programming? – Matei Focseneanu Jun 17 '13 at 08:18
  • @Olivier Watkins: thank you, your example fulfills exactly what I expressed, although there are some things I need to make clear... – Matei Focseneanu Jun 17 '13 at 08:19
  • @Matei i initially said use Flowlayout... but then realised i was wrong. So thats why his comment doesnt make sense anymore – Oliver Watkins Jun 17 '13 at 08:30
  • OK, I think I get the point. Thanx again for your time Olivier! – Matei Focseneanu Jun 17 '13 at 10:27
0

Have you tried setting the maximum size (setMaximumSize) for the north and center panel? Or perhaps setSize directly.

Machisuji
  • 738
  • 7
  • 16
  • Sorry, but no effect with setMaximumSize(), nor setSize() – Matei Focseneanu Jun 13 '13 at 10:08
  • Oh well then you'r really gonna have to use a proper layout manager. You can do pretty much anything with GridBagLayout. It may seem a bit complicated or confusing at first, but once you've got the basics down it's proper easy to build anything you want with it. – Machisuji Jun 13 '13 at 10:44