Java Swing GUI

JSeparator – Java Swing – Example

In this tutorial, we are going to see an example of JSeparator in Java Swing. JSeparator class is used to provide a general-purpose component to implement separator lines. It is used to draw a line to separate widgets in a layout. It inherits from the JComponent class.


 

JSeparator constructors class:
JSeparator constructors
Description
JSeparator() Create a new horizontal separator.
JSeparator(int orientation) Creates a new separator with a specified horizontal or vertical orientation.

 

Commonly used methods:
  • setOrientation(int orientation) : It is used to define the orientation of the separator.
  • getOrientation() : It is used to return the orientation of the separator.
 

Example of JSeparator in Java Swing:
import javax.swing.*;  

class MySeparator  
{  
  MySeparator()  
  {
    JFrame f = new JFrame("JSeparator Example"); 
    
    //create the menu bar
    JMenuBar bar = new JMenuBar(); 
    
    //create the menu
    JMenu menuFile = new JMenu("File");  
    //create menu items
    JMenuItem newF = new JMenuItem("New");  
    JMenuItem openF = new JMenuItem("Open");
    
    //add menu items to the main menu
    menuFile.add(newF);
    //add the separator
    menuFile.addSeparator();
    menuFile.add(openF);
    
    //add the menu to the menu bar
    bar.add(menuFile);
    
    //add the menu bar to the frame
    f.setJMenuBar(bar);  
    f.setSize(300,300);  
    f.setLayout(null);  
    f.setVisible(true);  
  } 
  public static void main(String args[])  
  {  
    new MySeparator();  
  }
}

Output:


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *