Java adds dashed or dashed border to JPanel

I'm having problems with this thing: can I somehow add a dashed (or dashed, it doesn't matter) border to JPanel?

I was looking for SO questions, but no one seems to have asked this before.

I am wondering if there is any class to use. actually i use:

myPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 

Obviously, this is a standard class that gives only a few standard boundaries, no one is useful to me.

+4
source share
2 answers
+4
source

Starting with Java 7, you can use BorderFactory.createDashedBorder(Paint) .

Before Java 7, you must define this boundary yourself. Then you can use this self-written border:

 private class DashedBorder extends AbstractBorder { @Override public void paintBorder(Component comp, Graphics g, int x, int y, int w, int h) { Graphics2D gg = (Graphics2D) g; gg.setColor(Color.GRAY); gg.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{1}, 0)); gg.drawRect(x, y, w - 1, h - 1); } } 
+11
source

Source: https://habr.com/ru/post/1437919/


All Articles