What is the importance of JSeparator class in Java?


JSeparator

  • A JSeparator is a horizontal or vertical line or an empty space that separates the components.
  • A JSeparator class is used to draw a line to separate the components in a Layout.
  • The easiest way to add a separator to a menu or toolbar is to call the addSeparator() method provided by the classes JMenu, JPopupMenu and JToolBar.
  • The important methods of JSeparator class are setOrientation() and getOrientation().

Example

import java.awt.*;
import javax.swing.*;
public class JSeparatorTest extends JFrame {
   private JLabel label1, label2;
   public JSeparatorTest() {
      setTitle("JSeparator Test");
      setLayout(new GridLayout(0, 1));
      label1 = new JLabel("Above Separator");
      add(label1);
      JSeparator sep = new JSeparator();
      add(sep); // add a separator between two labels.
      label2 = new JLabel("Below Separator");
      add(label2);
      setSize(375, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) {
      new JSeparatorTest();
   }
}

Output

Updated on: 10-Feb-2020

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements