Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

BorderLayout

java.awt.BorderLayout divides a container (eg, JPanel) into 5 geographical sections: North, South, East, West, and Center.

  • Fill their region. Components start at their preferred size, but will expand as needed to fill the region they are in.
  • One component per region. You can add at most one component to each region of a BorderLayout. To put more than one component in a section, put them in a panel (with its own layout), and then add that one panel to the border layout.
  • Where extra space goes. The size of a region is adjusted depending on what is in it. If there is nothing in an region, its size will be reduced to zero. Components in the North and South cells are stretched horizontally, and those in East and West are stretched vertically to fill all the space. The center will be stretched vertically and horizontally as needed, so it is a good place to put graphics or text areas that you want to expand.
  • Specifying the region. When you add components to a container which uses BorderLayout, specify the target region as the second parameter as, for example, BorderLayout.NORTH.

Resizing

The window on the left was created by the sample code below. This same window was made larger by dragging on the lower right corner. Note which components had horizontal space added to them and which had vertical space added to them.

To prevent component resizing, put a component into a Panel with a FlowLayout, and then put that panel in the BorderLayout. This is a common way to prevent resizing. The enclosing FlowLayout panel will stretch, but the component in it will not.

Not all regions are required

If nothing has been added to a region, the neighboring regions will expand to fill that space.

This window was created with 5 pixel gaps using only the NORTH, WEST, and CENTER regions. It was then resized, which added both vertical and horizontal space to the center, and each of the others was expanded as needed.

Constructors

If you don't need any space between regions, use the default constructor. You can also specify the number of pixels between regions.

p.setLayout(new BorderLayout());  // default is no gaps
p.setLayout(new BorderLayout(hgap, vgap);

Where hgap and vgap are the distances in pixels between the regions.

Example

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
// layouts/borderLayout/BorderTest.java - Demo use of BorderLayout
// Fred Swartz - 2004-11-03 (after Bush was elected).

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

///////////////////////////////////////////////// class BorderTest
class BorderTest {
    //================================================ method main
    public static void main(String[] args) {
        JFrame window = new BorderTestGUI();
        window.setTitle("BorderTest");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}

////////////////////////////////////////////// class BorderTestGUI
class BorderTestGUI extends JFrame {
    BorderTestGUI() {
        //... Create components (but without listeners)
        JButton north  = new JButton("North");
        JButton east   = new JButton("East");
        JButton south  = new JButton("South");
        JButton west   = new JButton("West");
        JButton center = new JButton("Center");
        
        //... Get content pane, set layout, add components
        Container content = this.getContentPane();
        content.setLayout(new BorderLayout());
        
        content.add(north , BorderLayout.NORTH);
        content.add(east  , BorderLayout.EAST);
        content.add(south , BorderLayout.SOUTH);
        content.add(west  , BorderLayout.WEST);
        content.add(center, BorderLayout.CENTER);
        this.pack();
    }
}
Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.