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:

  1. MatteBorder(Icon tileIcon):Creates a matte border with the specified tile icon.
  2. MatteBorder(Insets borderInsets, Color matteColor):Creates a matte border with the specified insets and color.
  3. MatteBorder(Insets borderInsets, Icon tileIcon):Creates a matte border with the specified insets and tile icon.
  4. MatteBorder(int top, int left, int bottom, int right, Color matteColor):Creates a matte border with the specified insets and color.
  5. 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() retruns the title icon for the border
isBorderOpaque() returns whether border is opaque or not

Below programs illustrate the MatteBorder class:

  1. 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 colors
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    class matte1 extends JFrame {
    
        // frame
        static JFrame f;
    
        // main class
        public static void main(String[] args)
        {
            // create a new frame
            f = new JFrame("frame");
    
            // create a object
            matte1 s = new matte1();
    
            // create a panel
            JPanel p = new JPanel();
    
            // create a label
            JLabel l = new JLabel("  this is a matte border 2");
    
            // create a label
            JLabel l1 = new JLabel(" this is a matte border 1");
    
            // set boreder for panel
            l.setBorder(new MatteBorder(4, 4, 4, 4, Color.red));
    
            // set boreder for label
            l1.setBorder(new MatteBorder(4, 4, 4, 4, Color.blue));
    
            // add button to panel
            p.add(l1);
            p.add(l);
    
            f.add(p);
    
            // set the size of frame
            f.setSize(400, 400);
    
            f.show();
        }
    }
    

    Output:

  2. 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  icons
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    class matte extends JFrame {
    
        // frame
        static JFrame f;
    
        // main class
        public static void main(String[] args)
        {
            // create a new frame
            f = new JFrame("frame");
    
            // create a object
            matte s = new matte();
    
            // create a panel
            JPanel p = new JPanel();
    
            // create a label
            JLabel l = new JLabel("  this is a matte border 2");
    
            // create a label
            JLabel l1 = new JLabel(" this is a matte border 1");
    
            // set boreder for panel
            l.setBorder(new MatteBorder(new ImageIcon("f:\\gfg.png")));
    
            // set boreder for label
            l1.setBorder(new MatteBorder(new ImageIcon("f:\\gfg.jpg")));
    
            // add button to panel
            p.add(l1);
            p.add(l);
    
            f.add(p);
    
            // set the size of frame
            f.setSize(400, 400);
    
            f.show();
        }
    }
    

    Output:

  3. 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 insets
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    class matte3 extends JFrame {
    
        // frame
        static JFrame f;
    
        // main class
        public static void main(String[] args)
        {
            // create a new frame
            f = new JFrame("frame");
    
            // create a object
            matte3 s = new matte3();
    
            // create a panel
            JPanel p = new JPanel();
    
            // create a label
            JLabel l = new JLabel("  this is a matte border 2");
    
            // create a label
            JLabel l1 = new JLabel(" this is a matte border 1");
    
            // set boreder for panel
            l.setBorder(new MatteBorder(new Insets(4, 7, 4, 10), Color.red));
    
            // set boreder for label
            l1.setBorder(new MatteBorder(new Insets(10, 4, 10, 4), new ImageIcon("f:\\gfg.png")));
    
            // add button to panel
            p.add(l1);
            p.add(l);
    
            f.add(p);
    
            // set the size of frame
            f.setSize(400, 400);
    
            f.show();
        }
    }
    

    Output:

  4. 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



    My Personal Notes arrow_drop_up

    First year Department of Information Technology Jadavpur University

    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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




    Practice Tags :
    Article Tags :
    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions