Java JSeparator

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JSeparator

The object of JSeparator class is used to provide a general purpose component for implementing divider lines. It is used to draw a line to separate widgets in a Layout. It inherits JComponent class.

JSeparator class declaration

  1. public class JSeparator extends JComponent implements SwingConstants, Accessible  

Commonly used Constructors of JSeparator

Constructor

Description

JSeparator()

Creates a new horizontal separator.

JSeparator(int orientation)

Creates a new separator with the specified horizontal or vertical orientation.

Commonly used Methods of JSeparator

Method

Description

void setOrientation(int orientation)

It is used to set the orientation of the separator.

int getOrientation()

It is used to return the orientation of the separator.

Java JSeparator Example 1

  1. import javax.swing.*;    
  2. class SeparatorExample    
  3. {    
  4.           JMenu menu, submenu;    
  5.           JMenuItem i1, i2, i3, i4, i5;    
  6.           SeparatorExample()  {    
  7.           JFrame f= new JFrame("Separator Example");    
  8.           JMenuBar mb=new JMenuBar();    
  9.           menu=new JMenu("Menu");    
  10.           i1=new JMenuItem("Item 1");    
  11.           i2=new JMenuItem("Item 2");       
  12.           menu.add(i1);  
  13.           menu.addSeparator();  
  14.           menu.add(i2);  
  15.           mb.add(menu);    
  16.           f.setJMenuBar(mb);    
  17.           f.setSize(400,400);    
  18.           f.setLayout(null);    
  19.           f.setVisible(true);    
  20. }    
  21. public static void main(String args[])    
  22. {    
  23. new SeparatorExample();    
  24. }}    

Output:

Java Jseparator 1

Java JSeparator Example 2

  1. import javax.swing.*;    
  2. import java.awt.*;  
  3. public class SeparatorExample    
  4. {    
  5.     public static void main(String args[]) {  
  6.         JFrame f = new JFrame("Separator Example");      
  7.         f.setLayout(new GridLayout(01));  
  8.         JLabel l1 = new JLabel("Above Separator");  
  9.         f.add(l1);  
  10.         JSeparator sep = new JSeparator();  
  11.         f.add(sep);  
  12.         JLabel l2 = new JLabel("Below Separator");  
  13.         f.add(l2);  
  14.         f.setSize(400100);  
  15.         f.setVisible(true);  
  16.       }  
  17.     }    

Output:

Java Jseparator 2