0

I am having problems setting the thickness of my border. I wanted to have a JPanel with a rounded dashed border. I was able to set it rounded by overriding the paintComponent to make it round. However, when I set the stroke to make the border thicker and dashed it does not work. I use the setStroke() method. My code is the following

private void loadTopPane() {

    JPanel topSection = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Dimension arcs = new Dimension(15, 15); // Border corners arcs
                                                    // {width,height},
                                                    // change this to
                                                    // whatever you want
            int width = getWidth();
            int height = getHeight();
            Graphics2D graphics = (Graphics2D) g;
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            float dash1[] = { 10.0f };
            final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
                    dash1, 0.0f);

            // Draws the rounded panel with borders.
            graphics.setColor(getBackground());
            graphics.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
                                                                                            // background
            graphics.setColor(getForeground());
            graphics.drawRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
                                                                                            // border
            graphics.setStroke(dashed);

        }
    };

    topSection.setLayout(null);
    topSection.setSize(1150, 175);
    topSection.setBackground(new Color(222, 225, 226));

    topSection.setBounds(25, 13, topSection.getPreferredSize().width, topSection.getPreferredSize().height);

    topSection.add(new JLabel("TESTING"));
    topSection.setBounds(20, 10, 1180, 180);
    frame.add(topSection);

}

So the output shows me a JPanel with a rounded border but it does not give me a border that is dashed and thicker. How can I fix this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Pablo
  • 91
  • 2
  • 6
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 15 '16 at 05:05
  • `topSection.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 15 '16 at 05:07
  • Have you considered call `setStroke` BEFORE you paint the border? – MadProgrammer May 15 '16 at 06:00

1 Answers1

2

Have you considered calling setStroke BEFORE you paint the border...

Stroked

float dash1[] = {10.0f};
final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
                                                                                     dash1, 0.0f);
graphics.setStroke(dashed);

// Draws the rounded panel with borders.
graphics.setColor(getBackground());
graphics.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
// background
graphics.setColor(getForeground());
graphics.drawRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366