Is there any way too add JLabels and JTextField to only the bottom half of the center border layout. For aesthetic reasons I do not want anything on the upper half and have not found any way of doing this. I am still fairly new using swing and AWT... Any suggestions would be greatly appreciate. I added a picture of what I am trying to achieve.
3 Answers
Give the BorderLayout.CENTER
JPanel a new GridLayout(2, 1)
(for 2 rows, 1 column). Add a blank JLabel to the same JPanel for the top half blank space. Add another component, a JPanel, to the BorderLayout.CENTER
JPanel and have it use a GridBagLayout for the bottom space, and then add your labels and fields using GridBagConstraints. The secret here is to nest JPanels, each using the layout manager of choice.
For example:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class Layouts {
private static final int TF_COLS = 8;
private static void createAndShowGui() {
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.setBorder(BorderFactory
.createTitledBorder("BorderLayout Panel"));
borderLayoutPanel.add(createInnerPanel("North"), BorderLayout.PAGE_START);
borderLayoutPanel.add(createInnerPanel("North"), BorderLayout.PAGE_START);
borderLayoutPanel.add(createInnerPanel("South"), BorderLayout.PAGE_END);
borderLayoutPanel.add(createInnerPanel("East"), BorderLayout.LINE_END);
borderLayoutPanel.add(createInnerPanel("West"), BorderLayout.LINE_START);
JPanel centerPanel = new JPanel(new GridLayout(2, 1));
centerPanel.add(new JLabel());
JPanel gblPanel = new JPanel(new GridBagLayout());
gblPanel.add(new JLabel("Question 1:"), createGbc(0, 0));
gblPanel.add(new JTextField(TF_COLS), createGbc(1, 0));
gblPanel.add(new JLabel("Question 2:"), createGbc(0, 2));
gblPanel.add(new JTextField(TF_COLS), createGbc(1, 2));
centerPanel.add(gblPanel);
borderLayoutPanel.add(centerPanel);
JFrame frame = new JFrame("Layouts");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(borderLayoutPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private static GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
int iGap = 5;
gbc.insets = (x == 0) ? new Insets(iGap, iGap, iGap, 3 * iGap) : new Insets(
iGap, iGap, iGap, iGap);
return gbc;
}
private static JComponent createInnerPanel(String text) {
JPanel panel = new JPanel();
panel.add(new JLabel(text));
panel.setBorder(BorderFactory.createEtchedBorder());
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
And yes, this is more initial work than simply setting bounds, but where you end up saving time and frustration is when you have to maintain this GUI later since if you use proper layouts, it will be much easier to debug, enhance or alter your GUI. It will also have a better chance of looking decent on all platforms and screen resolutions.

- 283,665
- 25
- 256
- 373
-
Awesome! I see what you are saying, so I can have a `GridLayout` inside my `BorderLayout.CENTER`? – Scruffy Nerfherder May 30 '15 at 04:17
-
@ScruffyNerfherder: for example -- please see code. – Hovercraft Full Of Eels May 30 '15 at 04:30
-
Okay, great! Thank you so much, I think I got it! – Scruffy Nerfherder May 30 '15 at 04:45
This is easily done with an EmptyBorder
:
centerPanel.setBorder(new EmptyBorder(top, left, bottom, right));

- 168,117
- 40
- 217
- 433
I usually use set bounds but feel free to look at other layouts like the gridbaglayout which allows you to make a grid like layout that you can put specific components in. Check out this link to see what it looks like. https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

- 147
- 10
-
-
*"I usually use set bounds.."* An answer should not include that (bad) advice, let alone start with it. – Andrew Thompson May 30 '15 at 04:24