You are on page 1of 11

GUI continued

SEED Infotech Pvt. Ltd. 1

Objectives of This Session

Demonstrate the usage of Dialog class

Demonstrate the use of frequently used swing


Distinguish between AWT & Swing

Explain the MVC architecture using an example

Explain the Graphics context and the use of repaint ()

and paint Component()

SEED Infotech Pvt. Ltd. 2

Dialog Boxes

A dialog box is a window with a title bar.

class Dialog inherits from class Window.
Dialog boxes can be modal or modeless.
Modal dialogs - doesn’t allow any other window
in the application to be accessed till Dialog is
Modeless dialogs - allows other windows in the
application to be accessed while dialog is

SEED Infotech Pvt. Ltd. 3

Class Dialog

Constructors -
Dialog(Frame parent, String title)
Dialog(Frame parent, String title, boolean

Some methods -
isModal() - indicates if the dialog is modal.
show() shows the dialog.

SEED Infotech Pvt. Ltd. 4


Steps to create a Dialog box -

Extend a class from Dialog.
In the constructor of extended class call the
constructor of super class Dialog.
In this constructor, pass parent window, title
& modal/modeless option for dialog.
Add controls to the dialog.
Call dialog’s show() or setVisible() method.

SEED Infotech Pvt. Ltd. 5

MVC architecture

Model : describes the contents of a component.

View : describes the visual appearance of a component
Controller: describes the behavior (reaction to events) of a

SEED Infotech Pvt. Ltd. 6

MVC architecture

Don’t make one object responsible for too much.
Instead, associate the look & feel of component with one
object & store the contents in another object.
The controller functions are responsible for connecting user
actions to the model & views.
Allows the programmer to change the look & feel of an
interface without changing the behavior of the underlying

SEED Infotech Pvt. Ltd. 7

Graphics Class

Graphics is an abstract class.

A graphics context enables drawing on screen in
A Graphics object encapsulates state info needed for
the basic rendering options that java supports.
It remembers a collection of settings for drawing
images &text.
All drawing in Java must go thru a Graphics object.

SEED Infotech Pvt. Ltd. 8

paint() method

This method of the Component class does nothing

by default – it must be overridden by the
Should not be called directly by programmer,
because drawing graphics is a event-driven
Even if called directly, the programmer should
ensure that he obtains a graphics handle for the
component & passes this handle as a parameter to
the paint() method.

SEED Infotech Pvt. Ltd. 9

repaint() method

Frequently called by programmer to force a paint()

Should not be overridden because it performs some
system-dependent tasks.
Eg. Obtaining a graphics context to pass on.
repaint() calls the update() method.
update() clears the components background of any previous
drawing & calls paint() directly.

SEED Infotech Pvt. Ltd. 10

paintComponent() method

This method is on similar lines to the paint() method of awt.

SEED Infotech Pvt. Ltd. 11