Menu Bar in Java

Ads
 
Tutorials   
Java/JDK 15 upcoming new features JDK/Java 15 Tutorials How to install OpenJDK 8 in Ubuntu 18.04? Java Interpreter is used for which purpose? Java program to read a text file and write to another file How to Serialize and Deserialize Array List in Java? What are the prerequisites for learning Java? How to replace all occurrences of a string in JavaScript? How to convert a stack trace to a string? Example code of reading file line by line in Java with Apache Commons IO library How to store unique values in Java using Set? java.nio ShortBuffer Example How to use Java Path class? Java NIO Package Tutorial How to limit object creation in Java? Java 11 String lines function example to split it in lines Top upcoming features of JDK 11 Java/JDK 11 Tutorial How to install JDK 10 in Ubuntu 18.04? Install JDK 10 on Windows Java 10 features Processing large file in Java Learning Java for non programmers Why You Should Learn Java in 2018? Installing JDK 9 on Windows 10 How to convert binary to decimal in Java? How to add two numbers in Java? What is Applet in Java with Example? What is nested class in Java with Example? Exception handling in Java Programming Examples What is Constructor Overloading in Java? How to display image in Swing? What is Java programming used for? How to create custom exception in Java? How to set CLASSPATH in Java? What is Multithreading in Java? IO Streams in Java with Examples How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner

Menu Bar in Java

A Menu Bar is a set of option that allow the user to choose from any one of the saving option. A list of other option component a user can choose in a menu bar. In this Tutorial we want to describe you how to create a Menu Bar in your application

A Menu Bar is a set of option that allow the user to choose from any one of the saving option. A list of other option component a user can choose in a menu bar. In this Tutorial we want to describe you how to create a Menu Bar in your application

Menu Bar in Java

     

A Menu Bar is a set of option that allow the user to choose from any one of the  saving option. A list of other option component  a user can choose in a menu bar. In this Tutorial we want to describe you how to create a Menu Bar in your application, First we need to import list of packages that required for creating a menu bar in an application. We defined a class name 'MenuBarExample' extends JFrame.A JFrame is a window working in a swing environment. We define a constructor that is used to create  a JFrame with a specified size of 200*350.The java  code also describe you to exit when the JFrame when it is closed and show us the window. The JMenuBar is used to control the list of bar at the top of window, this includes File,Edit,View,etc.JMenuBar is positioned at the menubar in the JFrame.The MenuBar is set at the JMenuBar.We define and add three dropdown menu in the menubar i.e

  1. File
  2. Edit
  3. View

After we have done with the list of option in menuBar,then we go for the sub menuBar options i.e. we create and add simple menu item to the list of dropdown menu present in menuBar.The various submenu that we have used in this menu Bar are-

File Edit View
New Copy  Toolbars
Open Paste -
Exit Cut -

The class implement Action Listener, that is used to add  action listener to submenu item. Action listener is a set of event  generated when a user perform certain action on a component.Example,when a user click or perform a certain operation on button and textfield,as a result a certain action event is generated on the clicking of a button or enter in a text field. The result come out is action performed that is given to all action listener registered with respective component. 

Let us go through list of method  to understand  example program code

1.newAction.addActionListener(new Action Listener())-This is used to register an instance of the event handler class as a listener on  components

2.public void actionPerformed(ActionEvent arg0)  - The program must register this object as an action listener the component source, using the addActionListener method. When the user clicks the onscreen component, the component fires an action event. This results in the invocation of the action listener's action Performed method. The single argument to the method is an Action Event object that gives information about the event and its source. The System.out.println is used to display the "you have clicked on the open action" as long as you clicked on open submenu.

Code for Menu Bar

  

   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import javax.swing.JFrame;
   import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  public class MenuBarExample extends JFrame
{
   public MenuBarExample()
{

   setTitle("MenuBarExample");
   setSize(200, 350);


   JMenuBar menuBar = new JMenuBar();

  setJMenuBar(menuBar);


  JMenu fileMenu = new JMenu("File");
  JMenu editMenu = new JMenu("Edit");
  JMenu viewMenu = new JMenu("View");
  menuBar.add(fileMenu);
  menuBar.add(editMenu);
  menuBar.add(viewMenu);


  JMenuItem newAction =   new JMenuItem("New");
  JMenuItem openAction =  new JMenuItem("Open");
  JMenuItem exitAction =  new JMenuItem("Exit");
  JMenuItem cutAction =   new JMenuItem("Cut");
  JMenuItem copyAction =  new  JMenuItem("Copy");
  JMenuItem pasteAction =  new JMenuItem("Paste");
  JMenuItem toolbarsAction= new JMenuItem("toolbarsAction");
  fileMenu.add(newAction);
  fileMenu.add(openAction);
  fileMenu.addSeparator();
  fileMenu.add(exitAction);
  editMenu.add(cutAction);
  editMenu.add(copyAction);
  editMenu.add(pasteAction);
  editMenu.addSeparator();
  viewMenu.add(toolbarsAction);

   newAction.addActionListener(new ActionListener();

 {
  public void actionPerformed(ActionEvent arg0)

 {
  System.out.println("You clicked on the new action");
  } 

  }

  };

  }

   public static void main(String[] args) 

 {
   MenuBarExample mba = new MenuBarExample();
   mba.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   mba.setVisible(true);
 }
 }

Output on Command Prompt


C:\saurabh>javac MenuBarExample.java

C:\saurabh>java MenuBarExample
You  clicked on the new action
You  clicked on the new action

Output Displayed as 

 


Ads

Ads