Java Swing Tutorial - Java BorderFactory .createEmptyBorder (int top, int left, int bottom, int right)








Syntax

BorderFactory.createEmptyBorder(int top, int left, int bottom, int right) has the following syntax.

public static Border createEmptyBorder(int top,   int left,   int bottom,   int right)

Example

In the following code shows how to use BorderFactory.createEmptyBorder(int top, int left, int bottom, int right) method.

/*w  w w.jav  a 2 s .co m*/
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;

public class Main {

  public static void main(String[] a) {
    JFrame frame = new JFrame("Empty Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border emptyBorder = BorderFactory.createEmptyBorder(20, 20, 0, 0);
    JButton emptyButton = new JButton("With Empty");
    emptyButton.setBorder(emptyBorder);
    JButton nonemptyButton = new JButton("Without Empty");
    Container contentPane = frame.getContentPane();
    contentPane.add(emptyButton, BorderLayout.NORTH);
    contentPane.add(nonemptyButton, BorderLayout.SOUTH);
    frame.pack();
    frame.setSize(300, frame.getHeight());
    frame.setVisible(true);
  }

}