0% found this document useful (0 votes)
123 views5 pages

Java Menu and Event Handling Guide

This lab manual provides practical exercises for Java programming, including creating a menu bar and implementing GUI components like radio buttons and checkboxes. It includes example code for building a simple menu-driven interface and assigning shortcut keys to menu items. The manual emphasizes the use of Java Development Kit and Integrated Development Environment for coding and debugging.

Uploaded by

Huzeefa Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views5 pages

Java Menu and Event Handling Guide

This lab manual provides practical exercises for Java programming, including creating a menu bar and implementing GUI components like radio buttons and checkboxes. It includes example code for building a simple menu-driven interface and assigning shortcut keys to menu items. The manual emphasizes the use of Java Development Kit and Integrated Development Environment for coding and debugging.

Uploaded by

Huzeefa Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming Practical:15 Lab Manual (Solved)

Resource required (additional)


Name of Resource Broad Specification Remark if any
Java Development Kit
java 17.0.4 For Java compilation
(JDK)
To simplify Java coding with syntax
Integrated Development highlighting, debugging, and
VS code
Environment (IDE) suggestions.

Conclusion
In this practical, we wrote a program to create a menu bar with various menu items and sub-
menu items. We learned how to use Menu and MenuItem classes to build a simple menu-
driven interface in Java.

Practical related Questions


1. Design an applet/application to demonstrate the use of Radio Button and Checkbox.
import [Link].*;
import [Link].*;

class SimpleForm extends JFrame implements ActionListener {


JRadioButton batch1, batch2;
JCheckBox manualChecked, microProject;
JButton submit;
JLabel result;

SimpleForm() {
setLayout(null);
setSize(250, 200);

batch1 = new JRadioButton("SY-Batch1");


batch2 = new JRadioButton("SY-Batch2");
manualChecked = new JCheckBox("Manual Checked");
microProject = new JCheckBox("Micro Project");

ButtonGroup group = new ButtonGroup();


Jamia Polytechnic Akkalkuwa Page No:1 Prepared by: Sayyed Waliullah
Java Programming Practical:15 Lab Manual (Solved)

[Link](batch1);
[Link](batch2);

[Link](50, 30, 100, 25);


[Link](50, 60, 100, 25);
[Link](50, 90, 150, 25);
[Link](50, 120, 150, 25);

submit = new JButton("Submit");


[Link](50, 150, 100, 30);
[Link](this);

result = new JLabel();


[Link](50, 180, 200, 25);

add(batch1); add(batch2); add(manualChecked); add(microProject);


add(submit); add(result);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


String selectedBatch = [Link]() ? "SY-Batch1" : "SY-Batch2";
String status = "Selected Batch: " + selectedBatch +
([Link]() ? ", Manual Checked" : "") +
([Link]() ? ", Micro Project" : "");
[Link](status);
}

public static void main(String[] args) {


new SimpleForm();
}
}
2. Write a program which creates Menu of different colors and disable menu item for Black
color.
import [Link].*;
import [Link].*;
Jamia Polytechnic Akkalkuwa Page No:2 Prepared by: Sayyed Waliullah
Java Programming Practical:15 Lab Manual (Solved)

import [Link].*;

class ColorMenuApp extends JFrame {


JMenuBar menuBar;
JMenu colorMenu;
JMenuItem redItem, greenItem, blueItem, blackItem;

ColorMenuApp() {
setTitle("Color Menu Example");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create the menu bar


menuBar = new JMenuBar();

// Create a menu
colorMenu = new JMenu("Colors");

// Create menu items


redItem = new JMenuItem("Red");
greenItem = new JMenuItem("Green");
blueItem = new JMenuItem("Blue");
blackItem = new JMenuItem("Black");

// Disable black color menu item


[Link](false);

// Add menu items to the menu


[Link](redItem);
[Link](greenItem);
[Link](blueItem);
[Link](blackItem);

// Add the menu to the menu bar


[Link](colorMenu);

// Set the menu bar for the frame


setJMenuBar(menuBar);
Jamia Polytechnic Akkalkuwa Page No:3 Prepared by: Sayyed Waliullah
Java Programming Practical:15 Lab Manual (Solved)

// Adding action listeners for menu items


[Link](e -> getContentPane().setBackground([Link]));
[Link](e -> getContentPane().setBackground([Link]));
[Link](e -> getContentPane().setBackground([Link]));

// Set the frame visible


setVisible(true);
}

public static void main(String[] args) {


new ColorMenuApp();
}
}
3. Write the procedure to assign shortcut key to the Menu Item.
1. Create a JMenu and JMenuItem.
2. Use setAccelerator(KeyStroke) to assign a shortcut key to the JMenuItem.
3. Add the JMenuItem to the JMenu.
4. Add the JMenu to the JMenuBar.
5. Set the JMenuBar to the JFrame.
Example:
import [Link].*;
import [Link].*;

public class MenuWithShortcut {


public static void main(String[] args) {
JFrame frame = new JFrame("Menu with Shortcut Key");
JMenuBar menuBar = new JMenuBar();

// Creating Menu
JMenu fileMenu = new JMenu("File");

// Creating Menu Item with shortcut key (Ctrl+N)


JMenuItem newItem = new JMenuItem("New");
[Link]([Link](KeyEvent.VK_N,
KeyEvent.CTRL_DOWN_MASK)); // Ctrl+N shortcut

Jamia Polytechnic Akkalkuwa Page No:4 Prepared by: Sayyed Waliullah


Java Programming Practical:15 Lab Manual (Solved)

// Adding action to the menu item


[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](frame, "New File Created");
}
});

// Adding Menu Item to Menu


[Link](newItem);

// Adding Menu to Menu Bar


[Link](fileMenu);

// Setting Menu Bar to JFrame


[Link](menuBar);

// JFrame settings
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

Jamia Polytechnic Akkalkuwa Page No:5 Prepared by: Sayyed Waliullah

Common questions

Powered by AI

Assigning shortcut keys in a Java program involves using the setAccelerator(KeyStroke) method on a JMenuItem. It's a five-step process: create a JMenu and JMenuItem, use setAccelerator to bind the KeyStroke, add the JMenuItem to the JMenu, attach the JMenu to a JMenuBar, and set this JMenuBar to a JFrame. This feature is useful for enhancing user experience by providing quick keyboard access to menu options, improving interface efficiency .

Java's Event-Driven Programming can be leveraged to create intuitive user interfaces by attaching event listeners to UI components that trigger specific actions in response to user interactions. For example, in a menu interface, selecting a menu item could trigger an ActionListener that performs tasks such as opening a new file or changing settings, providing immediate feedback. This approach results in responsive and dynamic interfaces that enhance user experience by reacting immediately to inputs .

A Java program can utilize radio buttons and checkboxes using Swing components such as JRadioButton and JCheckBox. Key components involved include creating a JFrame to hold components, using ButtonGroup to group radio buttons so that only one can be selected at a time, setting bounds for positioning elements, and implementing ActionListener to handle events upon actions like button submissions. The program can display results based on user input by using a JLabel to reflect the current state based on selections .

To change the background color of a frame based on menu item selection in Java Swing, attach an ActionListener to each JMenuItem and use the getContentPane().setBackground(Color) method within the event listener to set the desired color. For instance, selecting the 'Red' menu item would trigger an event that sets the background color of the frame to red using setBackground(Color.RED).

When setting default closure operations in a Java Swing application, consider user experience and resource management. The preferred operation is JFrame.EXIT_ON_CLOSE, which terminates the application upon closing the window, releasing resources. To achieve this, use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) within the JFrame setup. Consider alternative operations like HIDE_ON_CLOSE or DO_NOTHING_ON_CLOSE for situations where you want to hide the window or manage resources differently .

Using an IDE like VS Code is crucial for Java development, particularly for building GUI applications, because it offers syntax highlighting, debugging tools, and suggestions that simplify coding and reduce errors. Additionally, IDEs streamline the development process by providing real-time feedback and tools for designing interfaces visually, enhancing productivity and allowing developers to focus on logic rather than setup and configuration .

Handling action events in Java Swing applications is significant for making applications interactive by responding to user inputs like button clicks. It can be implemented by adding an ActionListener to a JButton and overriding its actionPerformed method to specify the actions to be taken when the button is clicked. This pattern allows developers to execute specific code in response to user actions, such as updating the display or processing data .

In Java GUI programming, JCheckBox allows multiple selections where each checkbox operates independently, suitable for options where multiple choices are valid. In contrast, JRadioButton is used within a ButtonGroup to offer mutually exclusive options where only one radio button can be selected at a time. This affects UI design by defining the type of user interactions possible—checkboxes for multiple selections and radio buttons for exclusive single selections .

A ButtonGroup in Java Swing is implemented by creating a ButtonGroup object and adding JRadioButton instances to it. The purpose of a ButtonGroup is to enforce mutual exclusivity among the radio buttons—ensuring that only one can be selected at a time. Without ButtonGroup, radio buttons operate independently, allowing multiple selections which defeats the purpose of exclusive choice interaction in the user interface .

To disable a menu item in Java Swing, use the setEnabled(false) method on the JMenuItem object. This method grays out the menu item, making it unresponsive to user actions, effectively preventing any associated events from being triggered when the item is clicked. For example, in a color menu, disabling the 'Black' menu item would prevent it from being selected .

You might also like