Java Swing | JSeparator with examples
Last Updated :
22 Jul, 2021
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:
- separator(): Creates a new horizontal separator.
- 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
import java.awt.*;
import javax.swing.*;
class separator extends JFrame
{
separator()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "separator" );
JPanel p = new JPanel();
JLabel l = new JLabel( "this is label 1" );
JLabel l1 = new JLabel( "this is label 2" );
JSeparator s = new JSeparator();
s.setOrientation(SwingConstants.VERTICAL);
p.add(l);
p.add(s);
p.add(l1);
p.setLayout( new GridLayout( 1 , 0 ));
f.add(p);
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
import java.awt.*;
import javax.swing.*;
class separator_1 extends JFrame
{
separator_1()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "separator" );
JPanel p = new JPanel();
JLabel l = new JLabel( "this is label 1" );
JLabel l1 = new JLabel( "this is label 2" );
JSeparator s = new JSeparator();
s.setOrientation(SwingConstants.HORIZONTAL);
p.add(l);
p.add(s);
p.add(l1);
p.setLayout( new GridLayout( 0 , 1 ));
f.add(p);
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
import java.awt.*;
import javax.swing.*;
class separator extends JFrame
{
separator()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "separator" );
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu( "menu" );
JMenuItem m1= new JMenuItem( "item 1" );
JMenuItem m2= new JMenuItem( "item 2" );
m.add(m1);
m.addSeparator();
m.add(m2);
mb.add(m);
f.setJMenuBar(mb);
f.setSize( 400 , 400 );
f.show();
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...