Breaking News
Friday, 28 September 2012

Swing layout in java

Swing layout :

In our simple Swing program, we specified the location of our various components inside the window with some constants: BorderLayout.CENTER, BorderLayout.SOUTH etc. In general, a window has a particular layout, and that when we add a component to the window, we add it in a particular position in that layout.


BoxLayout arranges components either horizontally or vertically in a panel. You can control alignment and spacing of the components. Complicated layouts can be made by combining many panels, some with horizontal layout and some with vertical layouts. Several classes are typically used: javax.swing.BoxLayout, javax.swing.Box, and javax.swing.Box.Filler.

To Create a JPanel with BoxLayout

Choose either a horizontal layout (BoxLayout.X_AXIS) or vertical layout (BoxLayout.Y_AXIS) for a JPanel.

JPanel p = new JPanel();

p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));


Unlike other layouts, the panel/container must be passed to the BoxLayout constructor.




This layout is represented by the Java class java.awt.BorderLayout, hence that is the class that the constants EAST, WEST, CENTER etc come from. With a BorderLayout, the window is assumed to contain five positions, as illustrated in Figure 1.

Figure 1: Positions in a BorderLayout.









This layout is useful in various typical situations where our window consists of some "main content", plus some other auxiliary component, for example:

  • the BorderLayout.CENTER position typically contains the "main" component of our window: it might be a table, a list of items, a graph etc;
  • the BorderLayout.NORTH position is useful for toolbars;
  • the BorderLayout.SOUTH position is typically used for a buttons bar or status bar.

To specify the layout of a frame, we would write something like:

frame.setLayout(new BorderLayout());


Another type of layout that it sometimes useful is GridLayout. As you might expect, this lays components out in a grid fashion, with a given number of rows and columns. You construct a GridLayout by saying how many rows and columns you require; you can also leave one of these two numbers as zero to mean "as many rows/columns as necessary":

// Lay components out as 4 rows of 3 columns
frame.setLayout(new GridLayout(4, 3));
// Lay components out with 5 columns
frame.setLayout(new GridLayout(0, 5));

When adding items to a frame with a GridLayout, no position is specified. Items are placed row by row (from top to bottom), column by column (from left to right).


This is the simplest layout manager in the Java Swing toolkit. It is mainly used in combination with other layout managers. When calculating its children size, a flow layout lets each component assume its natural (preferred) size.

The manager puts components into a row. In the order, they were added. If they do not fit into one row, they go into the next one. The components can be added from the right to the left or vice versa. The manager allows to align the components. Implicitly, the components are centered and there is 5px space among components and components and the edges of the container.

FlowLayout(int align)
FlowLayout(int align, int hgap, int vgap) 
    public FlowLayoutPane()
    // Use a FlowLayout layout manager. Left justify rows.
    // Leave 10 pixels of horizontal and vertical space between components.
    this.setLayout(new FlowLayout(FlowLayout.LEFT, 1010));

/\/ir@\/  <(.'.)>


Post a Comment

Thanks for comment

Toggle Footer