Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6
Jan 13, 2016
Java Programming: Advanced Topics
1
Common Elements of Graphical User Interfaces
Chapter 6
Java Programming: Advanced Topics
2
Objectives
• Learn the characteristics of a graphical user interface
• Explore the contents of the Java Foundation Classes
• Discover how the JFC supports creating GUIs for the Java platform
• Design GUIs with layouts that dynamically adjust to screen resolution and window size
• Include predefined components such as text fields, buttons, and multimedia elements in your GUI
Java Programming: Advanced Topics
3
Objectives (Cont.)
• See how the JFC provides a framework for interacting with the user in event-driven programs
• Write code using component, container, and layout manager classes of the AWT and Swing
• State the name of the event class for any specified listener interface in the java.awt.event package
• Write code to implement the paintComponent method of a javax.swing.Jcomponent component
Java Programming: Advanced Topics
4
Main Features and Terminology of a GUI
• Components are predefined standard elements (such as buttons, text fields, frame windows, and dialog boxes)
• The layout managers control the way components are arranged within a container
• Graphics: drawing in the graphics context for a component
• Graphics context: is the area on the display screen where a component is positioned
Java Programming: Advanced Topics
5
Main Features and Terminology of a GUI (Cont.)
• Painting: the process of displaying components or refreshing the display of components on the screen
• Events can be triggered by the operating system or by user actions
• The adapter is a class that implements all the methods of the interface by providing empty methods for all of them
• Each listener interface that has more than one method also has a corresponding adapter class
Java Programming: Advanced Topics
6
Introducing the Java Foundation Classes
• The goal of the JFC is to simplify the development of 100% Pure Java programs for network or standalone environments
• The classes enable you to:– Design GUIs that reflect the operating system that is
host to the JVM– Create your own platform-independent interface – Use a look and feel defined for the Java platform and
common across all implementations of the JVM
Java Programming: Advanced Topics
7
Introducing the Java Foundation Classes
Java Programming: Advanced Topics
8
Introducing the Java Foundation Classes
Java Programming: Advanced Topics
9
Abstract Windowing Toolkit API
Java Programming: Advanced Topics
10
Abstract Windowing Toolkit API (Cont.)
Java Programming: Advanced Topics
11
Abstract Windowing Toolkit API (Cont.)
Java Programming: Advanced Topics
12
Event Model• Define any class to be an event handler by
implementing a listener interface• Call a method of a component class to register
the listener with the component• When an event that relates to the component
occurs, only the registered listeners are notified and passed a copy of the event
• The event model is consistent with the JavaBeans interface
Java Programming: Advanced Topics
13
Swing API• The Swing API consists of more than 250
classes and 75 interfaces grouped into the following categories:– JComponent: extends the AWT class
Container and a set of components that are subclasses of JComponent
– Nonvisible support classes that provide important services
– A set of related interfaces that are implemented by Swing component and support classes
Java Programming: Advanced Topics
14
Swing Components
Java Programming: Advanced Topics
15
Swing Components (Cont.)
Java Programming: Advanced Topics
16
Swing Components (Cont.)
Java Programming: Advanced Topics
17
Swing Components (Cont.)
Java Programming: Advanced Topics
18
Separable Model Architecture• Swing components separate the manipulation of
data associated with a component from the rendering or graphical-representation component and its contents
• The Swing API uses a separable model architecture
• Swing models use two different styles of notification: – Lightweight– Stateful
• The Swing API defines a set of model interfaces
Java Programming: Advanced Topics
19
Swing Model Interfaces
Java Programming: Advanced Topics
20
Swing Model Interfaces (Cont.)
Java Programming: Advanced Topics
21
An Example of JFC Program
Java Programming: Advanced Topics
22
Layout Managers• Layout managers automate the positioning of
components within containers• Standard layout managers:
– BorderLayout– FlowLayout– GridLayout– GridBagLayout– BoxLayout– CardLayout
• The javax.swing.JTabbedPane and javax.swing.JSplitPane containers in the Swing API have built-in layout characteristics
Java Programming: Advanced Topics
23
Layout Managers and Related Interfaces
Java Programming: Advanced Topics
24
Border Layouts• The BorderLayout class implements the
LayoutManager2 interface to support a container that holds up to five components:– North– South– West– East– Center
• By default, the JWindow, JFrame, and JDialog containers have border layouts
Java Programming: Advanced Topics
25
The BorderLayout Strategy
Java Programming: Advanced Topics
26
Flow Layouts
• The class FlowLayout implements the LayoutManager interface
• Components are arranged in a row across the area of the container
• Components in a flow layout retain their preferred size
• By default, JPanel objects have flow layouts
Java Programming: Advanced Topics
27
A FlowLayout Example
Java Programming: Advanced Topics
28
Grid Layouts• The class GridLayout implements the
LayoutManager2 interface• GridLayout manager divides the area of the
container into a grid of equally sized rows and columns
• Components are automatically put into the next cell, in a left to right order, filling rows from top to bottom
Java Programming: Advanced Topics
29
A GridLayout Example
Java Programming: Advanced Topics
30
Grid-Bag Layouts• The class GridBagLayout implements the
LayoutManager2 interface to support grid-bag layouts
• A grid-bag layout is based on a rectangular grid, and it uses a helper class, GridBagConstraints, to specify how each component should be located within the grid
Java Programming: Advanced Topics
31
A GridBagLayout Example
Java Programming: Advanced Topics
32
Events• Events: objects that encapsulate changes in
state that are initiated by the user or the operating system
• The AWT API includes the package java.awt.event of the classes that encapsulate the events that relate to the GUI components in your application or applet
Java Programming: Advanced Topics
33
AWT Event Classes
Java Programming: Advanced Topics
34
AWT Event Classes(Cont.)
Java Programming: Advanced Topics
35
Swing Event Classes
Java Programming: Advanced Topics
36
Selected Methods of the Event Classes
Java Programming: Advanced Topics
37
Selected Methods of the Event Classes (Cont.)
Java Programming: Advanced Topics
38
Events that Start Long Operations
Java Programming: Advanced Topics
39
Painting• Painting: the act of drawing onto the graphics
context of a component• All objects that are instances of subclasses of
java.awt.Component handle their own painting• The key methods:
– void paintComponent (Graphics context)– void paint (Graphics context)– void repaint (long time)– void update (Graphics context)
Java Programming: Advanced Topics
40
Basic Support for Graphics• The AWT API provides basic graphics support
through the class Graphics• The drawing methods of the class Graphics are
instance methods of the Graphics class, and each instance of Graphics is the graphics context for a component
• The Graphics class provides a number of methods for drawing onto a component
Java Programming: Advanced Topics
41
Java 2D API • The basic support for drawing was lacking for
high-quality graphics• The goal was to create an API that addressed
the needs of graphics professionals• The Java 2D API was created and included as
part of the JFC
Java Programming: Advanced Topics
42
Notable Java 2D Features
Java Programming: Advanced Topics
43
Summary• The JFC consists of five major APIs, including
the classes and interfaces in the AWT and Swing APIs that you use to program a GUI for an application or applet
• Layout managers are used to arrange the components in a way that adjusts for screen resolutions and window resizings
• Standard layout managers include BorderLayout, FlowLayout, GridLayout, GridBagLayout, Box, and CardLayout
• Each component class supports a number of events that are appropriate to its type of component
Java Programming: Advanced Topics
44
Summary (Cont.)
• The AWT contains a hierarchy of event classes for different kinds of user-initiated events, such as ComponentEvent, MouseEvent, KeyEvent, ItemEvent, TextEvent, and ActionEvent
• Each listener interface that has more than one method also has a corresponding adapter class
• Painting is the act of drawing onto the graphics context of a component, and drawing methods of the Graphics class are paintComponent, paint, and update methods of a component
• The goal of the Java 2D API addresses the needs of graphics professionals