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(JFrame.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);
            }
        });
    }
}

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply