Lecture 10 GUI JMenuItem, JMenu, JDialogBox
Lecture 10 GUI JMenuItem, JMenu, JDialogBox
Lecture # 10
1
Course Books
◼ Text Book:
◼ Herbert Schildt, Java: The Complete Reference, McGraw-Hill
Education, Eleventh Edition
◼ Craig Larman, Applying UML & patterns, 2 edition
◼ Reference Books:
◼ Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
◼ Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Course Instructors
◼ Umm-e-Laila [email protected]
Assistant Professor, CED
Room Number: BS-04
Tel: 111-994-994, Ext. 536
◼ http://sites.google.com/site/ulaila206
4
REVISION
What is Menu?
◼ A menu provides a space-saving way to let
the user choose one of several options. Other
components with which the user can make a
one-of-many choice include combo boxes ,
lists , radio buttons and tool bars.
◼ A menu is a way to arrange buttons. There
are several types.
What is Menu?
◼ TRADITIONAL DROPDOWN MENUS:
❑ These are positioned across the top of a window
in a menu bar, and displayed below the menu
name.
◼ POPUP MENUS:
❑ These appear when the user clicks, eg with the
right mouse button, on a component that can
handle a popup request.
What is Menu?
◼ JMENU:
❑ It has a name and contains a number of menu
items which are displayed in a vertical list of menu
items.
◼ JMENUBAR:
❑ It is positioned across the top of a container (eg a
JFrame, JPanel, or JApplet). It's placed above the
content pane, so does not use the container's
layout.
What is Menu?
◼ JMENU ITEMS:
❑ These are usually text "buttons", but can also
have icons, checkboxes, radio buttons, or be
hierarchical submenus.
What is Menu?
◼ JSEPARATOR:
❑ The JSeparator class is a special component that
acts as a separator on a JMenu.
❑ JSeparator can be used anywhere that we want to
use a horizontal or vertical line to separate
different areas of a screen.
❑ The JSeparator class provides a horizontal or
vertical dividing line or empty space
What is Menu?
What is Menu?
◼ First, add the menu bar to the frame:
public class MyFrame extends JFrame
{
public MyFrame()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
. ..
}
. ..
}
◼ This statement :
String input = showInputDialog( "Enter your
name" );
import javax.swing.*;
import static javax.swing.JOptionPane.*;
public class MyApp
{
public static void main( String [] args )
{
Icon icon = new ImageIcon( "mertz.jpg" );
String ttl = "You Judge";
String msg = "Is this man guilty?";
int ans = showConfirmDialog ( null, msg, ttl,
YES_NO_OPTION, 0, icon ); } }
JDialogBox : Confirm Dialogs
String ttl = "Save File";
String msg = "Do you wish to save your changes?";
int ans = showConfirmDialog ( null, msg, ttl,
YES_NO_CANCEL_OPTION, WARNING_MESSAGE );
JDialogBox : Confirm Dialogs
String ttl = "Delete";
String msg = "All records will be deleted";
int ans = showConfirmDialog( null, msg, ttl,
OK_CANCEL_OPTION );
JDialogBox : Confirm Dialogs
int ans;
ans = showConfirmDialog( null, "Do you want to
continue?" );
JDialogBox : Confirm Dialogs