Introduction
This article describes how to create an OpenMenu bar in Java. The Netbeans IDE is used for the development of the example.
Packages Imported
java.io.*;
This package is used for system input and output through serialization, data streams and the file system.
java.awt.*;
AWT stands for Abstract Window ToolKit. The AWT package is imported to use the AWT components like label, checkbox and so on.
javax.swing.*;
Swing is an extended version of AWT. It supports some new components, like JComboBox, JScroll Pane and so on. Swing provides a Graphical User Interface to programs.
Java.awt.event.*;
This package is for dealing with various types of events fired by AWT components. This package provides classes and interfaces for dealing with these events.
What is Menu
A Menu is basically to be displayed to the user to help the user find information or execute a program function.
Example
In this example; we define how to create an OpenMenu in Java using the Netbeans IDE. There are certain steps in the Netbeans IDE that we need to follow as explained below.
Step 1
Open the Netbeans IDE then click on "File" -> "New project" then select "Java project" then provide a name (for example Menu) then click on "Ok" then right-click on our project and choose "New" -> Java class and then provide your class name (as Menu.java) and click "Ok" and use the following code for the class.
Step 2
In this class write the following code (the class name is "Menu"):
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Menu extends JFrame implements ActionListener{
JMenuBar db;
JMenu newfile;
JMenuItem demoopen;
JTextArea jt;
Menu()
{
demoopen=new JMenuItem("Open File");
demoopen.addActionListener(this);
newfile=new JMenu("File");
newfile.add(demoopen);
db=new JMenuBar();
db.setBounds(0,0,800,20);
db.add(newfile);
jt=new JTextArea(800,800);
jt.setBounds(0,20,800,800);
add(db);
add(jt);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==demoopen)
{
openFile();
}
}
void openFile()
{
JFileChooser cf=new JFileChooser();
int i=cf.showOpenDialog(this);
if(i==JFileChooser.APPROVE_OPTION)
{
File f=cf.getSelectedFile();
String filepath=f.getPath();
displayContent(filepath);
}
}
void displayContent(String fpath)
{
try
{
BufferedReader br=new BufferedReader(new FileReader(fpath));
String s1="",s2="";
while((s1=br.readLine())!=null)
{
s2+=s1+"\n";
}
jt.setText(s2);
br.close();
}catch (Exception e) {e.printStackTrace(); }
}
public static void main(String[] args)
{
Menu mu=new Menu();
mu.setSize(300,300);
mu.setLayout(null);
mu.setVisible(true);
mu.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
![d1.jpg]()
![d2.jpg]()
![d3.jpg]()
Step 3
Now go to Menu and right-click on that, click on "Run" from the menu bar as in the following:
![d4.jpg]()
Output
A window will appear and in that the file menu is displayed in the menu bar.
![d5.jpg]()
Now click on "File" and then click on "Open File".
![d6.jpg]()
From the window that is shown you can select any file from your system and you can open it.