0% found this document useful (0 votes)
18 views23 pages

Week 14

Uploaded by

sofyanbabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Week 14

Uploaded by

sofyanbabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to

GUI JAVA

Instructor Name: Mr. Abrar Ahmad


Department of Computer Science, HITEC University Taxila - Pakistan
Outline
2

 OOP with Java


 Programming Graphical User Interface (GUI)
 Sets of Java APIs for Graphics Programming
 AWT Introduction
 JavaFX Introduction
 Swing Introduction
 Swing Features
 Swing vs AWT
 Swing Components
 Types of Containers

OOP with Java
3

• As discussed, OOP permits higher level of abstraction than


traditional Procedural-Oriented languages (such as C and Pascal).

• You can create high-level abstract data types called classes to mimic
real-life things.

• Classes are self-contained and are reusable.


Programming Graphical User Interface (GUI)
4

• We can reuse the graphics classes provided in JDK for constructing


your own Graphical User Interface (GUI) applications.

• Writing your own graphics classes (and re-inventing the wheels) is


mission impossible!

• Built-in graphics classes or APIs, developed by expert programmers,


are highly complex and involve many advanced design patterns.

• Re-using them is not so difficult, if you follow the API


documentation, samples and templates.

• Just require a good knowledge of OOP, including inheritance,


polymorphism, abstract class and interface.
Sets of Java APIs for Graphics Programming
5

There are current three sets of Java APIs for graphics programming: AWT (Abstract
Windowing Toolkit), Swing and JavaFX.

1. AWT API was introduced in JDK 1.0. Most of the AWT components have become
obsolete and should be replaced by newer Swing components.

2. Swing API, a much more comprehensive set of graphics libraries that enhances the
AWT, was introduced as part of Java Foundation Classes (JFC) after the release of JDK
1.1. JFC has been integrated into core Java since JDK 1.2.

3. The latest JavaFX, which was integrated into JDK 8, is meant to replace Swing.
AWT Introduction
6

• AWT components are platform-dependent


• Java AWT uses the native GUI components of the operating system.
• It calls the platform (Operating systems) subroutine for creating components
such as textbox, checkbox, button etc.
• For example an AWT GUI having a button would have a different look and feel
across platforms like Windows, Mac OS & Unix,
• In simple terms, an application built on AWT would look like a Windows
application when it runs on Windows, but the same application would look
like a Mac application running on Mac OS.

• Heavy weight

• Its components are using the resources of OS.


AWT Introduction
7
AWT Introduction
8

 AWT Class Hierarchy


 Component: The base class for all AWT components.
 Container:

 Containers are components that can hold other components, including other
containers.
 An abstract subclass of Component that can contain other components.
 It provides methods that its subclasses use to manage the components they contain.

 Window: A top-level container with no borders or window decorations. (inherited


from Container class)
 Frame(concrete class): A subclass of Window with a title, border, and window
decorations.
 Panel: A generic container used for grouping components
9
AWT Introduction
11

import java.awt.*;
public class AwtExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT Example"); // Frame is a top-level window
Panel panel = new Panel(); // Panel is a simple container
Button button = new Button("Click Me"); // Button is a component

panel.add(button); // Adding the button to the panel (panel acts as a container)


frame.add(panel); // Adding the panel to the frame (frame acts as a container)
frame.setSize(300, 200); // Setting the size of the frame
frame.setVisible(true); // Making the frame visible
}
JavaFX Introduction
12

• JavaFX is a Java library used to develop Desktop applications as well as Rich


Internet Applications (RIA).

• The applications built in JavaFX, can run on multiple platforms including Web,
Mobile phones, TV, and Desktops.

• Intended to replace swing in Java applications as a GUI framework.

• Provides more functionalities than swing, lightweight.

• Like swing, provides its own components and doesn't depend upon the operating
system.

• Supports various operating systems including Windows, Linux and Mac OS.
Swing Introduction
13

Swing is part of the "Java Foundation Classes (JFC)"

The Java Foundation Classes (JFC) are a set of GUI components


which simplify the development of desktop applications.
JFC consists of:
• AWT: (Already Discussed)
• Swing API: A GUI toolkit that provides a set of "lightweight" (platform-
independent) components for advanced graphical programming.

• Accessibility API: provides assistive technology for the disabled.


• With the Java Accessibility API, developers can easily create Java applications
that are accessible to persons with disabilities.
• Accessible Java applications are compatible with assistive technologies such as
screen readers, speech recognition systems, etc.
Swing Introduction
14

• Platform Independence

• Pure Java Implementation: Swing components are written entirely in


Java.
• Do not rely on the native GUI components of the operating system,
which makes them platform-independent.
• i.e., a Swing application will behave consistently across different
platforms (Windows, macOS, Linux, etc.) as long as the Java Runtime
Environment (JRE) is installed.
Swing Introduction
15

• Look and Feel (L&F): By default, Swing components use a "Java Look and Feel"
(also known as "Metal"), which is platform-independent.

• Swing supports a pluggable look and feel architecture, which allows it to


mimic the appearance of native applications. For example, Swing can use the
"Windows Look and Feel" on Windows, the "GTK+ Look and Feel" on Linux,
and so on.

• When you choose a platform-specific look and feel, Swing components will
look similar to native components of that platform.

• However, this does not mean that they are using native widgets; they are
still Java implementations that are styled to look like the native ones.
Swing Introduction
16

• Swing provides platform specific Look and Feel and also an option for pluggable
Look and Feel, allowing application to have Look and Feel independent of
underlying platform.

“Look” refers to the appearance of GUI widgets and “feel” refers to the way
the widgets behave.
A widget is an element of a
• Java 2D API: for high quality 2D graphics and images. graphical user interface (GUI)
that displays information or
provides a specific way for a
• Drag-and-drop support between Java and native applications. user to interact with the
operating system or an
Widget also means the small program that is written in order to application.
describe what a particular widget looks like, how it behaves and
how it responds to user actions.
Swing’s Features
17

• Swing is huge (consists of 18 packages of 737 classes as in JDK 1.8) and has great
depth.

• Compared with AWT, Swing provides a huge and comprehensive collection of


reusable GUI components.

• Swing is written in pure Java (except a few classes) and therefore is 100%
portable.

• Swing components are lightweight.

• Swing components support pluggable look-and-feel.

• Swing supports mouse-less operation and many more…


AWT vs Swing
18
Swing Components
19

The Object class is the parent class of all the


classes in java by default. In other words, it is A container is a component which can contain other
the topmost class of java. components inside itself. It is also an instance of a
subclass of java.awt.Container.
java.awt.Container extends java.awt.Component so
containers are themselves components. A container is
used to
A container can also hold hold components.
containers because it is a
The Component class is the abstract
(subclass of) component.
superclass of the nonmenu-related
Abstract Window Toolkit components
Swing Components
20

In this diagram, the commonly used container


components are in orange while the non-container
components are in blue. Note that technically, all
javax.swing components are capable of holding other
Components, but in practice, only JFrame, JApplet, and
JPanel, plus a few not shown (JSplitPane, JTabbedPane
and JScrollPane) are commonly used for that purpose.
Types of Containers / Container Components
21

There are three container types - frames, dialogs and applets –

But applets are no longer used and most browsers no longer support them.

A frame on the other hand is a complete window. When you A dialog is a specialized sort of window intended to hold
create a frame you get a fully resizable movable windows nothing but components that the user can interact with. A
separate from the browser’s window. You can add other dialog appears, the user clicks buttons, enters text and then
components to a frame and you can add a menu. In all it goes away. This is generally referred to as a "modal"
respects a frame is very much a fully fledged Window’s dialog box but dialogs don't have to be modal.
window.

We also have the panel and a range of variations based on it


Containers and Non-Containers Components
22

How work with three top level containers in Swing.

• JFrame class: used for the application's main window (with an icon, a title,
minimize/maximize/close buttons, an optional menu-bar, and a content-pane),
as illustrated.
Containers
• JDialog class: used for secondary pop-up window (with a title, a close
button, and a content-pane).

• Japplet class: used for the applet's display-area (content-pane) inside a


browser’s window.
Containers and Non-Containers Components
23

• JComponent: The JComponent class is the base class of all Swing components
except top-level containers. Swing components whose names begin with "J" are
Non-Containers descendants of the JComponent class.

For example, JButton, JLabel, JTable etc.

But, JFrame and JDialog don't inherit JComponent class because they are
the child of top-level containers.
References
24

• https://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
• https://www.clear.rice.edu/comp310/JavaResources/GUI/
• https://www.guru99.com/java-swing-gui.html
• https://www.i-programmer.info/ebooks/134-modern-java/5041-building-a-java-gui-c
ontainers.html
• http://www.cafeaulait.org/course/week8/36.html
• https://examples.javacodegeeks.com/desktop-java/swing/java-swing-tutorial-beginn
ers/
• https://www.javatpoint.com/javafx-tutorial
• https://www.javatpoint.com/java-swing
• https://www.javatpoint.com/java-awt

You might also like