Components, Panels, Applets

Panels provide the space in which you can build your Applet. You can attach any GUI Component to a Panel, including other Panels.

Specifically, the Applet class provided with Java implements a basic Panel (is a Container) that you can use directly for simple interfaces (can put other Components or Containers into it). (figure 13.2 from 21 days)

Example: (these are named badly - note these are all applets) (figure 1)

Notice button instantiation The String you specify during the instantiation will appear on the button when it is displayed on the screen. The string will also be returned for use as identification when an event has occurred.

NOTE: There are no x, y coordinates because these are relative to the panel and platform.

Instead we will use Layout Managers. The default is the Flow Layout - fills in objects to the right until it flows to the next line (more later)

The above button doesn't help unless it does something.Recall that whenever a user clicks a button, an event happens (as with mouseDown and mouseUp events).

The button methods can be "caught" by overriding the action method of an applet. (The mouse and key events go to action methods with different signatures)

Events: these are system messages - a result of physical action by a user (usually).

One has a canvas on top of a Window. If the user clicks the mouse on the canvas, the canvas gets the event Multiple Buttons:

Distinguishing between multiple buttons can be done by using the target object passed by the Event object and comparing it to your button objects:

Note: here we could say ev.target == b1 since they literally are the same object. This would be faster (why?)

Other Button Types

Choice (like menus)

The string supplied to the addItem() method will be returned in the Object argument of a Choice event.

Checkbox Buttons are often used as state buttons. They provide toggle values on and off (T/F). The state of the button is returned as the Object argument for Checkbox events. (arg is true if checked, false otherwise) Both the name and state are returned in the Event arg. They can be retrieved using getLabel() and getState()

For the radio action (one on at a time), the Checkbox can be grouped. When one is selected, the others turn off.

Another technique for Buttons: (more OO honest) Since Java is object-oriented, it gives us the capability of creating completely self-contained buttons. In these types of buttons, you build the event handling function directly into an extended button class.

You can then add the button to your applet without worrying about any events the applet would generate.

Examples:

Suppose you actually want to make your own Button

Then in some class (whatever) you add the new subclass intances

Then the applet class does not deal with events from my Button, myButton does

Another example: (we have a specific kind of buton we want to subclass...we always know what that button does)

Test it:

Notice there is no action() method in the buttonTest applet class. The OkButton class will handle the button events.

If we had placed other objects in the applet, we could still include an action() method to handle their events.

Context:
So far we have been within an Applet. Consider applet
init()
make an instance
of button
...
							\ reference to
MyButton

action
...
 another applet
adds button

Suppose you have a button (or text area, etc) in a different applet (or different component is a better way to say this). We want a buttons actions to affect things in various applets. Then MyButton needs a reference to the applet object. So, one part is to send reference to self so it knows where it is.

in method: public MyButton (Object this, String label) OR, the add could be in some other applet.

In the interface, the button observer can pass the generic applet

TextFields

For direct user input of information via the keyboard.
You can choose one of four options:

When the user hits return in a text field, the action generates a text field event. You catch this event in the action method.

Text Areas: Java also allows programmers to use multi-lined text as a field (text area). For text areas you must also specify the number of rows:

To access the text currently in a TextArea you can use getText() TextAreas do not generate any unusual events, but you can use other events to drive access to the text in the event handler:

The above is a good example of how you might have components in different components (make sense) and you want to have a handle on them (use of self as parameter) OR how subclassing buttons can direct actions (when the send button was made, it could have made an IV to ta1)

Lists:

List l = new List (5, false);
Displays 5 lines of the list at a time; true allows multiple selection, false allows only one.
Applet provides a scrollbar for list items that exceed the viewing area

To access the selected items from a list, use the getSelectedItem() or getSelectedItems() methods.

For single selected lists, any double-click selection in a list will trigger an event that you catch in the action() method (first click chooses, second click triggers event) (this is questionable given latest communications on clicking) For multiple-select lists you need an external event to trigger your actions. For example:

Scrollbar example - test it and see how it works LayoutManager

FlowLayout

Basic - default for every panel, default placement is center

Components added to a panel follow each other in a list fashion. You position horizontally, you can specify the spaces between each component. setLayout(new FlowLayout(FlowLayout.LEFT,20,5)); // RIGHT

BorderLayout

Border provides 5 areas: North, South, East, West, Center

The center represents everything once the directions are filled

GridLayout

Rows and Columns  (fills across row (i.e. columns) first)
column major?
1 2
3 4
5 6

See GridBagLayout in "Java in a Nutshell" (page 108-114, and 256-257) (most powerful)

Canvases

If your applet will deal with graphics directly, canvases are a good idea. Once you start filling up a window with lots of interface elements and multiple panels, it is best not to draw directly onto the window surface any more. This is because your drawing will interfere with the buttons. Instead you add a canvas to the window.

A canvas is simply a rectangular area in which you can draw. (In contrast, a panel is a rectangular area into which you can place user interface components.) Making a canvas is a bit more complex than using a panel because you need to specify how to draw on the canvas. This means you must derive a new class from Canvas and then override the paint method in your extended class.

Canvases provide a basic component that catches expose events, mouse events, and other events. While the basic canvas does nothing with these events, you can extend canvas to create your own functioning canvases.

By creating a subclass of canvas, we can override the usual event handling methods in addition to the paint methods.

Canvases simplify the production of applets requiring unique functionality for several distinct areas.

Below we override the mouseDown() and paint() methods