Java Swing | JSeparator with examples

JSeparator is a part of Java Swing framework. It is used to create a dividing line between two components. More specifically, it is mainly used to create dividing lines between menu items in a JMenu. In JMenu or JPopupMenu addSeparartor function can also be used to create a separator.

Constructor of the class are:

  1. separator(): Creates a new horizontal separator.
  2. JSeparator(int o): Creates a new separator with the specified horizontal or vertical orientation.

Commonly used methods:



method explanantion
setOrientation(int o) Sets the orientation of the separator.
getOrientation() returns the orientation of the separator.
addSeparator() adds a separator in JMenu or JPopupMenu.

Below programs will illustrate the use of JSeparartor

  1. Program to create a vertical separator: In this program we create a frame which is named f with a title “separator” (frame is the container for other components). We create a panel to hold the labels and the separator. We set the orientation of the separator to vertical (using setOrientation(SwingConstants.VERTICAL)) and add the separator and the labels to the panel (using add() function)and add the panel to the frame. We set grid layout (using new GridLayout(400,400))for the panel with 1 row and 0 columns. We set the size of the frame using setSize(400,400) to 400,400. We use the show() function to display the frame.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // java Program to create a vertical separator
    import java.awt.*;
    import javax.swing.*;
    class seperator extends JFrame 
    {
        // constructor for the class
        seperator()
        {
        }
          
        // main class
        public static void main(String args[])
        {
            // create a frame
            JFrame  f = new JFrame("seperator");
              
            // create a panel
            JPanel p =new JPanel();
              
            // create a label
            JLabel l = new JLabel("this is label 1");
            JLabel l1 = new JLabel("this is label 2");
              
            // create a seperator
            JSeparator s = new JSeparator();
              
            // set layout as vertical
            s.setOrientation(SwingConstants.VERTICAL);
              
            p.add(l);
            p.add(s);
            p.add(l1);
              
            // set layout
            p.setLayout(new GridLayout(1,0));
              
            f.add(p);
              
            // show the frame
            f.setSize(400,400);
            f.show();
        }
    }

    chevron_right

    
    

    Output:

  2. Program to create a horizontal separator: In this program, we create a frame which is named f with a title “separator” (frame is the container for other components). We create a panel to hold the labels and the separator. We set the orientation of the separator to horizontal(using setOrientation(SwingConstants.HORIZONTAL)) and add the separator and the labels to the panel (using add() function)and add the panel to the frame. We set grid layout (using new GridLayout(400,400)) for the panel with 0 row and 1 columns. We set the size of the frame using setSize(400,400) to 400,400 . We use the show() function to display the frame.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // java Program to create a HORIZONTAL separator
    import java.awt.*;
    import javax.swing.*;
    class seperator_1 extends JFrame 
    {
        // constructor for the class
        seperator_1()
        {
        }
          
        // main class
        public static void main(String args[])
        {
            // create a frame
            JFrame  f = new JFrame("seperator");
              
            // create a panel
            JPanel p =new JPanel();
              
            // create a label
            JLabel l = new JLabel("this is label 1");
            JLabel l1 = new JLabel("this is label 2");
              
            // create a seperator
            JSeparator s = new JSeparator();
              
            // set layout as vertical
            s.setOrientation(SwingConstants.HORIZONTAL);
              
            p.add(l);
            p.add(s);
            p.add(l1);
              
            // set layout
            p.setLayout(new GridLayout(0,1));
              
            f.add(p);
              
            // show the frame
            f.setSize(400,400);
            f.show();
        }
    }

    chevron_right

    
    

    Output:

  3. Program to create a separator using addSeparator function: In this program we create a frame which is named f with a title “separator” (frame is the container for other components). To illustrate the use of the add separator function we will create a JMenuBar mb. Then create a JMenu to hold the menuitems. We will create two JMenuItems and add a separator in between them by using addSeparator() function. We will add the menu to menubar and menubar to the frame using add() and addMenuBar() functions respectively. We set the size of the frame using setSize(400,400) to 400,400. We use the show() function to display the frame.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // java Program to create a separator
    // using addSeparator function
    import java.awt.*;
    import javax.swing.*;
    class seperator extends JFrame 
    {
        // constructor for the class
        seperator()
        {
        }
          
        // main class
        public static void main(String args[])
        {
            // create a frame
            JFrame  f = new JFrame("seperator");
              
            // create a menubar
            JMenuBar mb =new JMenuBar();
              
            // create a menu 
            JMenu m = new JMenu("menu");
              
            // create menuitems
            JMenuItem m1= new JMenuItem("item 1");
            JMenuItem m2= new JMenuItem("item 2");
              
            // add menuitems
            m.add(m1);
            m.addSeparator();
            m.add(m2);
              
            // add menu
            mb.add(m);
              
            f.setJMenuBar(mb);
              
            // show the frame
            f.setSize(400,400);
            f.show();
        }
    }

    chevron_right

    
    

    Output:

  4. Note: The following Programs might not run in an online IDE please use an offline Compiler.
    Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/JSeparator.html



    My Personal Notes arrow_drop_up

    Second year Department of Information Technology Jadavpur University

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.