AWT and Swing Notes
Abstract Windowing Toolkit (AWT) vs. Swing
Similarities
Both AWT and Swing are tools provided by Java for developing interactive GUI applications.
Both provide GUI components for creating Java applications and applets.
They are part of Java Foundation Classes (JFCs), which are an important part of the Java SDK.
JFC simplifies Java GUI application development and consists of five APIs:
AWT
Swing
Java2D
Accessibility
Drag and Drop
Differences
AWT (Abstract Windowing Toolkit)
Some AWT components use native code.
AWT is platform-dependent.
AWT ensures that the look and feel of an application is comparable across different machines.
Swing
Swing is written entirely in Java.
Swing is platform-independent.
It ensures applications deployed across different platforms have the same appearance.
Swing is built around APIs that implement various parts of AWT and can be used with AWT.
AWT GUI Components: Fundamental Window Classes
GUI components like buttons and text fields are placed in containers.
Window Class Methods
Setting the size of the window:
void setSize(int width, int height)void setSize(Dimension d)Dimension dhas width and height as fields.
Setting visibility:
void setVisible(boolean b)If
bis true, the window becomes visible.
Frame objects are commonly used in designing GUI applications.
AWT GUI Components: Graphics
Graphics Class (Abstract) Methods:
drawLine()drawPolyline()setColor()fillRect()drawPolygon()getFont()drawRect()fillPolygon()setFont()clearRect()getColor()drawString()
Color Class Constructors:
Color(int r, int g, int b)Integer values range from 0 to 255.
Color(float r, float g, float b)Float values range from 0.0 to 1.0.
Color(int rgbValue)Value range from 0 to (black to white).
Red: bits 16-23
Green: bits 8-15
Blue: bits 0-7
Graphics Example:
Example code provided demonstrates how to set background color, font, and draw strings and rectangles using the Graphics class.
More AWT Components
AWT controls are subclasses of the Component class that allow users to interact with a GUI application.
Example: Demonstrates adding buttons, labels, text fields, checkboxes, lists, choice menus, and scrollbars to a frame.
Layout Managers
Definition:
Layout managers determine the position and size of components within a container.
They govern the layout of components in the container.
Common Layout Managers in Java:
FlowLayoutBorderLayoutGridLayoutGridBagLayoutCardLayout
Methods
Setting the layout manager:
void setLayout(LayoutManager mgr)Passing
nullmeans no layout manager is in use.
If no layout manager is used, elements must be positioned manually using:
public void setBounds(int x, int y, int width, int height)This method is from the Component class.
Manual positioning can be tedious with multiple components.
The FlowLayout Manager
It is the default manager for the Panel class and its subclasses (e.g., Applet class).
Positions components left-to-right, top-to-bottom, starting at the upper-left corner.
FlowLayout Constructors:
FlowLayout()Creates a new FlowLayout object with center alignment and a 5-unit horizontal and vertical gap by default.
FlowLayout(int align)Creates a new FlowLayout object with the specified alignment and a default 5-unit horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap)Creates a new FlowLayout object with the specified alignment,
hgap-unit horizontal gap, andvgap-unit vertical gap.
Gap
Gap is the spacing between components, measured in pixels.
Possible Alignment Values:
FlowLayout.LEFTFlowLayout.CENTERFlowLayout.RIGHT
The BorderLayout Manager
It is the default layout for Window objects and their subclasses (Frame and Dialog).
Divides the Container object into five regions:
North (stretch horizontally)
South (stretch horizontally)
East (adjust vertically)
West (adjust vertically)
Center (adjust in both directions)
Parameters
hgapandvgaprefer to spacing between components within the container.
Adding Components to a Specified Region:
Use the
addmethod with two arguments:Component to add
Region where the component is positioned
Only one component can be placed in each region.
Valid Regions:
BorderLayout.NORTHBorderLayout.SOUTHBorderLayout.EASTBorderLayout.WESTBorderLayout.CENTER
The GridLayout Manager
Positions components left-to-right, top-to-bottom, starting at the upper-left corner (like FlowLayout).
Divides the container into rows and columns with equally sized regions.
Ignores the component's preferred size.
GridLayout Constructors:
GridLayout()Creates a new GridLayout object with a single row and column by default.
GridLayout(int rows, int cols)Creates a new GridLayout object with the specified number of rows and columns.
GridLayout(int rows, int cols, int hgap, int vgap)Creates a new GridLayout object with the specified number of rows and columns, and
hgap-unit horizontal andvgap-unit vertical spacings.
Panels and Complex Layouts
For complex layouts, you can combine different layout managers and use panels.
A Panel is both a Container and a Component.
Components can be inserted into a Panel.
Panels can be added to a Container.
Swing GUI Components
The package is
javax.swing.Written entirely in Java, providing the same look and feel across different platforms.
Offer more advanced components like Color Choosers and Option Panes.
Naming Convention
Swing GUI component names are similar to AWT, but prefixed with
J.AWT:
ButtonclassSwing:
JButtonclass
Swing Components:
JComponent: The root class for all Swing components, excluding top-level containers.
JButton: A "push" button. Corresponds to the Button class in the AWT package.
JCheckBox: An item that can be selected or deselected by the user. Corresponds to the Checkbox class in the AWT package.
JFileChooser: Allows the user to select a file. Corresponds to the FileChooser class in the AWT package.
JTextField: Allows for editing a single-line text. Corresponds to the TextField class in the AWT package.
JFrame: Extends and corresponds to the Frame class in the AWT package but the two are slightly incompatible in terms of adding components to this container. Need to get the current content pane before adding a component.
JPanel: Extends JComponent. A simple container class but not top-level. Corresponds to Panel class in the AWT package.
JApplet: Extends and corresponds to the Applet class in the AWT package. Also slightly incompatible with the Applet class in terms of adding components to this container.
JOptionPane: Extends JComponent. Provides an easy way of displaying pop-up dialog boxes.
JDialog: Extends and corresponds to the Dialog class in the AWT package. Usually used to inform the user of something or prompt the user for an input.
JColorChooser: Extends JComponent. Allows the user to select a color.
Swing: Setting Up Top-Level Containers
Top-level containers in Swing are slightly incompatible with those in AWT in terms of adding components.
Adding a Component to the Container:
Get the content pane of the container using the
getContentPanemethod.Add components to the content pane using the
addmethod.
JFrame Example
The
java.awtpackage is still imported because the layout managers in use are defined in this package.Giving a title to the frame and packing the components within the frame is applicable for AWT frames, too.
Coding Convention:
Declare components as fields.
A
launchFramemethod is defined for initialization and component addition.No longer just extend the Frame class, which allows for organized and easier event handling code addition.