Friday, November 1, 2024
Google search engine
HomeLanguagesJavaJava Swing | MatteBorder

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 explanation
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: 
 

  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




// 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 border for panel
        l.setBorder(new MatteBorder(4, 4, 4, 4, Color.red));
 
        // set border 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();
    }
}


  1. Output
     

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




// 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 border for panel
        l.setBorder(new MatteBorder(new ImageIcon("f:\\gfg.png")));
 
        // set border 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();
    }
}


  1. Output
     

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




// 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 border for panel
        l.setBorder(new MatteBorder(new Insets(4, 7, 4, 10), Color.red));
 
        // set border 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();
    }
}


  1. Output
     

RELATED ARTICLES

Most Popular

Recent Comments