CS136, Lecture 5

    1. Last time covered most Java GUI components
        1. Other GUI components:
    2. Layout managers
      1. FlowLayout
      2. BorderLayout
      3. GridLayout
      4. Mixing layouts
        1. Panels
        2. Canvases
    3. Mouse & Keyboard Events

Last time covered most Java GUI components

Buttons, Labels, Text Fields, TextAreas, Choice buttons

Recall the action routine has declaration:

public boolean action (Event evt, Object obj)

See example Book (but ignore layout).

Other GUI components:

Other components that we are less likely to use include checkboxes, radio buttons, lists, scroll bars, etc. Information on these can be found through the web page.

Layout managers

Layout managers help the programmer lay out components on the applet so that they look good even if the user changes the size of the applet.

FlowLayout

The default Layout manager for an applet is FlowLayout, which simply lays out items from left to right across the top of the applet. When there is no more space it starts a new row. Thus if an applets width is decreased, items from one row may move down to a lower row.

BorderLayout

The BorderLayout provides a bit more control over placement by allowing the programmer to add components into one of 5 areas: North, South, East, West, and Center.

BorderLayout has two constructors:

	public BorderLayout()
	public BorderLayout(int horizGap, int vertGap) 
					// leaves given gap size between components.
The layout manager of an applet can be changed to BorderLayout by executing:
	setLayout(new BorderLayout())
Components can be added by using a more refined version of add:
	add(String direction, Component comp)
Thus to add a button on the north, write
	add("North",startButton)
Note that all directions must begin with a capital letter or an error will occur.

Components added to "North" and "South" extend to the left and right edges of the Applet, while those added "East" and "West" extend up and down to the components to the "North" and "South". The component in the "Center" fills the remaining space.

If one component is omitted, other components stretch to fill that space.

With the BorderLayout, components stretch to fill all of the Applet. As a result using this by itself can often make layouts look distorted.

GridLayout

GridLayout arranges components into evenly spaced rows and columns. Constructors include:
	public GridLayout(int rows, int cols)  
	   // Construct layout w/ given rows and columns
	public GridLayout(int rows, int cols, int h, int v)  
	   // Construct layout w/ given rows and columns, and 
	   // horiz and vertical gaps of h and v between components
Executing
	setLayout(new GridLayout(2,3))
divides the applet into 2 rows and 3 columns. When "add" method is executed, components are added from left to right first across the first row, then second row, etc.

The only somewhat unfortunate thing about GridLayout is that all components added are resized to be exactly the same size.

Mixing layouts

Most of the time, none of the three layout managers described above does exactly what you want them to do. However, you can build up your applet from components called panels and canvases and then place the GUI components directly on those components.

Panels

A Panel is a type of container. GUI components can be added to a panel, which can itself be added to an applet (or indeed another Panel).

The constructor for a panel is:

	public Panel()

Canvases

A Canvas is a component on which graphics can be drawn and that can receive mouse events.

Its constructor is:

	public Canvas()
Both Panel and Canvas have method:
	public void resize(int width, int height)
Canvases must be resized or they will be set to have size (0,0). You can try to resize panels, but the layout manager may stretch them to a somewhat different size.

The example program Book provides a good example of using panels and both FlowLayout and BorderLayout to lay out an applet nicely.

We'll see other examples later.

Mouse & Keyboard Events

Events are also generated every time the user moves the mouse or presses a key on the keyboard.

Back up for moment to talk about event handling. All events go to a default method handleEvent(Event evt)

This default method automatically calls the method action if the event concerns a GUI component.

Other events are routed to other methods (makes it easier to override one without touching others).

No matter how handled, the methods return true to indicate that they were successfully handled, and false otherwise.

Some other events and methods which handle them:

Constants are declared in class Event for some special keys: Notice that constants are declared "final static". Final means they cannot be redefined, while static means there is only one copy for the class (i.e., each object of the class doesn't have to carry a copy).

Constants can be accessed by prefixing the class name, e.g., Event.UP.

Warning: Events can only be handled in Java when nothing else in the applet is being executed. In particular, a mouse click on a "stop" button won't be recognized if other routines are being executed. To do this kind of programming, you need to learn how to spawn "threads" to perform actions while applet is waiting for events to handle.

See "Pusher" for sample code using mouse and key events.