Java Swing | MatteBorder
MatteBorder is a class which is used to make a matte-like border of either a solid color or a tiled icon. It is a part of javax.swing.border package and contains different styles of Borders for Components. This class helps us to set an icon or a solid color as a border and the insets of the border can also be applied using this class.
Constructor of the class are:
- MatteBorder(Icon tileIcon):Creates a matte border with the specified tile icon.
- MatteBorder(Insets borderInsets, Color matteColor):Creates a matte border with the specified insets and color.
- MatteBorder(Insets borderInsets, Icon tileIcon):Creates a matte border with the specified insets and tile icon.
- MatteBorder(int top, int left, int bottom, int right, Color matteColor):Creates a matte border with the specified insets and color.
- MatteBorder(int top, int left, int bottom, int right, Icon tileIcon):Creates a matte border with the specified insets and tile icon.
Commonly used methods are:
| method | explanantion |
|---|---|
| getBorderInsets() | Returns the insets of the border. |
| getBorderInsets(Component c, Insets insets) | Reinitialize the insets parameter with this Border’s current Insets. |
| getMatteColor() |
Returns the color used for tiling the border or null if a tile icon is being used. |
| getTileIcon() | returns the title icon for the border |
| isBorderOpaque() | returns whether border is opaque or not |
Below programs illustrate the MatteBorder class:
- Program to apply matte border using solid colors: We will create a frame f titled ”frame” and will create a panel which will act as a container. We will create two labels l1 and l. We will set the border of both the frame to the matte border using setborder() function one label will have a red border and other will have blue. We will add the label to the panel and panel to the frame. We will set the size of the frame to 400,400 using setSize(400,400) and display the frame using show().
// java Program to apply matte border using solid colorsimportjava.awt.event.*;importjava.awt.*;importjavax.swing.*;importjavax.swing.border.*;classmatte1extendsJFrame {// framestaticJFrame f;// main classpublicstaticvoidmain(String[] args){// create a new framef =newJFrame("frame");// create a objectmatte1 s =newmatte1();// create a panelJPanel p =newJPanel();// create a labelJLabel l =newJLabel(" this is a matte border 2");// create a labelJLabel l1 =newJLabel(" this is a matte border 1");// set boreder for panell.setBorder(newMatteBorder(4,4,4,4, Color.red));// set boreder for labell1.setBorder(newMatteBorder(4,4,4,4, Color.blue));// add button to panelp.add(l1);p.add(l);f.add(p);// set the size of framef.setSize(400,400);f.show();}}chevron_rightfilter_noneOutput:

- Program to apply matte border using icons : We will create a frame f titled ” frame” and will create a panel which will act as a container. We will create two labels l1 and l. We will set the border of both the frame to the matte border using setborder() function one label will have an image icon as border and other will have another image icon. We will import the images using new ImageIcon() function. We will add the label to the panel and panel to the frame. We will set the size of the frame to 400,400 using setSize(400,400) and display the frame using show().
// java Program to apply matte border using iconsimportjava.awt.event.*;importjava.awt.*;importjavax.swing.*;importjavax.swing.border.*;classmatteextendsJFrame {// framestaticJFrame f;// main classpublicstaticvoidmain(String[] args){// create a new framef =newJFrame("frame");// create a objectmatte s =newmatte();// create a panelJPanel p =newJPanel();// create a labelJLabel l =newJLabel(" this is a matte border 2");// create a labelJLabel l1 =newJLabel(" this is a matte border 1");// set boreder for panell.setBorder(newMatteBorder(newImageIcon("f:\\gfg.png")));// set boreder for labell1.setBorder(newMatteBorder(newImageIcon("f:\\gfg.jpg")));// add button to panelp.add(l1);p.add(l);f.add(p);// set the size of framef.setSize(400,400);f.show();}}chevron_rightfilter_noneOutput:

- Program to apply matte border using solid color and image by specifying insets: We will create a frame f titled ” frame” and will create a panel which will act as a container. We will create two labels l1 and l. We will set the border of both the frame to the matte border using setborder() function one label will have an image icon as border and other will have another image icon. We will import the images using new ImageIcon() function. We will specify the insets or width of the border using new Insets() function. We will add the label to the panel and panel to the frame. We will set the size of the frame to 400,400 using setSize(400,400) and display the frame using show().
// java Program to apply matte border using// solid color and image by specifying insetsimportjava.awt.event.*;importjava.awt.*;importjavax.swing.*;importjavax.swing.border.*;classmatte3extendsJFrame {// framestaticJFrame f;// main classpublicstaticvoidmain(String[] args){// create a new framef =newJFrame("frame");// create a objectmatte3 s =newmatte3();// create a panelJPanel p =newJPanel();// create a labelJLabel l =newJLabel(" this is a matte border 2");// create a labelJLabel l1 =newJLabel(" this is a matte border 1");// set boreder for panell.setBorder(newMatteBorder(newInsets(4,7,4,10), Color.red));// set boreder for labell1.setBorder(newMatteBorder(newInsets(10,4,10,4),newImageIcon("f:\\gfg.png")));// add button to panelp.add(l1);p.add(l);f.add(p);// set the size of framef.setSize(400,400);f.show();}}chevron_rightfilter_noneOutput:

- Java Swing | Translucent and shaped Window in Java
- Java Swing | JPopupMenu
- Java Swing | JTextArea
- Java Swing | JTable
- JLabel | Java Swing
- Java Swing | JSlider
- Java Swing | ToolTip
- JRadioButton | Java Swing
- Java Swing | JToolBar
- Java Swing | JFileChooser
- Java Swing | JTextField
- Java Swing | Look and Feel
- Java Swing | JProgressBar
- Java Swing | JSpinner
- Java Swing | JPasswordField
Note: The above programs might not run in an online compiler please use an offline IDE.
Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/border/MatteBorder.html
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



