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.



Example:


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


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.

 

Program


Borderexframe.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.*;
class Borderexframe extends JFrame
{
public Borderexframe()
{
setTitle("BorderframeDemo");
setSize(400,400);
setLocationByPlatform(true);
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");
add(firstpanel);
createBorder(b1,new LineBorder(Color.GREEN,5,true));
createBorder(b2,BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.RED,Color.ORANGE));
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.add(yellow);
secondpanel.add(blue);
createRadioButton(yellow,Color.YELLOW);
createRadioButton(blue,Color.BLUE);
secondpanel.setBorder(BorderFactory.createTitledBorder(new SoftBevelBorder(EtchedBorder.RAISED),"BACKGROUND",TitledBorder.CENTER,TitledBorder.ABOVE_TOP));
add(secondpanel);

// thirdpanel and its components

thirdpanel=new JPanel();
add(thirdpanel);
}

 
public void createBorder(JButton b,final Border bo)
{
firstpanel.add(b);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
thirdpanel.setBorder(bo);
}
});
}

public void createRadioButton(JRadioButton rb,final Color c)
{

secondpanel.add(rb);
rb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
secondpanel.setBackground(c);
}
});

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

Borderframemain.java

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

class Borderframemain
{
public static void main(String args[])throws Exception
{
Borderexframe f1=new Borderexframe();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
}


Output



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)


0 comments:

Post a Comment

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