Intro to GUI Libraries in Java
Graphics Libraries
- In the original version of Java, graphics components were in the AWT
library (Abstract Windows Toolkit)
- Was okay for developing simple GUI applications
- For different platforms, AWT components mapped to platform-specific
components
- Prone to platform specific bugs
- Primary package:
java.awt. See other awt package APIs on the java.sun
site
- When Java 2 was released, a library known as the Swing
components were introduced with the idea of replacing the older AWT
user-interface components (like Button, TextField,
TextArea, etc).
- less dependent on target platform
- a more robust and flexible library
- Primary package:
javax.swing. See other swing package APIs on the java.sun
site
- Since release of Java 2, the Swing components are recommended for
building graphic user interfaces for later browsers. Keep in mind
that earlier browser versions may not be able to handle these. For
now, the AWT components should still work in applets in most
browsers, as well.
Components and Containers
Components
- A graphics component is an object having a graphical
representation that can be displayed on screen and that can interact with
the user
- Some examples of components:
- button
- check box
- text field
- combo box
-
class Component: A class that serves as a basis for all
non-menu graphic user interface classes
-
class JComponent: This class is the basis for all
Swing components (except for the top level containers)
Containers
- A container is a GUI component that can contain other
components
- Note that a container is a component itself
- But more specifically, it can be used to hold other components in a
specific area
- Examples of GUI containers:
- window
- panel
- tabbed pane
- scroll pane
-
class Container: This class library forms a basis for
containers
- A top level container is a special container that is intended
to be the outermost container for some GUI context. In Java, there are
three top level containers:
- Frame -- the outer container for an application window
- Applet -- the outer container for a Java applet
- Dialog -- the outer container for a dialog box
- In the Swing libraries, these three top-level containers are in these
three classes:
- One very useful general purpose container:
JPanel
- A panel is an invisible container that can be used for storing
components. It can be nested in other containers, and it can also be
used as an easy drawing canvas
Events
- Event: A signal that something has happened in a program.
Examples: Button clicks, mouse movements, menu selections
- GUI programs generally driven by events, rather than a specific
procedural order
- Events are handled with event objects. These are triggered by
actions on source objects (components or objects on which the event is
generated), and they must implement corresponding event listener
interfaces. The listener listens for the event, and invokes an event
handler when event occurs
-
java.util.EventObject: Base class for event classes in Java
Some examples of event types:
- ActionEvent - clicking a button, pressing return on a text field
- ItemEvent - clicking a check box, selecting an item
- WindowEvent - Closing a window, opening a window
- ContainerEvent - component added to a container
- ComponentEvent - resizing a component, hiding a component
- TextEvent - changing a text value
- MouseEvent - clicking the mouse, dragging the mouse
- KeyEvent - pressing a key on the keyboard
These are just a few examples, not a comprehensive list.