Introduction
A good layout manager proves essential for creating a good graphical user interface (GUI), but many beginner developers find the standard Java layout managers difficult to master. Of these, BorderLayout and FlowLayout generally prove useful, but developers often start running into difficulties when coding more complex layouts. GridBagLayout, in particular, is very powerful, but many developers find it difficult and non-intuitive. Following the principle, if you are surprised at a layout manager's behavior, you can get quite frustrated. On this basis, GridBagLayout gets particularly poor marks. With that in mind, a better way to get similar results must exist.
Partition Layout Manager
PartitionLayout is a totally free layout manager designed to make creating user interfaces fast and easy. It retains powerful features of GridBagLayout and removes its complexity. It is remarkably simple and easy to use and gives exactly the same results as expected. Isn't that wonderful!!
PartitionLayout is a powerful layout manager that lays out components based on a grid. You can think of PartitionLayout as a simpler, clear and less complex version of GridBagLayout. PartitionLayout supports a rectangular grid of cells. The major difference between GridBagLayout and PartitionLayout is that PartitionLayout displays components in a grid with exact specification provided by the user without adding surprise changes to the layout by adding its own intelligence as GridBagLayout does. Also in PartitionLayout all the components are given equal amounts of extra space when available. Each component of a PartitionLayout can occupy one or more cells. Use the PartitionLayout when you need tabular layouts and when each component should grow equally when extra space is available.
With PartitionLayout, users can specify layout properties for each component as to where the component will be displayed on the grid.
Because PartitionLayout enables you to specify layout properties for each component, you must associate components managed by PartitionLayout with instances of the class PartitionConstraints. These instances specify how PartitionLayout should lay out components in the grid.
PartitionLayout - A Simple Example
This example illustrates how components that span multiple cells can be added easily to the grid.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ExPartitionLayout {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new PartitionLayout());
PartitionConstraints gbc = new PartitionConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = PartitionConstraints.BOTH;
gbc.anchor = PartitionConstraints.NORTH;
gbc.ipadx = 50;
JButton btn1 = new JButton("Button 1");
pane.add(btn1, gbc);
PartitionConstraints gbc2 = new PartitionConstraints();
gbc2.gridx = 1;
gbc2.gridy =0;
gbc2.gridwidth = 2;
gbc2.gridheight = 2;
gbc2.fill = PartitionConstraints.BOTH;
JButton btn2 = new JButton("Button 2");
pane.add(btn2, gbc2);
PartitionConstraints gbc3 = new PartitionConstraints();
gbc3.gridx = 0;
gbc3.gridy =1;
gbc3.gridwidth = 1;
gbc3.gridheight = 2;
gbc3.fill = PartitionConstraints.BOTH;
JButton btn3 = new JButton("Button 3");
pane.add(btn3, gbc3);
PartitionConstraints gbc4 = new PartitionConstraints();
gbc4.gridx = 1;
gbc4.gridy =2;
gbc4.gridwidth = 2;
gbc4.gridheight = 1;
gbc4.fill = PartitionConstraints.BOTH;
JButton btn4 = new JButton("Button 4");
pane.add(btn4, gbc4);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("PartitionLayoutDemo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() {
createAndShowGUI();} });
}
}
PartitionLayout Download
To use this layout manager we recommend downloading a jar file named
partitionLayout.jar. Click on that link below, and then save it to disk on your
PC when invited to do so. Before we give the link, here
are the instructions on using the jar file with your application.
To use the PartitionLayout manager in your code, you want to save
on your PC the file partitionLayout.jar
In your code, call it MyApplication.java, place the line
import partitionLayout.* ;
Let us assume you have a folder C:\jars which contains
partitionLayout.jar
In your command window you now type these two lines:
javac -classpath C:\jars\partitionLayout.jar MyApplication.java
java -classpath .;C:\jars\partitionLayout.jar MyApplication
Here is the link. Firefox downloads a jar file, IE wants to zip it.
partitionLayout.jar