*Graphical User Interfaces* Flashcards
Components
Components present data to users and/or allow them to interact with the application. Examples include buttons, labels, text fields, check boxes and radio buttons.
Java Foundational Classes
These are used to create graphical user interfaces (GUI). The two primary sets of such classes are the AWT and Swing.
Abstract Windowing Toolkit (AWT)
The first set of classes for drawing graphics and creating GUI in Java. Programmers are limited in what they can do with the AWT because they do not actually draw components on the screen. Instead, they communicate with another layer of software, known as the peer classes, which directs the underlying operating system to draw its own built-in components. This means that applications built with Java have a look that’s consistent with other applications on the same operating system, but behavior can vary slightly. In addition, in order for Java to maintain its portability, AWT is limited to offering only those components that every operating system supports.
Swing
A third limitation with AWT is that programmers cannot easily customize components. Hence, Swing was introduced with the release of Java 2. Swing is a library of classes that do not replace AWT, but provide an improved alternative for creating GUI applications and applets. Very few Swing classes rely on peer classes. Instead, Swing draws components on the screen. This means that Swing components can have a consistent look and predictable behavior on any operating system.
To use the Swing classes, you need to use the following import statement:
import javax.swing.*;
Heavyweight components
Components that are coupled with their underlying peer classes, such as AWT components
*Note, components that are not coupled with peer classes are referred to as lightweight components
Event-driven programming
Programs that operate in a GUI environment must be event-driven. That is, they create and respond to various events that user perform such as clicking a button.
Event listener
An object that automatically executes one of its methods when a specific event occurs.
Frame
A frame is a basic window that has a border around it, a title bar, and a set of buttons for minimizing, maximizing, and closing the window. In a Swing application, you create a frame from the JFrame class.
How do you set a frame’s size?
With the setSize method
How do you display a frame on the screen?
With the setVisible method
Using inheritance to extend the JFrame class
While you can create an instance of the JFrame class and display it, it's much more common to use inheritance to create a new class that extends the JFrame class. For example: public class CustomWindow extends JFrame
When you do this the new class inherits methods of the JFrame class such as setTitle, setSize, setDefaultCloseOperation, and setVisible.
Content panes and panels
A content pane is a container that is part of every JFrame object. You cannot see the content pane and it does not have a border, but any component that is to be displayed in a JFrame must be added to it content pane.
A panel is also a container that holds GUI components but unlike content panes, they cannot be displayed by themselves; they must be added to content panes, since they are used to hold and organize collections of related components.
Event object
When an event takes place in a GUI, the component responsible for the event (the event source) creates an event object in memory. The event object contains information about the event.
Event listener
An event listener is an object that responds to events. If the event source component is connected to an event listener then the event object is automatically passed as an argument to a specific method in the event listener. Then the method performs the appropriate action in response to the event. Note that an event source can be linked to more than one event listener.
*Note that when you’re building a GUI application, you need to write the classes for the event listeners that your application needs.
Event firing
The process of an event object being created by a source component and responded to by an event listener.
If you’re writing an event listener class for a JButton component, what interface must the class implement? What method must the class have and when is it executed?
The class must implement the ActionListener interface, and it must have a method named actionPerformed. The method is executed when the user clicks the button.
What is a common technique for writing an event listener class?
To write it as a private inner class, inside the class that creates the GUI
What is one requirement of all event listeners?
They must implement an interface. The specific interface used depends on the type of event the listener needs to handle. For example, action events require an action listener that implements the ActionListener interface.
Registering an event listener object
The process of connecting an event listener object to a GUI component. For example:
// Adds an action listener to the button
calcButton.addActionListener(new CalcButtonListener());