The FlowLayout manager is a good place to start as it is the default for Applets and Panels The FlowLayout manager simply places components on a background one after the other from left to right. If it runs out of space to the right it simply wraps around the components to the next line.

The following code creates a very simple application and adds a series of buttons

import javax.swing.*;
import java.awt.*;

public class FlowAp extends JFrame{
    public static void main(String argv[]){
	FlowAp fa=new FlowAp();
	//Change from BorderLayout default
	fa.getContentPane().setLayout(new FlowLayout());

	JButton one = new JButton("One");
	JButton two = new JButton("Two");
	JButton three = new JButton("Three");
	JButton four = new JButton("four");
	JButton five = new JButton("five");
	JButton six = new JButton("Six");

    }//End of constructor

}//End of Application

The following image is the default appearance when you fire it up from the command line.

If you change the width of the application by dragging the right hand side of the Frame however, the program re-configures the layout of the buttons thus.

Bear in mind that both images are the display for exactly the same java code. The only thing that has changed is the width. The FlowLayout manager automatically changes the layout of the components when the Frame is re-sized. If you were to make the Frame very small the FlowLayout manager would change the layout so that the buttons were wrapped around in several rows.

When you first come across this approach to the management of components it may seem a little arbitrary. Some of the GUI building tools such as NetBeans and Borland/Inprise JBuilder offer ways of specifically placing components. For instance Borland JBuilder offers the XYLayout that gives you this functionality.

Last modified: Sunday, 20 September 2015, 07:21 PM