Class java.awt.BorderLayout

This is the layout you want to use when the most important part of the dialog should take the most of its area; and some additional controls should be at the edge of the dialog, for example buttons "OK" a "Cancel" at the bottom.

This layout divides the dialog into five parts: a central part, and four parts along each side. You probably will not use all of them together; maybe just the center and one of the edges.

The central part is called CENTER. To address the four parts along the edges, you can use absolute directions: SOUTH, NORTH, EAST, WEST, or directions relative to the flow of text in the given language: PAGE_START, PAGE_END, LINE_START, LINE_END.

For example, the NORTH is always at the upper part of the dialog. In an English application, the PAGE_START would also be the upper part, because in English we start reading the page from the top. Analogically, the WEST part is always on the left side of the dialog. In English, the LINE_START is also on the left, because in English we read lines from left to right, but for example in Arabic, the LINE_START would be on the right.

Within one dialog, use only the absolute directions, or only the relative directions. Do not mix both of them in the same dialog; it could lead to some of them not being shown. (The central part is called CENTER in both situations.)

Here is an example using the absolute directions:

JPanel panel = new JPanel(new BorderLayout());
panel.add(new JButton("Center"), BorderLayout.CENTER);
panel.add(new JButton("North"), BorderLayout.NORTH);
panel.add(new JButton("South"), BorderLayout.SOUTH);
panel.add(new JButton("East"), BorderLayout.EAST);
panel.add(new JButton("West"), BorderLayout.WEST);

Here is the same example, using the relative directions:

JPanel panel = new JPanel(new BorderLayout());
panel.add(new JButton("Center"), BorderLayout.CENTER);
panel.add(new JButton("Page Start"), BorderLayout.PAGE_START);
panel.add(new JButton("Page End"), BorderLayout.PAGE_END);
panel.add(new JButton("Line Start"), BorderLayout.LINE_START);
panel.add(new JButton("Line End"), BorderLayout.LINE_END);

And this is how you would create a more typical dialog, with some important content in the center, and buttons at the bottom. The bottom part of the BorderLayout contains a panel, which itself uses FlowLayout to position the individual buttons.

JPanel settings = new JPanel(new FlowLayout());
settings.add(new JButton("OK"));
settings.add(new JButton("Cancel"));

JPanel panel = new JPanel(new BorderLayout());
panel.add(new JButton("Center"), BorderLayout.CENTER);
panel.add(settings, BorderLayout.SOUTH);

Note: The CENTER part is the default one. When putting a component there, you don't have to name it explicitly. Just use:

panel.add(new JButton("Center"));  // BorderLayout.CENTER

viliam@bur.sk