DEPARTMENT OF COMPUTER
ENGINEERING
Subject: Advanced Java Programming Subject Code: 22517
Semester: 05 Course: CO5I-B
Laboratory No: L002 Name of Subject Teacher: Prof. Pradeep Shirke
Name of Student: Ashwath Bhekare Roll ID: 22203B0015
Experiment No: 5
Title of Experiment Write a program using AWT to create a menu bar where menu
bar contains menu items such as File, Edit, View and create a
submenu under the File menu: New and Open.
Program Code
1. Write a program which creates Menu of different colors and
disable menu item for Black color.
Ans:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ColorMenuExample {
public static void main(String[] args) {
// Create the frame
JFrame frame = new JFrame("Color Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
JMenu colorMenu = new JMenu("Colors");
JMenuItem redItem = new JMenuItem("Red");
JMenuItem greenItem = new JMenuItem("Green");
JMenuItem blueItem = new JMenuItem("Blue");
JMenuItem blackItem = new JMenuItem("Black");
redItem.addActionListener(e ->
frame.getContentPane().setBackground(Color.RED));
greenItem.addActionListener(e ->
frame.getContentPane().setBackground(Color.GREEN));
blueItem.addActionListener(e ->
frame.getContentPane().setBackground(Color.BLUE));
blackItem.setEnabled(false);
// Add menu items to the menu
colorMenu.add(redItem);
colorMenu.add(greenItem);
colorMenu.add(blueItem);
colorMenu.add(blackItem); // Black is disabled
// Add the menu to the menu bar
menuBar.add(colorMenu);
// Set the menu bar for the frame
frame.setJMenuBar(menuBar);
// Set the initial background color
frame.getContentPane().setBackground(Color.WHITE);
// Make the frame visible
frame.setVisible(true);
}
}
Output:
Exercise
1. Find errors in following program and display output as shown
below.
import java.awt.*;
import java.awt.event.*;
public class MenuDemo1 extends Frame
{
MenuBar mb;
MenuItem m1,m2,m3,m4;
Menu mn;
MenuShortcut ms;
MenuDemo1()
{
setTitle("MenuBar Demo");
setSize(500,500);
setLayout(null);
ms=new MenuShortcut(KeyEvent.VK_X);
mn=new Menu("File");
mb=new MenuBar();
m1=new MenuItem("New...");
m2=new MenuItem("Open...");
m3=new MenuItem("Save As...");
m4=new MenuItem("Exit",ms);
mn.add(m1);
mn.add(m2);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
mb.add(mn);
}
public static void main(String[] args)
{
MenuDemo1 md=new MenuDemo1();
md.setVisible(true);
}
}
Ans:
import java.awt.*;
import java.awt.event.*;
public class MenuDemo1 extends Frame {
MenuBar mb;
MenuItem m1, m2, m3, m4;
Menu mn;
MenuShortcut ms;
MenuDemo1() {
setTitle("MenuBar Demo");
setSize(500, 500);
setLayout(null);
// Create the MenuShortcut
ms = new MenuShortcut(KeyEvent.VK_X);
// Create the Menu
mn = new Menu("File");
// Create the MenuBar
mb = new MenuBar();
// Create MenuItems
m1 = new MenuItem("New...");
m2 = new MenuItem("Open...");
m3 = new MenuItem("Save As...");
m4 = new MenuItem("Exit", ms);
// Add MenuItems to Menu
mn.add(m1);
mn.add(m2);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
// Add Menu to MenuBar
mb.add(mn);
// Set the MenuBar to the Frame
setMenuBar(mb);
// Set the Frame to be visible
setVisible(true);
// Add WindowListener to handle closing the Frame
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
});
public static void main(String[] args) {
new MenuDemo1();
}
Output:
Practical Related Questions
1. Write the use of setEnabled() method
Ans:
The setEnabled() method is commonly used in Java GUI programming (especially
in Swing) to enable or disable a component such as buttons, menu items, or other
interactive elements. When a component is disabled, it cannot be interacted with
by the user.
Syntax - component.setEnabled(boolean enabled);
2. Write the procedure to assign shortcut key to the Menu Item.
Ans:
Step-by-Step Procedure
1. Open the Application
Launch the application where you want to assign a shortcut key.
2. Access Preferences or Settings
o Navigate to the Preferences, Settings, or Options menu. This is typically
found under the Edit, Tools, or File menu.
3. Locate Keyboard Shortcuts
o Look for a section labeled Keyboard Shortcuts, Hotkeys, or Key
Bindings. This section allows you to manage existing shortcuts and create
new ones.
4. Find the Desired Menu Item
o Browse the list of commands to locate the specific menu item you wish to
assign a shortcut to. This may be categorized or sorted alphabetically.
5. Assign the Shortcut Key
o Select the menu item and look for an option to Edit, Add, or Change the
shortcut.
o Press the key combination you want to assign (e.g., Ctrl + N for a new
document). Ensure this combination is not already in use by another
command.
6. Save Your Changes
o After assigning the shortcut, make sure to click Save, Apply, or OK to
confirm your changes.
7. Test the New Shortcut
o Use the newly assigned shortcut to test its functionality and ensure it
works as intended.
3. Write a syntax and use of addSeparator() method.
Ans:
The addSeparator() method is commonly used in Java's Swing library to create a visual
separation between menu items in a JMenu or a JToolBar. This helps organize menu
items and improve user interface clarity.
Syntax: void addSeparator()
Use of addSeparator():
The addSeparator() method is used to insert a horizontal separator in a menu or toolbar,
visually distinguishing groups of related actions. This can enhance the user experience
by making it easier for users to navigate the menu.
Marks Obtained Dated signature of Teacher
Process Product Total (50)
Related (35) Related
(15)