What are different types of borders used in Swing? Explain all borders with suitable example.

 USING BORDERS

Every JComponent can have one or more borders. Borders are useful not only for drawing lines and fancy edges but also for providing titles and empty space around components. To put a border around a JComponent, we use its setBorder method. We can use the Border Factory class to create most of the borders that Swing provides. Different borders defined in javax.swing.border package are:

BevelBorder: We can create two types of bevel borders such as raised bevel and lowered bevel. The following two statements use these two borders:-

For Raised Bevel Border

pane.set Border (BorderFactory.createRaisedBevelBorder());

//pane is object of Jpanel

Output looks like



For Lowered Bevel Border

pane.setBorder (BorderFactory.createLowered BevelBorder());

Output looks like




Line Border: A flat border with a specified thickness and color. For creating line border BorderFactory provides methods such as createLineBorder. The following statement use Line border:

pane.setBorder(Border Factory.createLineBorder());

Output looks like



Etched Border: A border with an etched (imprinted) line appearance. Java provides the createEtched Border method to create an etched border. Following statement use Etched Border:

pane.setBorder(Border Factory.createEtched Border());




Titled Border: A border allowing a string title in a specific location and position. For creating the border with title, we can use the createTitledBorder method, also we can justify is alignment and its position as follows: 

pane.setBorder(Border Factory.create TitledBorder("Border Title"));

Output looks like



Matte Border: A border consisting of either a flat color or tiled image. A matte border displays a matte pattern. The matte pattern is created by using either a solid color or an icon. Solid matte borders are created by using the specified color. Tiled matte borders are created by using an icon that displays a matte picture.

Imagelcon icon= new Imagelcon("rem.gif");

pane.setBorder(BorderFactory.createMatteBorder(-1,-1,-1,-1,icon));

Output looks like



Compound Border: If we want to use two types of borders then Java provides CompoundBorder which is a combination of two borders where one type of border is around the other border. Java BorderFactory provides the createCompoundBorder method which takes two arguments as Borders and create one compound border. In the below example, we have used line border and raised bevel border to create a Compound.

BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.blue,3), BorderFactory.createLowered Bevel Border()):



Complete Example of all type of border used in swing

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

class BorderDemo extends JFrame

{

JPanel p1, p2,p3, sp1, sp2;

JLabel 1;

Border Demo ()

{

setSize(500, 400);

setDefaultLookAndFeelDecorated (true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("Border Demo");

setLayout (new Border Layout());

//Creating Jpanel Objects

p1=new JPanel();

p2=new JPanel();

p3=new JPanel();

sp1=new JPanel();

sp2=new JPanel();

//Setting size of Jpanels

p1.setPreferredSize (new Dimension (500, 50));

p2.setPreferredSize(new Dimension (50,400));

//Setting Borders

pl.setBorder (BorderFactory.createRaisedBevelBorder()); p2.setBorder (Border Factory.createRaised BevelBorder());

p3.setBorder (BorderFactory.createTitledBorder("Registration

Form"));

//Adding Panels in frame

add (p1, BorderLayout. NORTH); add (p2, BorderLayout.WEST);

add (p3, BorderLayout.CENTER);

p3.setLayout (new GridLayout()); //setting grid layout in p3

//setting border for sub panels

sp1.setBorder (BorderFactory.createLineBorder (Color.blue, 2)); sp2.setBorder (BorderFactory.createCompound Border

(BorderFactory. createLowered BevelBorder(), BorderFactory.createLineBorder (Color.red, 2)));

//adding panels to p3

p3.add(spl);

p3.add(sp2);

//adding Labels in Panels

pl.add(new JLabel("Tool Bar Here", JLabel.CENTER));

p2.add(new JLabel("Buttons Here", JLabel.CENTER));

sp1.add(new JLabel("Labels Here",JLabel.CENTER));

sp2.add(new JLabel("Input Contols Here", JLabel.CENTER));

setVisible(true);

}

public static void main(String args[])

{

Border Demo frame=new Border Demo();

}

}


                            



Comments

Popular posts from this blog

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Explain Parallel Efficiency of MapReduce.

Short Note on Security Architecture of E-governance