Graphical User Interface (GUI), Part 1: Applets The Model-View-Controller GUI Architecture
Graphical User Interface (GUI), Part 1: Applets The Model-View-Controller GUI Architecture
Note this is a huge area many books are devoted solely to this
topic. Here we will provide an overview and how to get started.
OOP: GUI-1
Applet
An applet (application-let) is a Java program that runs in an
internet browser. Characteristics of an Applet
n n n
Typically a smaller application. Consists of a user interface component and various other components. Program is downloaded.
u
Does not require any software to be installed on the client maschine. Called by the system not called by the programmer. Has a special <APPLET> tag for this.
Applet, cont
code="MyClass.class" codebase="http://www.myHome.com" archive="MyJarFile.jar" height="100" width="200"> </applet> <!-- never omitted --> <applet
Deprecated in HTML 4.0 (and XHTML), widely supported. Replaced by the <object> tag. For details on applets see package javax.swing.JApplet
and java.applet.Applet.
OOP: GUI-1
Applet, cont
From java.applet.Applet.
OOP: GUI-1
Model-View-Controller Design
Swing architecture is rooted in the model-view-controller
(MVC) design (from the programming language SmallTalk). In the MVC architecture a visual application is broken up into three separate parts.
n n n
A model that represents the data for the application. A view that is the visual representation of that data. A controller that takes user input on the view and translates that to changes in the model. View Model Controller
OOP: GUI-1
Model-View-Controller, cont.
Philosophy: Don't call use, we call you! (event driven). Model
n
The core logic of the program that processes data independent of the GUI. Presentation of the model. There can be several views on the same model. Output to screen. Input from devices such as keyboard and mouse. Effect the model directly and the view indirectly.
View
n n n
Controller
n n
Merge the view and controller parts into a single User-Interface (UI) part. Very difficult to write a generic controller that does not know the specifics about a view. Center an application around its data not its user interface. Component Controller Model View UI Object
OOP: GUI-1
// the model class class Model { private int x; private int y; public Model () { x = 0; y = 0;} public int getX() {return x;} public void setX(int x) {this.x = x;} public int getY() {return y;} public void setY(int y) {this.y = y;} public int calc() {return x*y;} }
OOP: GUI-1 8
/* see next slide for ActionListener */ AL al = new AL(); public void init() { Container cp = getContentPane(); cp.setLayout (new GridLayout(4,2)); // change layout cp.add(xl); cp.add(x); cp.add(yl); cp.add(y); cp.add(prodl); cp.add(prod); cp.add(calc); calc.addActionListener(al); // add action list }
OOP: GUI-1 9
OOP: GUI-1
10
OOP: GUI-1
GUI Overview
To create a Java GUI, you need to understand
n n n n n n
OOP: GUI-1
12
Containers
A container is a special component that can hold other
components. The AWT Applet class, as well as the Swing JApplet class, are containers. Other containers include
n
Frames
u u
A frame is a container that is free standing and can be positioned anywhere on the screen. Frames give the ability to do graphics and GUIs through applications and applets.
n n n n
OOP: GUI-1
14
Applet
Panel
Tabbed Pane
Dialog
Scroll Pane
Toolbar
[Source: java.sun.com]
Frame
OOP: GUI-1
Split Pane
15
Special Containers
Internal frame
Layered pane
Root pane
[Source: java.sun.com]
OOP: GUI-1 16
Events
Every time the user types a character or pushes a mouse button, All the objects have to do implement the appropriate interface
and be registered as an event listener on the appropriate event source. an event occurs. Any object can be notified of the event.
Button
ActionEvent
OOP: GUI-1
17
Events, cont.
Several events implemented in java.awt.AWTEvent
subclasses (java.awt.Event is deprecated).
n
public abstract class AWTEvent extends EventObject { public void setSource(Object newSource); public int getID(); public String toString(); public String paramString(); protected void consume(); protected boolean isConsumed(); }
OOP: GUI-1
18
Events Handlers
In the declaration for the event handler class, one line of code
specifies that the class either implements a listener interface (or extends a class that implements a listener interface).
public class MyClass implements ActionListener
OOP: GUI-1
20
Button clicks (action events). see previous slide. Click button Close frame Press mouse button Move mouse Component visible ActionListener WindowListener MouseListener MouseMotionListener ComponentListener FocusListener
OOP: GUI-1
21
public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); }
OOP: GUI-1
22
Layout Managers
A layout manager is an object that determines the manner in
which components are displayed in a container.
Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Box Layout Overlay Layout
(in java.awt) (in java.awt) (in java.awt) (in java.awt) (in java.awt) (in javax.swing) (in javax.swing)
OOP: GUI-1
23
OOP: GUI-1
24
Flow Layout
A flow layout puts as many components on a row as possible,
then moves to the next row Rows are created as needed to accommodate all of the components. Components are displayed in the order they are added to the container. The horizontal and vertical gaps between the components can be explicitly set. Default for JPanel.
OOP: GUI-1
25
Border Layout
A border layout defines five areas into which components can
be added. The default for most GUIs
OOP: GUI-1
26
Box Layout
A box layout organizes components either horizontally (in one
row) or vertically (in one column). Special rigid areas can be added to force a certain amount of spacing between components. By combining multiple containers using box layout, many different configurations can be created. Multiple containers with box layouts are often preferred to one container that uses the more complicated gridbag layout manager.
OOP: GUI-1
27
OOP: GUI-1
28
"Atomic" Components
The root in the component hierarchy is JComponent. The JComponent provides the following functionality to its
descendants, e.g., JLabel, JRadioButton, and JTextArea.
n n n n n n n n
Tool tips Borders Keyboard-generated actions Application-wide pluggable look and feel Various properties Support for layout Support for accessibility Double buffering
OOP: GUI-1
29
Basic Components
Button Menu
Combo Box
Slider
List
Text Field
[Source: java.sun.com]
OOP: GUI-1 30
Non-Editable Displays
Label
Progress bar
Tool tip
[Source: java.sun.com]
OOP: GUI-1
31
Editable Displays
File Chooser
Color Chooser
Table
Text
Tree
OOP: GUI-1
[Source: java.sun.com]
32
Summary
The Model-View-Controller GUI Architecture
n
OOP: GUI-1
33