JSeparator in Java Swing

A JSeparator in Java Swing is a graphical component used to visually separate different sections of a user interface. It’s often employed to enhance the organization and readability of GUIs.

Following is a basic example demonstrating the usage of JSeparator:

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class JSeparatorDemo extends JFrame {
	public JSeparatorDemo() {
		setTitle("JSeparator Demo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout());

		JLabel label1 = new JLabel("Section 1");
		add(label1);

		JSeparator separator1 = new JSeparator();
		separator1.setPreferredSize(new Dimension(200, 10));
		add(separator1);

		JLabel label2 = new JLabel("Section 2");
		add(label2);

		JSeparator separator2 = new JSeparator(SwingConstants.VERTICAL);
		separator2.setPreferredSize(new Dimension(10, 100));
		add(separator2);

		JLabel label3 = new JLabel("Section 3");
		add(label3);

		pack();
		setLocationRelativeTo(null); // Center the window
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			new JSeparatorDemo().setVisible(true);
		});
	}

}

Output:

JSeparator Demo in java swing