How to set a MatteBorder from BorderFactory in Java?

Java 8Object Oriented ProgrammingProgramming

Set a MatteBorder from BorderFactory class −

MatteBorder border = (MatteBorder)BorderFactory.createMatteBorder(2, -1, 5, 10, icon);

Now, set the above created MatteBorder to a component −

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

The following is an example to set a MatteBorder from BorderFactory class −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
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);
      ImageIcon icon = new ImageIcon("new.gif");
      MatteBorder border = (MatteBorder)BorderFactory.createMatteBorder(2, -1, 5, 10, icon);
      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 31-May-2019 10:59:52
Advertisements