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

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


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Border Example

Example -- A Titled, Etched, Empty Border around a Panel

Problem: Create a panel with 10 pixels of space around it, then an etched line with a title at the top left.

To do this, we need to create the etched border, then create a titled border from that. Then we put the titled border (which includes the etched border) together with the empty border to form a compound border. Then this compound border is added to the panel. Here are some ways to do this.

Solution 1 - Inline code

You need to create the borders and combine them. You could add the border in one massive statement, but here it's broken down for simplicity.
import javax.swing.border.*;
. . .
JPanel processPanel = new JPanel();
Border etchedBdr = BorderFactory.createEtchedBorder();
Border titledBdr = BorderFactory.createTitledBorder(etchedBdr, "Process");
Border emptyBdr  = BorderFactory.createEmptyBorder(10,10,10,10);
Border compoundBdr=BorderFactory.createCompoundBorder(titledBdr, emptyBdr);

Solution 2 - Utility method

It's common to use the same style of border on several panels. Write a utility method to do this, and save the method in your collection of handy methods.
  1. There will be a consistent border style.
  2. Changes will be made consistently.
  3. The initial extra writing is repaid with only two calls.
This example creates the same border as above.
import javax.swing.border.*;
. . .
// We can do this because the same border object can be reused.
    static final Border empty10Border = BorderFactory.createEmptyBorder(10,10,10,10);
    static final Border etchedBorder  = BorderFactory.createEtchedBorder();
. . .
public static Border myTitledBorder(String title) {
    return BorderFactory.createCompoundBorder(
               BorderFactory.createTitledBorder(etchedBorder, title), 
}//end myTitledBorder
. . .
    JPanel processPanel = new JPanel();
Ask programming questions?



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:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


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.

[an error occurred while processing this directive]