How to leave space with EmptyBorder in Java Swing

Java 8Object Oriented ProgrammingProgramming

Llet us first create a JPanel and set titled border:

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));

Now to create Empty Border:

JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(panel, BorderLayout.CENTER);
panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));

The following is an example to leave space with EmptyBorder in Java Swing:

Example

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingDemo {
   public static void main(String[] args) {
      JPanel panel = new JPanel();
      panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));
      JPanel panel2 = new JPanel(new BorderLayout());
      panel2.add(panel, BorderLayout.CENTER);
      panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
      panel2.setPreferredSize(new Dimension(700, 350));
      JFrame frame = new JFrame("Set Space");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setContentPane(panel2);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }
}

Output

raja
Published on 03-May-2019 11:17:52
Advertisements