How to change Button Border in Java Swing

Java 8Object Oriented ProgrammingProgramming

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

raja
Published on 06-May-2019 10:41:03
Advertisements