Dotted border on one side for JPanel

Is there a way to add a dotted border on only one side to a component Java Swing

like JPanel

or JLabel

?

The question does not duplicate the other question because this question is about a line border for one side. But I need a dotted border.

+3


source to share


2 answers


The question does not duplicate the other question because this question is about line border for one side

Yes, this is a duplicate, because the API shows you how to use MatteBorder

with Icon

one hand. So, all you have to do is create a Dotted Line Icon. See the example posted in my code below.

Or the second option, not listed in the wiring, is to use CompoundBorder

to create the desired effect:



Border empty = BorderFactory.createEmptyBorder(0, -1, -1, -1);
Border dashed = BorderFactory.createDashedBorder(null, 5, 5);
Border compound = new CompoundBorder(empty, dashed);

      

By setting the size EmptyBorder

to -1, you set the merged size to 0, so it DashedBorder

will only paint on one side.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class DashedBorder extends JPanel
{
    public DashedBorder()
    {
        //  Use an Icon in the MatteBorder

        JLabel label1 = new JLabel("Label Using a Matte Border");
        add(label1);

        Icon icon = new DashedLineIcon(Color.BLACK, 5, 1, 5, 0);
        Border matte = BorderFactory.createMatteBorder(1, 0, 0, 0, icon);
        label1.setBorder( matte );
        System.out.println(matte.getBorderInsets(label1));

        add(Box.createHorizontalStrut(100));
        //  Create a CompoundBorder using the DashedBorder

        JLabel label2 = new JLabel("Label Using a Dashed Border");
        add(label2);

        Border dashed = BorderFactory.createDashedBorder(null, 5, 5);
        Border empty = BorderFactory.createEmptyBorder(1, -1, -1, -1);
        Border compound = new CompoundBorder(empty, dashed);
        label2.setBorder( compound );
        System.out.println(compound.getBorderInsets(label2));

    }

    static public class DashedLineIcon implements Icon
    {
        private Color color;
        private int dashWidth;
        private int dashHeight;
        private int emptyWidth;
        private int emptyHeight;

        public DashedLineIcon(Color color, int dashWidth, int dashHeight, int emptyWidth, int emptyHeight )
        {
            this.color = color;
            this.dashWidth = dashWidth;
            this.dashHeight = dashHeight;
            this.emptyWidth = emptyWidth;
            this.emptyHeight = emptyHeight;
        }

        public int getIconWidth()
        {
            return dashWidth + emptyWidth;
        }

        public int getIconHeight()
        {
            return dashHeight + emptyHeight;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, dashWidth, dashHeight);
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Dashed Border");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DashedBorder());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

      

+2


source


And I ended up with this solution, although it's a little more complicated:

private void drawBorder(Graphics g){
  int x = this.getWidth();
  float dash[] = {1.0f};
  Graphics2D g2;
  BasicStroke stroke;

  g2 = (Graphics2D)g;
  stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dash, 0.0f);
  g2.setStroke(stroke);
  g2.setColor((Color) new Color(120, 120, 120));

  g2.draw(new Line2D.Double(0, 0, x, 0));
}

panel = new JPanel(){
  @Override
  public void paintComponent(Graphics g){
    drawBorder(g);
  }
};

      



Here I have drawn the top line using methods Graphics2D

and using a class BasicStroke

to create dashes, which are actually dots, because the density array dash

tells it that it has a space 1

between dashes by dashes of lengths.

-1


source







All Articles