Java Swing How to - Add Empty border to JPanel








Question

We would like to know how to add Empty border to JPanel.

Answer

/*ww w  .  j  av  a  2s  .co m*/
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
  public static void main(String[] args) throws Exception {
    JLabel label = new JLabel("java2s.com");
    
    JPanel panel  = new JPanel();
    panel.add(label);    
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    
    JOptionPane.showMessageDialog(null, panel);

  }
}