Java Swing BorderFactory Example

By | 2017-11-19T09:06:56+00:00 November 19th, 2017|java|0 Comments

In this tutorial, we are going to learn how to apply borders using Swing BorderFactory class.

Swing BorderFactory :

BorderFactory is a Factory class which produces different types of borders (Border instances). Here I am going to show how to apply borders for swing components.

Swing BorderFactory Example:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyBorderFrame extends JFrame {
    JButton ok, cancel;
    Container contentPane;
    JPanel panel;
    Font fontTitle;

    MyBorderFrame() {
        super("Border and Sizes Demo");

        panel = new JPanel();

        ok = new JButton("Ok");
        cancel = new JButton("Cancel");

        ok.setPreferredSize(new Dimension(100, 25));
        cancel.setPreferredSize(new Dimension(100, 25));

        contentPane = getContentPane();

        contentPane.setLayout(new FlowLayout());

        fontTitle = new Font("Comic Sans Ms", Font.BOLD + Font.ITALIC, 12);





class BorderDemo {
    public static void main(String args[]) {
        MyBorderFrame frame = new MyBorderFrame();
        frame.setSize(400, 100);

Output :

Line Border :

Swing BorderFactory Line

Bevel Border :



Swing BorderFactory Bevel

Etched Border :

"Titled Border", TitledBorder.RIGHT, TitledBorder.TOP,

Swing BorderFactory Etched

Bevel and Etched Border :

"Font Names", TitledBorder.RIGHT, TitledBorder.TOP, fontTitle,;

Swing BorderFactory Bevel Etched

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment