GUI programming in JAVA
Designing Graphical User Interfaces in Java:
● These are the basic steps to create and deploy GUI
applications: Create forms. Forms can be created within
existing projects.
● To indent components:
○ Select the component you want to indent in the GUI
Builder workspace.
○ Select the component below the component below
which you want to indent it.
AWT (Abstract Window Toolkit)
● AWT (Abstract Window Toolkit) is a part of the Java Foundation Classes (JFC)
used to create GUI (Graphical User Interface) or window-based applications.
● Part of [Link] package.
● Provides GUI components like Button, Label, TextField, Checkbox, Choice, List,
Canvas, etc.
● Heavyweight components: Depend on the underlying OS for look and feel.
● Platform-dependent appearance: AWT apps look like Windows apps on Windows,
Mac apps on macOS, etc.
Types of Containers in Java AWT
There are four types of containers in Java AWT:
● Window: Window is a top-level container that represents a graphical window or
dialog box. The Window class extends the Container class, which means it can
contain other components, such as buttons, labels and text fields.
● Panel: Panel is a container class in Java. It is a lightweight container that can be used
for grouping other components within a window or a frame.
● Frame: The Frame is the container that contains the title bar and border and can have
menu bars.
● Dialog: A dialog box is a temporary window an application creates to retrieve user
input.
Programs
● Common AWT Components
● Label
● Button
● TextField
● Checkbox
● CheckboxGroup
● Choice
● List
● Canvas
● Scrollbar
● MenuItem & Menu
● PopupMenu
● Panel
● Toolkit
Label
Label in Java AWT is a component for placing text or images in a container. The
text displayed in the Container (Display) by Java AWT Label can't be edited
directly by the user but needs the programmer to change it from his side.
Java AWT Label Class Constructors
There are three types of Java AWT Label Class
1. Label():
Creates Empty Label.
2. Label(String str):
Constructs a Label with str as its name.
3. Label(String str, int x):
Constructs a label with the specified string and x as the specified alignment
Java AWT Button
The Button class is a control component in AWT, that triggers an event when it is
clicked.
● Button(): Creates a new button with the label as an empty string.
● Button(String label): Creates a new button with the label as the specified
string as a parameter.
// Java AWT Program to create
// Basic Button
import [Link];
import [Link];
// Driver Class
public class Main {
// main function
public static void main(String[] args)
{
// Create a frame
Frame frame = new Frame("AWT Button Example");
// Create a button
Button button = new Button("Click Me!");
// Set the button position on the frame
[Link](150, 130, 100, 30);
// Add the button to the frame
[Link](button);
// Set the frame size and layout
[Link](400, 300);
[Link](null);
// Set the frame visibility to true
[Link](true);
}
}
Java AWT | Canvas Class
Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the
user can draw or trap input from the user. Canvas class inherits the Component
class.
Constructor of the Canvas class are :
1. Canvas(): Creates a new blank canvas.
2. Canvas(GraphicsConfiguration c): Creates a new canvas with a specified
graphics configuration.
Java AWT Toolkit
The Abstract Window Toolkit (AWT) is a Java package that provides a
platform-indepеndеnt sеt of tools for creating graphical usеr intеrfacеs
(GUIs). AWT is part of thе Java Foundation Classеs (JFC), which also
includes Swing for morе advancеd GUI dеvеlopmеnt.
Layout Managers in Java AWT
Layout managers (FlowLayout, BorderLayout, GridLayout, CardLayout) control
component arrangement in containers.
● FlowLayout
● BorderLayout
● GridLayout
● CardLayout
Layout Programs
Event Handling Components in Java AWT
Event handlers such as ActionListener, MouseListener, ItemListener, KeyListener and WindowListener are
used to capture user actions and execute the corresponding response in GUI applications.
● ActionListener
● One of the most important components in AWT is the ActionListener interface. It is a key element
for adding interactivity in Java applications by handling user actions.
● Mouse and MouseMotion Listener
● ItemListener
● KeyListener
● WindowListener
Event handling programs
a simple ActionListener is implemented on a Button component.
// Set the positions
[Link](160, 100, 80, 40);
// Java Program to implement
// AWT ActionListener // Add button to the frame
[Link](b);
import [Link].*;
import [Link].*; // Set the background color of the button
[Link]([Link]);
// Driver Class
// Create a text field
public class Main { TextField tf = new TextField();
// main function
public static void main(String[] args){ // Set the positions
[Link](50, 50, 300, 30);
// Create a frame // Add text field to the frame
Frame f = new Frame("AWT ActionListener Example"); [Link](tf);
// Create a label
// Set the size Label lb = new Label();
[Link](400, 200);
// Set the positions
[Link](100, 150, 300, 30);
// Set the layout
[Link](null); // Add label to the frame
[Link](lb);
// Make the frame visible // Add an action listener to the button
[Link](true); [Link](new ActionListener() {
// Override the actionPerformed() method
// Set the background color of the frame public void actionPerformed(ActionEvent e){
[Link](Color.LIGHT_GRAY);
// Update the text of the label
[Link]("Hey " + [Link]() + "! "
// Create a button + "Welcome !");
Button b = new Button("Click Me"); } });}}
Introduction to Java Swing
● Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract
Window Toolkit [AWT].
● By today's application GUI requirements, AWT is a limited implementation, not quite
capable of providing the components required for developing complex GUIs required in
modern commercial applications.
● Includes New and improved Components that have been enhancing the looks and
Functionality of GUIs'
● Swing can be used to build (Develop) The Standalone swing GUI Apps as Servlets and
Applets
● A servlet is a Java programming language class used to extend the capabilities of a server.
● A Java applet is a small application which is written in Java and delivered to users in the form of
bytecode.
● It Employs model/view design architecture.
● Swing is more portable and more flexible than AWT, the Swing is built on top of
the AWT.
● Swing is Entirely written in Java.
● Java Swing Components are Platform-independent, and The Swing Components
are lightweight.
● Swing Supports a Pluggable look and feel and Swing provides more powerful
components.
Difference between Java Swing and Java AWT
The MVC Connection
● Over the years, one component architecture has proven itself to be
exceptionally effective: - Model-View-Controller or MVC for short.
● In MVC terminology, the model corresponds to the state information
associated with the Component.
● The view determines how the component is displayed on the screen,
including any aspects of the view that are affected by the current state of the
model.
● The controller determines how the component reacts to the user.
MVC Architecture
Swing API architecture follows loosely based MVC architecture in the following
manner.
● Model represents component's data.
● View represents visual representation of the component's data.
● Controller takes the input from the user on the view and reflects the
changes in Component's data.
● Swing component has Model as a seperate element, while the View and
Controller part are clubbed in the User Interface elements. Because of
which, Swing has a pluggable look-and-feel architecture.
The simplest Swing components have capabilities far beyond AWT components as follows:
● Swing buttons and labels can be displaying images instead of or in addition to
text.
● The borders around most Swing components can be changed easily. For example,
it is easy to put a 1-pixel border around the outside of a Swing label.
● Swing components do not have to be rectangular. Buttons, for example, can be
round.
● Now The Latest Assertive technologies such as screen readers can easily get
information from Swing components. Example: A screen reader tool can easily
capture the text that is displayed on a Swing button or label.
Components of Swing Class
Programs
A Java thread is the smallest unit of execution within a program.
It is a lightweight subprocess that runs independently but shares the
same memory space as the process, allowing multiple tasks to execute
concurrently.