Is there any way to achieve custom javax.swing.border.Border
as shown in image?
Asked
Active
Viewed 8,428 times
5

Andrew Thompson
- 168,117
- 40
- 217
- 433

user225928
- 75
- 1
- 5
2 Answers
9
You simply need to extend AbstractBorder and override its paintBorder() method, here is one related example :
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
public class CustomMarginBorder
{
private JPanel contentPane;
private CustomBorder customBorder;
private void displayGUI()
{
JFrame frame = new JFrame("Custom Arrow Border Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
customBorder = new CustomBorder(Color.BLUE, 15);
contentPane = new JPanel();
contentPane.setBorder(customBorder);
frame.setContentPane(contentPane);
frame.setSize(300, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new CustomMarginBorder().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class CustomBorder extends AbstractBorder
{
private Color borderColour;
private int gap;
public CustomBorder(Color colour, int g)
{
borderColour = colour;
gap = g;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y
, int width
, int height)
{
super.paintBorder(c, g, x, y, width, height);
Graphics2D g2d = null;
if (g instanceof Graphics2D)
{
g2d = (Graphics2D) g;
g2d.setColor(borderColour);
//Left Border
g2d.draw(new Line2D.Double((double)x + 10, (double)y + 10
, (double)x + 10, (double)y + 20));
g2d.draw(new Line2D.Double( (double)x + 10, (double)y + 10
, (double)x + 20, (double)y + 10));
// Right Border
g2d.draw(new Line2D.Double( (double)width - 10, (double)y + 10
, (double)width - 10, (double)y + 20));
g2d.draw(new Line2D.Double( (double)width - 10, (double)y + 10
, (double)width - 20, (double)y + 10));
// Lower Left Border
g2d.draw(new Line2D.Double( (double)x + 10, (double)height - 10
, (double)x + 20, (double)height - 10));
g2d.draw(new Line2D.Double( (double)x + 10, (double)height - 10
, (double)x + 10, (double)height - 20));
// Lower Right Border
g2d.draw(new Line2D.Double( (double)width - 10, (double)height - 10
, (double)width - 20, (double)height - 10));
g2d.draw(new Line2D.Double( (double)width - 10, (double)height - 10
, (double)width - 10, (double)height - 20));
}
}
@Override
public Insets getBorderInsets(Component c)
{
return (getBorderInsets(c, new Insets(gap, gap, gap, gap)));
}
@Override
public Insets getBorderInsets(Component c, Insets insets)
{
insets.left = insets.top = insets.right = insets.bottom = gap;
return insets;
}
@Override
public boolean isBorderOpaque()
{
return true;
}
}
For Thick line Border use this :
class CustomBorder extends AbstractBorder
{
private Color borderColour;
private int gap;
private double rectWidth;
private double rectHeight;
public CustomBorder(Color colour, int g, double w, double h)
{
borderColour = colour;
gap = g;
rectWidth = w;
rectHeight = h;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y
, int width
, int height)
{
super.paintBorder(c, g, x, y, width, height);
Graphics2D g2d = null;
if (g instanceof Graphics2D)
{
g2d = (Graphics2D) g;
g2d.setColor(borderColour);
//Left Border
g2d.fill(new Rectangle2D.Double(
(double)x + gap
, (double)y + gap
, rectWidth, rectHeight));
g2d.fill(new Rectangle2D.Double(
(double)x + gap
, (double)y + gap + rectHeight
, rectHeight, rectWidth));
// Right Border
g2d.fill(new Rectangle2D.Double(
(double)width - gap - rectWidth
, (double)y + gap
, rectWidth, rectHeight));
g2d.fill(new Rectangle2D.Double(
(double)width - gap - rectHeight
, (double)y + gap + rectHeight
, rectHeight, rectWidth));
// Lower Left Border
g2d.fill(new Rectangle2D.Double(
(double)x + gap
, (double)height - gap - rectWidth
, rectHeight, rectWidth));
g2d.fill(new Rectangle2D.Double(
(double)x + gap
, (double)height - gap
, rectWidth, rectHeight));
// Lower Right Border
g2d.fill(new Rectangle2D.Double(
(double)width - gap - rectHeight
, (double)height - gap - rectWidth
, rectHeight, rectWidth));
g2d.fill(new Rectangle2D.Double(
(double)width - gap - rectWidth
, (double)height - gap
, rectWidth, rectHeight));
}
}
@Override
public Insets getBorderInsets(Component c)
{
return (getBorderInsets(c, new Insets(gap, gap, gap, gap)));
}
@Override
public Insets getBorderInsets(Component c, Insets insets)
{
insets.left = insets.top = insets.right = insets.bottom = gap;
return insets;
}
@Override
public boolean isBorderOpaque()
{
return true;
}
}
OUTPUT :
Border with thin and thick line
For

nIcE cOw
- 24,468
- 7
- 50
- 143
-
1..nice. :) See also [How do I create screenshots?](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) (for tips on making *great* screenshots). – Andrew Thompson Jul 24 '13 at 13:59
-
@AndrewThompson : My mouse is acting weird, seems like its time to replace one, since it seldom use to work when its being dragged. I added the link to my favourites, since you pointed me once before, regarding the same thingy :-). For the rest you're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Jul 24 '13 at 14:18
-
I was mainly thinking of how Alt+PrintScreen will 'auto-trim' the active window, not sure where that is buried in the long (long) thread now. ;) – Andrew Thompson Jul 24 '13 at 14:22
-
1Thanks for reply , `g2d.setStroke(new BasicStroke(5));` in Thin line Border Code can change to Thick line without changing all code. – user225928 Jul 25 '13 at 11:55