Layout Management

Java has a huge library of visual components, but these aren't very useful if you can't place them where you would like.

Java programmers typically try to program as portably as possible, so this means not specifying component positions in terms of pixels, but in relation to the edges of each other and of the container they are in., There are a number of classes that are capable of doing this, these are called layout managers. We will cover some of the more useful layout managers.

It is possible, given a good understanding of Java, to write your own layout manager, but this isn't normally necessary, as the layout managers that Java comes with are very flexible.

BorderLayout

BorderLayout documentation

Lays out components in terms of NORTH, SOUTH, EAST, WEST and CENTER (American spelling).

This code has some errors:

import java.awt.BorderLayout;

import javax.xwing.JFrame;
import javax.swing.JLabel;

public class BorderLayoutExample
{
	public staic void main(String[] args)
	{
		JFrame frame=new JFrame("BorderLayout Example";

		frame.setSize(400,400);

		frame.setLayout(new BorderLayout())

		JButton topButton=new JButton("Top");
		JButton bottomButton=new JButton("Bottom");
		JButton leftButton=new JButton("Left");
		JButton rightButton=new JButton("Right");
		JButton centreButton=new JButton("Centre");

		frame.add(topButton,BorderLayout.NORTH);
		frame.add(bottomButton,BorderLayout.SOUTH);
		frame.add(leftButton,BorderLayout.WEST);
		frame.add(rightButton,BorderLayout.EAST);

		frame.add(centreButton,BorderLayout.CENTER);
		frame.setVisible(false);
	}
}

Have a go at making this code compile and run successfully. There are a few deliberate errors in there, so make sure you analyse the compiler output and when you finally get it to compile, you still might not see anything, so follow the code carefully and spot the deliberate logic error.

Have a go at resizing the frame to see what happens to the components inside, especially when you make the frame really small.

BorderLayout is the default layout manager for a JFrame, so if you remove the line with setLayout in the code above, you should see no difference.

FlowLayout

FlowLayout tutorial

This layout manager lays components out starting at the top then places them in a row at the top, until there is no more room, then starts a new row, similar to how a word processor deals with words. Again there are some deliberate mistakes in the code:

import java.awt.FlowLayout;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Jspinner;

public class FlowLayoutExample
{
	public static void main(String[] args)
	{
		JFrame frame=new JFrame();
		frame.setSize(400,400);

		frame.setLayout(new FlowLayout());

		JCheckBox box=new JCheckBox();
		frame.add(box);
		
		JLabel label=new JLabel("A label");
		frame.add(label);

		JSpinner spinner=new JSpinner();
		frame.add(spinner);

		JLabel longLable=new JLabel
		(
			"A really long label that will hopefully"+
			"go on the next line"
		);
		
		frame.add(longLabel);

		frame.setVisible(true);
	}
}

If you change the new FlowLayout() to new FlowLayout(FlowLayout.LEFT) the rows will be aligned to the left. FlowLayout is the default layout for JPanel (a general container class that we can use to lay out some parts of a frame differently to other parts).

BoxLayout

BoxLayout documentation

BoxLayout is similar to FlowLayout, except it can only lay out one row, and it can make the row vertical (though normally this is called a column).

This initially doesn't sound very useful, but it's simple to make quite complex-looking layouts with BoxLayout because you can nest a JPanel inside the JFrame, and give the nested JPanel another layout manager (which can be another BoxLayout).

Again, there are some errors in this code:

import java.awt.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFraim;
import javax.swing.JLabel;
import javax.swing.JTextField;

class BoxLayoutExample
{
	public static void main(String[] args)
	{
		JFrame frame=new JFrame();
		frame.setSize(400,4400);

		BoxLayout mainLayout=new BoxLayout frame.getContentPane(),BoxLayout.Y_AXIS);

		frame.setLayout(mainLayout);

		JLabel thoughtsLabel=
			=new JLabel("Enter your thoughts on Java.");

		frame.add(thoughtsLabel);

		JTextField textField=new JTextField();
		frame.add(okButton);

		JButton okButton=new JButton("OK');
		frame.add(okButton);

		JButton cancelButton=new JButton("Cancel");
		frame.add(cancelButton);

		frame.setVisible(true);
	}
}