Custom Border

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.*;
import javax.swing.border.Border;

public class BorderDemo3 extends JApplet {
	JPanel p1;
	JButton b[];

	@Override
	public void init() {
		getContentPane().add(p1 = new JPanel());
		b = new JButton[15];
		for (int i = 0; i < b.length; i++) {
			b[i] = new JButton();
			b[i].setFont(new Font("Arial", Font.BOLD, 30));
		}

		// empty border
		b[0].setText("SimpleBorder");
		b[0].setBorder(new SimpleBorder());
		p1.add(b[0]);
	}
}

class SimpleBorder implements Border {
	int top;
	int left;
	int bottom;
	int right;
	Color color = null;

	public SimpleBorder() {
		this.top = 2;
		this.left = 4;
		this.bottom = 8;
		this.right = 10;
		this.color = Color.RED;
	}

	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width,
			int height) {
		Insets insets = getBorderInsets(c);
		if (color != null)
			g.setColor(color);
		g.fill3DRect(0, 0, width - insets.right, insets.top, true);

		g.fill3DRect(0, insets.top, insets.left, height - insets.top, true);
		g.fill3DRect(insets.left, height - insets.bottom, width - insets.left,
				insets.bottom, true);
		g.fill3DRect(width - insets.right, 0, insets.right, height
				- insets.bottom, true);
	}

	@Override
	public Insets getBorderInsets(Component c) {
		return new Insets(top, left, bottom, right);
	}

	@Override
	public boolean isBorderOpaque() {
		return true;
	}
}

Download

Borders in Java

Border factory Example - LineBorder,BevelBorder,TitledBorder,EtchedBorder etc
Border in Swing  
Custom Border


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code