0% found this document useful (0 votes)
18 views

Lecture 10 GUI JMenuItem, JMenu, JDialogBox

The document discusses different types of dialog boxes in Java including message dialogs, input dialogs, and confirm dialogs. It explains how to create dialog boxes using the JOptionPane class and static methods like showMessageDialog, showInputDialog, and showConfirmDialog. Examples are provided to demonstrate how to display different types of dialog boxes and get user input or selection from the dialog.

Uploaded by

Abdul Haseeb
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)
18 views

Lecture 10 GUI JMenuItem, JMenu, JDialogBox

The document discusses different types of dialog boxes in Java including message dialogs, input dialogs, and confirm dialogs. It explains how to create dialog boxes using the JOptionPane class and static methods like showMessageDialog, showInputDialog, and showConfirmDialog. Examples are provided to demonstrate how to display different types of dialog boxes and get user input or selection from the dialog.

Uploaded by

Abdul Haseeb
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
You are on page 1/ 49

Graphical User Interfaces Part 4

Advance Controls in GUI

Compiled By: Umm-e-Laila

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

◼ Aneeta Siddiqui [email protected]


Assistant Professor, CED
Room Number: BS-03
Tel: 111-994-994,
Course Website

◼ 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);
. ..
}
. ..
}

◼ Add menus to the menu bar


JMenu fileMenu = new JMenu("File");
JMenu fontMenu = new JMenu("Font");
menuBar.add(fileMenu);
menuBar.add(fontMenu);
What is Menu?
◼ Add menu items and subitems:
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
JMenu styleMenu = new JMenu("Style");
fontMenu.add(styleMenu); // A submenu
JMenuItem and JMenu Example
import javax.swing.*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample(){
JFrame f= new JFrame("Menu and MenuItem Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
JMenuItem and JMenu Example
public static void main(String args[])
{
new MenuExample();
}}
JDialogBox

◼ The Java API class javax.swing.JOptionPane has


facilities for creating a dialog box that can appear on the
computer’s desktop to request input from or display
messages to the user.

◼ Here are three easy dialog boxes provided by the class.


Three Types of JOptionPane Dialog
Boxes
Three Types of JOptionPane Dialog
Boxes
JOptionPane Message Dialogs
JOptionPane Message Dialogs
JDialogBox

◼ The dialog box is modal, meaning that the execution of


the Java program is blocked until the user’s interaction
with the box is completed, which happens when the user
clicks the OK button or the Close button
JDialogBox Example
import static javax.swing.JOptionPane.*;
public class MyApp {
public static void main( String [] args )
{
Icon icon = new ImageIcon( "essent.jpg" );
String ttl = "Essent";
String msg = "Electronic music for the 21st century";
showMessageDialog( null, msg, ttl, 0, icon );
} }
JDialogBox Example
JDialogBox
◼ In the Java API, ease-of-use constants are
usually defined as static fields within the class. A
list of static fields within the
javax.swing.JOptionPane that are valid for the
message type parameter is shown on the next
page.
◼ To use these constants, simply pass them as the
fourth argument to the
showMessageDialogmethod.
JDialogBox
JDialogBox
JDialogBox
JDialogBox
JDialogBox : Input Dialogs
JDialogBox : Input Dialogs
JDialogBox : Input Dialogs
JDialogBox : Input Dialogs
import static javax.swing.JOptionPane.*;

public class MyApp {


public static void main( String [] args )
{
String prompt = "Enter 'yes' to confirm deletion";
String title = "Warning";
String input = showInputDialog( null, prompt, title,
WARNING_MESSAGE );
} }
JDialogBox : Input Dialogs
String prompt = "Enter your birthday";
String input = showInputDialog( prompt, "mm/dd/yyyy" );

String input = showInputDialog( "Enter your


name" );
JDialogBox : Input Dialogs

◼ An input dialog is modal, blocking the Java program until


the user’s interaction with the box is completed, which
happens as the following table explains:
JDialogBox : Input Dialogs

◼ This statement :
String input = showInputDialog( "Enter your
name" );

◼ Displays this dialog:


JDialogBox : Input Dialogs

◼ If the user types within the text field and clicks OK or


presses Enter, any contents of the text field is placed into
a String object and its reference placed into variable
input.
JDialogBox : Input Dialogs

◼ If the user clicks OK or presses Enter with nothing in the


text box, a String object containing the null string is
returned.
JDialogBox : Input Dialogs

◼ If the user clicks Cancel or Close (no matter what’s in the


text field), the null pointer is returned.
JDialogBox : Confirm Dialogs
JDialogBox : Confirm Dialogs
JDialogBox : Confirm Dialogs
JDialogBox : Confirm Dialogs
JDialogBox : Confirm Dialogs

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

◼ A confirm dialog box is modal, blocking the Java


program until the user clicks one of the option buttons or
the Close () button.

◼ The showConfirmDialog method returns an integer code


indicating which button the user clicked, which your
program can check using an if-else or switch statement.
JDialogBox : Confirm Dialogs
JDialogBox : Confirm Dialogs

You might also like