Add to Google Reader or Homepage

JPanel Border

How to add Borders to JPanel

An user interface design, normally requires us to include many a number of components in the application window(JFrame and JPanel in our case). In which there may be a need to visually separate the components in order to reduce the fuzziness.

Say for an example,If you have ever worked in the MS Word you could have noticed in the print window that the components used to select the page range will be separated from the components used to select the number of copies with the help of borders.

Thus borders helps in  visually grouping the components that are related,thereby making things clear.

In this Post,I will show you how to add borders to a panel and what are the options available to us in enhancing the UI design.

To add borders to our Panel,

 1.First create the object of Border class.

2.By using the static methods of BorderFatory class you can create borders like 

a.Titled Border - use BorderFatory.createTitledBorder()

b.EtchedBorder-use BorderFatory.createEtchedBorder()

c. Empty Border-use BorderFatory.createEmptyBorder()

d.Lowered Bevel Border-use BorderFatory.createLoweredBevelBorder()

e.Raised Bevel Border...-use BorderFatory.createRaisedBevelBorder()

except SoftLevelBorder which is available as separate class.

3. Then assign the resultant object to the Border object  created in the first step.

4. Using the panel's setBorder (Border b) method  we can set the desired border for our panel by passing the Border object as argument.


Border b= BorderFactory.createTitledBorder("My Border");

Check out the API of BorderFactory class to know the signature of various methods which i have mentioned before.

A simple program that explains how to add borders to a panel is shown below.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.*;
class Borderexframe extends JFrame
public Borderexframe()
setLayout(new GridLayout(3,1));// create sections for three panels


// firstpanel and its components

firstpanel=new JPanel();
JButton b1=new JButton("LineBorder");
JButton b2=new JButton("Etched");
JButton b3=new JButton("TitledBorder");
createBorder(b1,new LineBorder(Color.GREEN,5,true));
createBorder(b3,BorderFactory.createTitledBorder(new SoftBevelBorder(EtchedBorder.RAISED),"MY BORDER",TitledBorder.CENTER,TitledBorder.TOP));

//second panel and its components

secondpanel=new JPanel();
JRadioButton yellow=new JRadioButton("Yellow");
JRadioButton blue=new JRadioButton("BLUE");
secondpanel.setBorder(BorderFactory.createTitledBorder(new SoftBevelBorder(EtchedBorder.RAISED),"BACKGROUND",TitledBorder.CENTER,TitledBorder.ABOVE_TOP));

// thirdpanel and its components

thirdpanel=new JPanel();

public void createBorder(JButton b,final Border bo)
b.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)

public void createRadioButton(JRadioButton rb,final Color c)

rb.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)

private final JPanel firstpanel;
private final JPanel secondpanel;
private final JPanel thirdpanel;

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

class Borderframemain
public static void main(String args[])throws Exception
Borderexframe f1=new Borderexframe();


We can also be able combine one or more borders to produce the combined effect.To do so use the following method.

BorderFactory.CompoundBorder(Border Out-side,Border in-side)

java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top