CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > CompoundBorder in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

CompoundBorder in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: March 23, 2021 ยท 2 min read ยท 0 Comments
Share: X in ๐Ÿ”—

In Java Swing, a CompoundBorder is a class that allows us to combine two different Border objects into a single border. This is useful when we want to apply multiple borders to a component, such as a panel or a button.

We can create a CompoundBorder using the BorderFactory.createCompoundBorder() method, which takes two Border objects as arguments. The first argument is the outer border, and the second argument is the inner border. The resulting CompoundBorder will have the appearance of the outer border, with the inner border inset by the outer border’s thickness.

Following is an example that demonstrates how to use CompoundBorder to apply two borders to a JPanel:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

public class CompoundBorderDemo extends JFrame {
    
    public CompoundBorderDemo() {
    	setLayout(new BorderLayout());
    	
        setTitle("CompoundBorder Demo");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.BLUE, 5),
            BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.YELLOW, Color.LIGHT_GRAY)
        ));
        add(panel, BorderLayout.NORTH);
        
        JLabel label = new JLabel("Hello, World!");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label);
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new CompoundBorderDemo();
    }

}

In this example, we create a JPanel and set its border using BorderFactory.createCompoundBorder(). We pass in two Border objects: a blue line border with a thickness of 5 pixels as the outer border, and an Etched border with a type RAISED, highlighting color yellow and Light Grays as shadows for the inner border. The resulting border is a blue border with yellow and light gray shadows around the panel.

Output:

ComoundBorder demo

You can experiment with different combinations of borders to achieve different effects using CompoundBorder.

Reference: https://docs.oracle.com/javase/tutorial/uiswing/components/border.html

Related Posts:

  • Nested and Inner Class in Java
  • Command Line Arguments in Java
  • Inner Thread Communication in Java
  • EtchedBorder in Java Swing
  • Control Statements in Java
  • JPanel in Java Swing
Tags:javaswing
Was this article helpful?
โ† Previous Article[Solved]- DNSHostNotFound: Failed to look up service “”: DNS name does not exist issue while connecting to MongoDb Atlas
Next Article โ†’JComboBox in Java Swing

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap