Borders Line,Etched, Matte and Beveled

  

  Borders in Swing are very useful not only for adding simple border lines to a component but also to add titles, create bevel effects and adding spaces around components. Almost all Java Components [with the exception of Labels and Panels] create there own borders in most Look and Feels.
   In this post, let’s check some of the predefined borders provided by Swing and how to create and use them.

  All Swing Borders should implement the javax.swing.border.Border interface. Although you can create your own borders by implementing this interface, you most probably will use one of the predefined Borders in Swing. These Borders include the following
    1. Line Border - Creates a border with Single Line
    2. Empty Border - Creates a whitespace (or) Padding around the component
    3. Bevel Border - Creates a appearence of raised or lowered effect on the component
    4. Matte Border - Creates a colored Border
    5. Titled Border - Creates a Line Border, but with a title
    6. Compound Border - Creates a Border with one or two borders nested

How to Create a Predefined Border Using BorderFactory?
   The best way to create a predefined border you use one of the Factrory methods in the javax.swing.BorderFactory class. These methods are all named like this: createXXXBorder(..), where XXX is the type of the border for eg., line, matt etc.

   So for example to create a Line Border, you would use the following method

//creates red colored border
BorderFactory.createLineBorder(Color.RED); 
//Creates a red colored border, 3 px in width
BorderFactory.createLineBorder(Color.RED, 3);

There are other similar Methods, which you can check here.

Creating Predefined Borders without the BorderFactory

   Each predefined Border , has it’s own class, instance of which is created by the BorderFactory class. So, if you require, instead of relying on the BorderFactory Methods, you can create your own border instances. For eg., instead of creating a LineBorder by calling the BorderFactory.createLineBorder(..), method, you can directly create a LineBorder as below.

  //create a blue colored border
  new LineBorder(Color.Blue);

Why Using a BorderFactory is Better?
    As one can note, many border types, as opposed to a TextField for eg., dosen’t have any instance specific values. So it makes sense that these borders are shared. That’s exactly what the BorderFactory does. It creates instances only when required, Otherwise caches an existing instance and returns it. So, it’s highly recommended that you use BorderFactory to create Borders, instead of constructors, especially if you are using it to create borders like, LineBorder, BeveledBorder or EtchedBorders that don’t have induvidual properties for each instance.

   In the next article, I will explain how to create Custom Borders by extending the AbstractBorder class.







One Response to 'Borders Line,Etched, Matte and Beveled'

  1. Creating a Custom Border » Java Swing - May 20th, 2008 at 2:52 pm

    […] « Borders Line,Etched, Matte and Beveled […]


Leave a Reply





Popular Articles

Blog Categories

Monthly Archives

Resources