Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJava Swing | JSeparator with examples

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 explanation
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.

Java




// java Program to create a vertical separator
import java.awt.*;
import javax.swing.*;
class separator extends JFrame
{
    // constructor for the class
    separator()
    {
    }
     
    // main class
    public static void main(String args[])
    {
        // create a frame
        JFrame f = new JFrame("separator");
         
        // 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 separator
        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();
    }
}


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.

Java




// java Program to create a HORIZONTAL separator
import java.awt.*;
import javax.swing.*;
class separator_1 extends JFrame
{
    // constructor for the class
    separator_1()
    {
    }
     
    // main class
    public static void main(String args[])
    {
        // create a frame
        JFrame f = new JFrame("separator");
         
        // 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 separator
        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();
    }
}


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.

Java




// java Program to create a separator
// using addSeparator function
import java.awt.*;
import javax.swing.*;
class separator extends JFrame
{
    // constructor for the class
    separator()
    {
    }
     
    // main class
    public static void main(String args[])
    {
        // create a frame
        JFrame f = new JFrame("separator");
         
        // 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();
    }
}


Output
 

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments