Provision for GUIs in Java CSC 202
Evolution of GUI Capabilities in Java
Java 1.0 Abstract Window Toolkit (AWT) Called “Abominable” or “Awful Window Toolkit”. Done in 30 days. Last minute add-on. Java 1.1 Significantly improved, But GUI not finished.
Java 2 - Swing Much improved from AWT. Part of Java Foundation Classes (JFC). Very easy to add: Accelerators Tool Tips Graphics Pluggable look and feel.
AWT vs Swing AWT approach Swing approach Implement common elements of various operating environments. Swing approach Departure from AWT effort. Swing only needs to know how to write a pixel on a screen in a particular environment. AWT classes are extended in Swing. Example: AWT Button became Swing JButton.
The Operating System GUI Interface
Multitask operating system (Windows, UNIX, Linux, etc.) When the o.s. detects an event such as a mouse click, It determines what the event was; It determines in which application window it occurred.
Operating System Interface The operating system Passes event info as a message to the appropriate application, and Permits the app to take action. May interrupt the subsequent action if another event is detected.
Java Containment Hierarchy - 1 One and only one top-level container. May contain any number of intermediate-level and atomic components. Swing: almost always a Jframe (see Fig. 12.1, Table 12.2) See MyFrame.java (Listing 12.1) Note the method calls.
Containment Hierarchy - 2 Next important level: “content pane.” Internal component of the top-level container. All visible components other than menu bar go here. (See Listing 12.2). Lower levels (lightweight components) See Figure 12.1.
Creation of a Basic GUI Create a frame. Add components. Instantiate a frame object. Establish characteristics of the frame. Example: MyFrame.java (Listing 12.1) Add components. Example: MyFrameWithComponents.java (Listing 12.2)
Layout Managers Each top-level and intermediate container has a default layout manager. JFrame, JDialog, and JPanel – BorderLayout JWindow – FlowLayout Defines how components are sized and positioned within the container's content pane. (See pp. 411-417, 8th ed.; pp. 451-458, 9th ed.)
Creating a Swing-Based Window - Two Approaches: Construct a GUI object using Swing components from within a main() method without first creating a separate GUI class. Sometimes used to introduce Swing components and to create extremely simple GUIs. This is how our first example was done. (See FirstGUI.java) Construct the GUI as a separate class using Swing components. (See FirstWindow.java) In general this is preferred (adheres more closely to accepted OO practice).
Reading Chapter 12 – Introduction to GUIs Chapter 13 – Reference Chapter 16 – Read to understand how to handle event-driven programming.