Chapter 8. GUI Programming
Chapter 8. GUI Programming
•Chapter 8
1
Object Oriented Programming Course VKU
2
Object Oriented Programming Course VKU
Introduction to AWT
• AWT stands for Abstract Windowing Toolkit
• AWT is a set of Java classes that allows us to create a GUI
• Provides various items to create an attractive and efficient GUI
• Containers
• Components
• Layout managers
• Graphics and drawing capabilities
• Fonts
• Events
Object Oriented Programming Course VKU
Containers
• Area where you can place your components.
• Can hold elements, can be drawn on, and painted.
• Can have frames, panes, latches, hooks, and smaller-sized components within
it.
• java.awt package contains a class named Container. This class directly or
indirectly derives two commonly used containers:
• Frames
• Panels
Object Oriented Programming Course VKU
Frames
• Are separate windows
• Are a subclass of Window
• Are displayed within a separate window, and have borders
• A Frame is independent of an applet, and of the browser
• Constructors:
• Frame( )
• Frame(String title)
Object Oriented Programming Course VKU
Panels
• Are areas contained within a window.
• Are displayed within the window that the browser or the
appletviewer provides and do not have borders.
• Are used to group a number of components
• A panel cannot be seen directly. Hence, we need to add it to a
frame.
• Constructor
• Panel()
Object Oriented Programming Course VKU
Dialog
• Is a subclass of the class Window
• The dialog object is constructed as follows:
Components
• Can be placed on a user interface, and can be resized, or made
visible
• Examples
• textfields, labels, checkboxes, textareas
• scrollbars, scrollpanes, dialog
Object Oriented Programming Course VKU
Label
• Is used to display the String
• Constructors:
• Label( )
• Label(String labeltext)
• Label(String labeltext, int alignment)
• Methods:
• setFont(Font f)
• setText(String s)
• getText( )
Object Oriented Programming Course VKU
TextField
• Is a single line area, in which text can be displayed, or entered by the user
• Constructors:
• TextField( )
• TextField(int columns)
• TextField(String s)
• TextField(String s, int columns)
• Methods:
• setEchoChar(char)
• setText(String s)
• getText( )
• setEditable(boolean)
• isEditable( )
Object Oriented Programming Course VKU
TextArea
• Is used when text is to be accepted as two or more lines
• Is an editable text field with multi-line features
• Steps for creating TextArea:
• Create an element
• Specify the number of rows or columns it must have (optional)
• Decide its placement on the screen
Object Oriented Programming Course VKU
TextArea (Contd…)
• Constructors:
• TextArea( )
• TextArea(int rows, int cols )
• TextArea(String text)
• TextArea(String text, int rows, int cols)
Object Oriented Programming Course VKU
TextArea Methods
• setText(String)
• getText( )
• setEditable(boolean)
• isEditable( )
• insertText(String, int)
• replaceText(String, int, int)
Object Oriented Programming Course VKU
Button
• Push/ Command button is the easiest way to trap user action
• Steps for creating buttons:
• Create the button element, preferably with a label indicating its purpose
• Decide where to place it on the screen
• Display it on the screen
• Constructors:
• Button( )
• Button(String text)
Object Oriented Programming Course VKU
Choice Lists
• ‘Choice’ class enables to create multiple item lists
• When a list is first created, it is empty
• Steps to create Choice lists:
• Create the list element
• Add items (Strings) to it, one by one
• Decide where to place it on the screen
• Display it on the screen
• Example
Choice colors=new Choice( );
colors.addItem(“Red”);
colors.addItem(“Green”);
Object Oriented Programming Course VKU
Layout Manager
• Different types of layouts:
• Flow Layout
• Border Layout
• Card Layout
• Grid Layout
• GridBag Layout
• Layout manager is set with the method called ‘setLayout( )’
Object Oriented Programming Course VKU
FlowLayout
• Is the default layout manager for applets and panels
• Components are arranged from the upper left corner to the bottom right
corner
• Constructors:
FlowLayout mylayout = new FlowLayout();
FlowLayout exLayout = new flowLayout(FlowLayout.RIGHT);
Object Oriented Programming Course VKU
BorderLayout
• Is the default layout manager for Window, Frame and Dialog
• Layout arranges up to five components in a container
• Components can be assigned to the NORTH, EAST, SOUTH, WEST and CENTER
of the container
• Example: To add component to North region
Button b1= new Button(“North Button”);
setLayout(new BorderLayout( ));
add(b1, BorderLayout.NORTH);
Object Oriented Programming Course VKU
CardLayout
• Can store a stack of several layouts
• Each layout is like a card in a deck
• Card is usually a Panel object
• A separate component such as a button controls the card to be
displayed on top
• Steps for creating card layout:
• Set the layout of the main panel to CardLayout
• Add the other panels to the main panel
Object Oriented Programming Course VKU
GridLayout
• Helps to divide the container into a grid
• Components are placed in rows and columns
• Each grid should contain at least one component
• Is used when all components are of the same size
• Constructor
GridLayout gl = new GridLayout(no. of rows, no. of columns);
Object Oriented Programming Course VKU
GridBagLayout
• Places components precisely
• Components need not be of the same size
• Components are arranged in a grid of rows and columns
• Order of placing the components is not left-to-right and top-to-bottom
• Constructor
GridBagLayout gb = new GridBagLayout( );
Object Oriented Programming Course VKU
GridBagLayout
• To use this layout, information on the size and layout of each
component is needed
• The class ‘GridBagLayoutConstraints’ holds all the information that
the class GridLayout requires to position and size each component
GridLayout g; GridLayoutConstraints gbc;
g=new GridLayout();gbc=new GridLayoutConstraints();
g.setConstraints(Obj,gbc);
gbc.weigthx=x; gbc.weigthy=y;
Object Oriented Programming Course VKU
Handling Events
• Events are handled by:
• Abstract Windowing Toolkit
• Browser
• Event handler written explicitly by programmers
• Application needs to register an event handler with an object
• Handler will be called whenever the appropriate event takes place
for the right object
Object Oriented Programming Course VKU
Menus
• Types of menus :
• Pull-down
• Pop-up menu
• Only one menubar can be placed inside a Frame
• Components of Menu:
• Menubar
• MenuItems
Object Oriented Programming Course VKU
Swing
Object Oriented Programming Course VKU
• Three classes: JFrame, JPanel, and JLabel. These classes are part of a larger
collection of classes that are all related through inheritance.
• The Swing family tree splits at the Component class into one group of
classes that are derived from the JComponent class, and another branch
that descends from the Window class.
Object Oriented Programming Course VKU
Description of Classes
• Object: All classes ultimately derive from Object, thus this class is at the top of the tree.
• Component: represents an object that has a visual representation that can be shown
on-screen and that can interact with users. This class defines some basic methods that
are available to all Swing classes.
Object Oriented Programming Course VKU
• Container: builds on the basic visual capabilities of the Component class by adding the
ability to hold other containers.
• Window: a specialized type of container object that has a border, a title bar, buttons
that minimize, maximize, and close the window, and that can be repositioned and
possibly even resized by the user.
Object Oriented Programming Course VKU
• Frame: a type of Window that serves as the basis for Java GUI applications. Frame is an
AWT class that has been improved upon by the JFrame class.
• JFrame: the Swing version of the older Frame class. Most of the Swing applications
include at least one JFrame object.
• JComponent: is the basis for all other Swing components except for frames.
Object Oriented Programming Course VKU
• JPanel: used to organize and control the layout of other components such
as labels, buttons, text fields, etc. In most Swing applications, one or more
panels are added to a frame. Then, when the frame is displayed, the
components that were added to its panels are made visible.
• JLabel: creates a label that displays a simple text value.
Object Oriented Programming Course VKU
Constructor Description
JFrame ( ) Creates a new frame with no title.
Method Description
void setIconImage (Icon image) Sets the icon displayed when the frame is minimized.
void setLayout Sets the layout manager used to control how
(LayoutManager layout) components are arranged when the frame is
displayed. The default is the BorderLayout manager.
Object Oriented Programming Course VKU
Useful JFrame Constructors and Methods
(Cont’d)
Method Description
void setLocation Sets the x and y position of the frame on-
(int x, int y) screen. The top-left corner of the screen is
0, 0.
Constructor Description
JPanel () Creates a new panel.
JPanel (boolean isDoubleBuffered) Creates a new panel. If the parameter is
true, the panel uses a technique called
double-buffering.
Object Oriented Programming Course VKU
Useful JPanel Constructors and Methods
(Cont’d)
Constructor Description
JPanel (LayoutManager layout) Creates a new panel with the specified
layout manager. The default layout
manager is FIowLayout.
Object Oriented Programming Course VKU
Useful JPanel Constructors and Methods
(Cont’d)
Method Description
void add (Component c) Adds the specified component to the
panel.
void remove (Component c) Removes the specified component
from the panel.
Object Oriented Programming Course VKU
Useful JPanel Constructors and Methods
(Cont’d)
Method Description
void setLayout Sets the layout manager used to control how
(LayoutManager layout)
components are arranged when the panel is
displayed. The default is the FIowLayout
manager.
Object Oriented Programming Course VKU
Useful JPanel Constructors and Methods
(Cont’d)
Method Description
void setLocation (int x, int y) Sets the x and y position of the frame-
screen. The top-left corner of the screen is
0, 0.
Object Oriented Programming Course VKU
Useful JPanel Constructors and Methods
(Cont’d)
Method Description
void setSize (int width, int Sets the size of the frame to the specified width
height)
and height.
void setToolTipText (String Sets the tooltip text that's displayed if the user
text)
rests the mouse over an empty part of the
panel.
Object Oriented Programming Course VKU
Using Labels
• A label is a component that simply displays text. Labels are used for a
variety of purposes: to display captions for other controls such as text fields
or combo boxes, to display informational messages, or to show the results
of a calculation or a database lookup.
Object Oriented Programming Course VKU
Using Labels
• A label can also display an image, or it can display both an image and some
text. And you have complete control over the appearance of the text.
• You can specify the font, size, whether the text is bold, italic, or underlined,
what color the text is displayed as, and so on.
Object Oriented Programming Course VKU
Constructor Description
JLabel ( ) Creates a new label with no initial text.
Method Description
String getText ( ) Returns the text displayed by the label.
void setText (String text) Sets the text displayed by the label.
Object Oriented Programming Course VKU
Useful JLabels Constructors and Methods
(Cont’d)
Method Description
void setToolTipText (String Sets the tooltip text that's displayed if the
text)
user rests the mouse over the label for a few
moments.
Creating Buttons
• Next to labels, the Swing component used most is the JButton component
which creates a button the user can click.
• The constructors of the JButton class are similar to the constructors for the
JLabel class. You can either create an empty button or a button with text.
Object Oriented Programming Course VKU
Constructor Description
JButton ( ) Creates a new button with no initial
text.
JButton (String text) Creates a new button with the
specified text.
Object Oriented Programming Course VKU
Useful JPanels Constructors and Methods
(Cont’d)
Method Description
doClick ( ) Triggers an action event for the button as if
the user clicked it.
void setEnabled (boolean value) Enables or disables the button. The default setting
is true (enabled).
Object Oriented Programming Course VKU
Useful JPanels Constructors and Methods
(Cont’d)
Method Description
void setRolloverEnabled Enables or disables the rollover effect, which
(boolean value)
causes the border to get thicker when the
mouse moves over the button. The default
setting is true (rollover effect enabled).
Object Oriented Programming Course VKU
Useful JPanels Constructors and Methods
(Cont’d)
Method Description
void setText (String text) Sets the text displayed by the button.
void setToolTipText (String Sets the tooltip text that's displayed if the user lets
text) the mouse rest over the button.
void setVisible (boolean value) Shows or hides the button. The default setting is
true (the button is visible).
Object Oriented Programming Course VKU