How to change Button Border in Java Swing


For Button border, use createLineBorder() method in Java, which allows you to set the color of the Border as well:

JButton button = new JButton("Demo Button!");
Border border = BorderFactory.createLineBorder(Color.BLUE);

The following is an example to change button border in Java:

Example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Button Border");
      Container container = frame.getContentPane();
      JButton button = new JButton("Demo Button!");
      Border border = BorderFactory.createLineBorder(Color.BLUE);
      button.setBorder(border);
      container.add(button, BorderLayout.CENTER);
      frame.setSize(550, 300);
      frame.setVisible(true);
   }
}

Output

Updated on: 30-Jul-2019

803 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements