add Title Border to JComponent - Java Swing

Java examples for Swing:Border

Description

add Title Border to JComponent

Demo Code


//package com.java2s;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.border.Border;

public class Main {
    public static void addTitleBorder(JComponent comp, String title) {
        comp.setBorder(BorderFactory.createTitledBorder(comp.getBorder(),
                title));/*w  w w  . jav  a 2s  . c  o  m*/
    }

    public static void addTitleBorder(JComponent comp, String title,
            int margin) {
        addTitleBorder(comp, title, margin, true);
    }

    public static void addTitleBorder(JComponent comp, String title,
            int margin, boolean origBorderInside) {
        comp.setBorder(BorderFactory.createTitledBorder(
                createCompoundBorder(createEmptyBorder(margin),
                        comp.getBorder(), origBorderInside), title));
    }

    public static Border createCompoundBorder(Border border1,
            Border border2, boolean border2Inside) {
        if (border2Inside) {
            return BorderFactory.createCompoundBorder(border1, border2);
        } else {
            return BorderFactory.createCompoundBorder(border2, border1);
        }
    }

    public static Border createEmptyBorder(int margin) {
        return BorderFactory.createEmptyBorder(margin, margin, margin,
                margin);
    }
}

Related Tutorials