How do I create a compound border?

In this example we create a compound border, a border around border. Here, instead of creating a new instance of Border class directly we create the border using the BorderFactory factory class.

package org.kodejava.example.swing;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.*;

public class CompoundBorderExample extends JFrame {
    public CompoundBorderExample() {
        initializeUI();
    }

    private void initializeUI() {
        setSize(300, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        BevelBorder raisedBevel = (BevelBorder) BorderFactory.createBevelBorder(BevelBorder.RAISED);
        BevelBorder loweredBevel = (BevelBorder) BorderFactory.createBevelBorder(BevelBorder.LOWERED);

        Border border = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel);
        JPanel panel = new JPanel();
        panel.setBorder(border);

        setContentPane(panel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CompoundBorderExample().setVisible(true);
            }
        });
    }
}

How do I create Border for swing component?

This example shows how we can create border for swing components. In the example below we set border of a JPanel component. Some border implementation that we use below including LineBorder, BevelBorder, EtchedBorder and MatteBorder.

package org.kodejava.example.swing;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;
import java.awt.*;

public class BorderDemo extends JFrame {

    public BorderDemo() {
        initializeUI();
    }

    private void initializeUI() {
        setSize(400, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(15, 15));

        JPanel top = new JPanel();
        top.setBorder(new LineBorder(Color.RED, 1, true));

        JPanel bottom = new JPanel();
        bottom.setBorder(new BevelBorder(BevelBorder.LOWERED));

        JPanel left = new JPanel();
        left.setBorder(new EtchedBorder(EtchedBorder.RAISED));

        JPanel right = new JPanel();
        right.setBorder(new MatteBorder(5, 5, 5, 5, Color.BLUE));

        JPanel center = new JPanel();
        center.setBorder(new BevelBorder(BevelBorder.RAISED));

        getContentPane().add(top, BorderLayout.NORTH);
        getContentPane().add(bottom, BorderLayout.SOUTH);
        getContentPane().add(left, BorderLayout.WEST);
        getContentPane().add(right, BorderLayout.EAST);
        getContentPane().add(center, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new BorderDemo().setVisible(true);
            }
        });
    }
}