Web programming

Units WEB1P and WEB2P

Java applets

What is an applet?

What can they do?

See the Java tutorial trail Writing Applets for more details.

User interface

Applets are able to use the full power of the Java GUI. This was originally based around the Abstract Window Toolkit (AWT), but modern applets use the newer Swing toolkit.

AWT

A tutorial on AWT Fundamentals is available online.

In AWT the basic element of a user interface is a component. A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface. These are represented by the classes Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent.

An Applet is a sub-class of Container that implements the additional facilities necessary to run in a browser window.

The core of the AWT is the paint method (strictly a Container method). This is where you draw what you want to appear on the screen. You must always draw the entire screen - not just the bits that you want to change - because the screen is cleared at the start of the painting. Your program never calls paint; it is called from within the framework. If you want to initiate an update to the screen, you call repaint and it calls paint for you. The parameter to paint is a Graphics context. Most of the basic drawing functions are methods in this class (e.g. drawString, drawLine, drawPolygon, drawOval).

Examples

Controls

Controls are visual components that respond to being altered by the user (typically with the mouse). These include buttons, scrollbars, check boxes, choice boxes (menus) and text fields.

When something happens to one of these (e.g. a button is clicked), an event occurs. If your program wants to be notified when a control event occurs, you must write a class that implements the appropriate listener interface (e.g. ActionListener, MouseListener, WindowStateListener).

Controls are added to the container. The control must be told what class to notify of its listened-for events.

Examples

MVC

The examples above show simple one-class applets. In reality, an applet is likely to be composed of many classes. A good way of managing these is to use the MVC (Model-View-Controller) pattern that we discussed in the context of servlets.

Example

Layout management

AWT also provides facilities for managing the layout of a component. By default, an applet uses the flowLayout interface, which lays visual controls out in the order in which you add them.

Layout managers deal with issues such as window resizing and the relative positioning of controls. They will ensure (normally!) that all controls remain visible.

Other layout managers available include BorderLayout (has five regions: north, south, east, west, center), CardLayout (stack of cards) and GridLayout (rectangular grid) . Layout managers can be nested for finer control (using Panels).

Example

Swing

It was quickly realised that the facilities in AWT did not provide everything that programmers would need to implement great GUIs.

Swing was created to provide a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms. For a programmer's guide to using these components, see Creating a GUI with JFC/Swing, a trail in The Java Tutorial.

NetBeans has excellent facilities for developing Swing components.

Example

Security

One of the main goals of the Java environment is to make browser users feel secure running any applet. Why would anyone allow a program to run on their computer that they did not trust? For this reasons, applets loaded over a network are restricted in what they can do. In particular they cannot:

A security exception is raised if any of the above is attempted. An applet run locally (e.g. via the AppletViewer) is not restricted in the same way, so be cautious that just because you've tested your applet in AppletViewer doesn't mean that it will work the same once downloaded into the user's browser.

An FAQ on applet security is available online.

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The web programming units include some material that was formerly part of the WPRMP, WECPP, WPSSM and WEMAM units.