How to set color to MatteBorder in Java?

Java 8Object Oriented ProgrammingProgramming

Set color to MatteBorder using the Color class −

Border border = new MatteBorder(5, 10, 5, 5, Color.BLUE);

Now, set it to a button component in Java −

JButton button = new JButton("Matte Border");
button.setBorder(border);

The following is an example to set color to MatteBorder in Java −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);
      Border border = new MatteBorder(5, 10, 5, 5, Color.BLUE);
      JButton raisedButton = new JButton("Raised Border");
      raisedButton.setBorder(raisedBorder);
      JButton button = new JButton("Matte Border");
      button.setBorder(border);
      Container contentPane = frame.getContentPane();
      contentPane.add(raisedButton,BorderLayout.NORTH);
      contentPane.add(button,BorderLayout.EAST);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

Output

raja
Published on 04-Jun-2019 07:32:37
Advertisements