Borders in Java Swing.

Border in Java

You can create lot of borders in Java.To create java border, Java provides separate class for each java border. There are two ways using which we can create the border in java, i.e. using constructor or using java BorderFactory. If we want to use custom values then creating border through constructor is preferable but typically java BorderFactory is used to create java border because of following reasons:

Java BorderFactory Advantages

1. Java BorderFactory uses factory pattern, so it uses shared resources and will not create java Border objects every time.
2. Java BorderFactory uses values which are compatible with current look and feel.

Now lets see how we can use java BorderFactory to create different types of Borders in Java using java border example for each border type.

Use of Java BorderFactory to create Border in java

1. Creating line border in java
For creating line border BorderFactory provides method such as createLineBorder as follows :
Java Border Example 1:
        Label centerLabel = new Label (" Border Demo ");
        LineBorder line = (LineBorder)BorderFactory.createLineBorder( Color.BLUE );//createLineBorder creates Line border in java
        testPanel.setBorder( line );

2. Creating Etched Border in java

Java Borderfactory provides createEtchedBorder method to create etched border.
Java Border Example 2:

        EtchedBorder etched = (EtchedBorder)BorderFactory.createEtchedBorder(););//createEtchedBorder creates etched border in java

        testPanel.setBorder( etched );

3. Creating Bevel Border in java

We can create two types of bevel borders such as raised bevel and lowered bevel. In following java border example we will create these two borders.
Java Border Example 3:

        BevelBorder raisedBevel = (BevelBorder)BorderFactory.createRaisedBevelBorder();
        testPanel.setBorder( raisedBevel );
        BevelBorder loweredBevel = (BevelBorder)BorderFactory.createLoweredBevelBorder();
        testPanel.setBorder( loweredBevel );

[caption id="attachment_687" align="alignleft" width="196" caption="Raised Bevel Border Example"][/caption]

[caption id="attachment_688" align="alignleft" width="219" caption="Lowered Bevel Border Example"][/caption]

4. Adding title to Border in java

For creating the border with title, we can use createTitledBorder method , also we can justify is alignment and its position as follows :
Java Border Example 1:

        TitledBorder borderWithTitle = BorderFactory.createTitledBorder( "Title Name" );
        borderWithTitle.setTitleJustification( TitledBorder.CENTER ); //Set title at center
        //borderWithTitle.setTitlePosition( TitledBorder.BOTTOM ); //Set title at bottom
        testPanel.setBorder( titledBorder );

5. Creating Compound border

If we want to use two types of borders then Java provides CompoundBorder which is combination of two borders where one type of border is around the other border. Java BorderFactory provides createCompoundBorder method which takes two arguments as Borders and create one compound border. In below example we have used line border and raised bevel border to create Compound border.
Java Border Example 5
        Border compoundBorder = BorderFactory.createCompoundBorder( line, raisedBevel );
        testPanel.setBorder( compoundBorder );

Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment