Java Swing BorderFactory Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn how to apply borders using Swing BorderFactory class.
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.
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);
        panel.setBorder(BorderFactory.createLineBorder(Color.blue));

        panel.add(ok);
        panel.add(cancel);

        contentPane.add(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

class BorderDemo {
    public static void main(String args[]) {
        MyBorderFrame frame = new MyBorderFrame();
        frame.setSize(400, 100);
        frame.setVisible(true);
    }
}
Output :
Line Border :
Bevel Border :
panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

 
Etched Border :
panel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.RAISED),
"Titled Border", TitledBorder.RIGHT, TitledBorder.TOP,
fontTitle, Color.red));

Bevel and Etched Border :
panel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createBevelBorder(BevelBorder.LOWERED),
"Font Names", TitledBorder.RIGHT, TitledBorder.TOP, fontTitle,
Color.red));

Happy Learning 🙂

How to change Look and Feel of Swing setLookAndFeel
Java JColorChooser Example
Java Swing JLabel Example
Java Swing Login Example
Java Swing JSplitPane Example
Java Swing ProgressBar Example
Java Swing JTable Example
Java Swing Advanced JTable Example
Java Swing JTabbedPane Example
Java Swing JMenu Example
Java Swing JToolBar Example
Java Swing JOptionPane Example
Java Swing JTree Example
How to create Java Smiley Swing
How to create Java Rainbow using Swing

How to change Look and Feel of Swing setLookAndFeel
Java JColorChooser Example
Java Swing JLabel Example
Java Swing Login Example
Java Swing JSplitPane Example
Java Swing ProgressBar Example
Java Swing JTable Example
Java Swing Advanced JTable Example
Java Swing JTabbedPane Example
Java Swing JMenu Example
Java Swing JToolBar Example
Java Swing JOptionPane Example
Java Swing JTree Example
How to create Java Smiley Swing
How to create Java Rainbow using Swing
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String