0% found this document useful (0 votes)
17 views54 pages

CO2 Java Beans

The document provides an overview of Advanced Java Programming, specifically focusing on Java Beans and Graphical User Interfaces (GUIs). It discusses the elements of GUIs, their advantages over command-line interfaces, and the technology behind GUIs, including event-driven programming and various UI frameworks. Additionally, it covers Java Foundation Classes (JFC) and its components, emphasizing the importance of Swing in building GUI applications in Java.

Uploaded by

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

CO2 Java Beans

The document provides an overview of Advanced Java Programming, specifically focusing on Java Beans and Graphical User Interfaces (GUIs). It discusses the elements of GUIs, their advantages over command-line interfaces, and the technology behind GUIs, including event-driven programming and various UI frameworks. Additionally, it covers Java Foundation Classes (JFC) and its components, emphasizing the importance of Swing in building GUI applications in Java.

Uploaded by

Shashwat Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advanced Java Programming

Module 2

Java Beans
1
Outline

● Graphical User Interface (GUI)


● JFC
● JavaBeans
● Handling Events in JavaBeans

2
Graphical User Interface (GUI)

[Link]
ser-interface-gui

[Link]
ystem
3
Graphical User Interface (GUI)

•GUI is an interface that allows users to interact with


different electronic devices using icons and other
visual indicators. The graphical user interfaces were
created because command line interfaces were quite
complicated and it was difficult to learn all the
commands in it.
•In today’s times, graphical user interfaces are used
in many devices such as mobiles, MP3 players,
gaming devices, smartphones etc.
4
Graphical User Interface (GUI)
•The below diagram provides the position of
the graphical user interface with respect to the
computer system −

5
Elements in Graphical User Interface
•Graphical User Interface makes use of visual elements
mostly. These elements define the appearance of the GUI.
Some of these are described in detail as follows −
Window
•This is the element that displays the information on the
screen. It is very easy to manipulate a window. It can be
opened or closed with the click of an icon. Moreover, it can
be moved to any area by dragging it [Link] a multitasking
environment, multiple windows can be open at the same
time, all of them performing different tasks.
•There are multiple types of windows in a graphical user
interface, such as container window, browser window, text
terminal window, child window, message window etc.
6
Elements in Graphical User Interface
Menu
•A menu contains a list a choices and it allows users to select one
from them. A menu bar is displayed horizontally across the screen
such as pull down menu. When any option is clicked in this menu,
then the pull down menu appears.
•Another type of menu is the context menu that appears only when
the user performs a specific action. An example of this is pressing
the right mouse button. When this is done, a menu will appear under
the cursor.
Icons
•Files, programs, web pages etc. can be represented using a small
picture in a graphical user interface. This picture is known as an
icon. Using an icon is a fast way to open documents, run programs
etc. because clicking on them yields instant access. 7
Elements in Graphical User Interface
Controls
•Information in an application can be directly read or influences using
the graphical control elements. These are also known as widgets.
Normally, widgets are used to display lists of similar items, navigate
the system using links, tabs etc. and manipulating data using check
boxes, radio boxes etc.
Tabs
•A tab is associated with a view pane. It usually contains a text label
or a graphical icon. Tabs are sometimes related to widgets and
multiple tabs allow users to switch between different widgets. Tabs
are used in various web browsers such as Internet Explorer, Firefox,
Opera, Safari etc. Multiple web pages can be opened in a web
browser and users can switch between them using tabs.
8
GUI Operating System
•An operating system is software that provides an interface between the
user and the hardware. The computer cannot perform any task without an
operating system. The operating system controls and manages the
operation of computer hardware.
•The evolution of operating systems had made PCs easier to use and
understand, flexible and reliable. Some popular, modern graphical user
interfaces include Microsoft Windows, macOS, Ubuntu Unity, and
GNOME Shell for desktop environments, Android, Apple's iOS,
BlackBerry OS, Windows 10 Mobile, Palm OS-WebOS, and Firefox OS
for smartphones.
•Modern operating systems and graphical user interfaces are incorporated
into every interactive application, such as ATMs, video games,
smartphones, self-service checkouts, airline self-ticketing and check-in,
and desktops.
•Some operating systems are graphical user interfaces (GUI), and some are
9
command-line interfaces (CLI).
What is GUI OS?
•GUI stands for Graphical User Interface. It is a visual representation
of communication presented to the user for easy interaction with the
machine. The actions in a GUI are usually performed through direct
manipulation of graphical elements like buttons and icons.
Communication can be performed by interacting with these icons
rather than the usual text-based or command-based communication.

10
What is GUI OS?
•There are several different visual programming languages with their
unique advantages for developing a graphical user interface design.
C # or Java may be preferable due to their ability to run GUIs
simultaneously in a browser and as a desktop application. Other
languages are also be prefered, such as Python, HTML5/Javascript,
and C/C++.
•There are different elements and objects that the user use to interact
with the software to make a user friendly GUI.
•Button: A graphical representation of a button that acts as a program
when pressed.
•Dialogue box: A type of window that displays additional
information and asks a user for input.
•Icon: Small graphical representation of a program, features, or file.
11
What is GUI OS?
•Menu: List of commands or choices offered to the user through the
menu bar.
•Ribbon: Replacement for the file menu and toolbar that groups
programs activities together.
•Tab: Clickable area at the top of a window that shows another page
or area.
•Toolbar: Row of buttons, often near the top of an application
window, that controls software functions.
•Window: Rectangular section of the computer's display that shows
the program currently being used.

12
Types of GUI based Operating System
•Sketchpad, believed to be the first graphical computer-aided design
program, was developed in 1962 by Ivan Sutherland at MIT and
consisted of a light pen that enabled users to create and manipulate
objects in engineering drawings in real time with coordinated
graphics. The different types of the operating system based on GUI
are as follows:

13
Types of GUI based Operating System
1. Microsoft Windows
•Microsoft Windows is one of the most common GUI based operating
systems. It is developed and marketed by Microsoft. The current
version of Microsoft Windows is Windows 10. Moreover, it has
several earlier versions like Windows XP, Windows 8, Windows 7,
etc. Besides, the most common editions for home computers are
Windows Home (Win Home) and Windows Professional (Win Pro or
Windows Pro).
2. Linux
•Linux is a popular operating system. It is not a part of any particular
firm or organization. Rather, it was developed by a
programmer Linus Torvalds in 1991. It is an open-source operating
system. Today, LINUX is run in many organizations, private offices,
on mobiles, supercomputers, over the internet, etc. 14
Types of GUI based Operating System
•It already contains almost all the features of UNIX OS. Moreover, it also
has some additional features. It contains various user interfaces
like GNOME, KDE, Mate, Cinnamon, etc. And it has several versions of
distributions as follows UBUNTU, DEBIAN, SOLAS, LINUX MINT, etc.
3. Android
•It is one of the most common versions of the operating system today. It is
used on mobile phones and tablets. Most versions of it are the open-source
operating system.
4. Apple iOS
•After android, it is one of the most popular OS. It is designed to run on
Apple devices such as iPhones, iPad tablets, etc.
•5. MacOS
•It is the operating system used in Apple laptops and Apple desktops.
Moreover, Its most popular features are Siri and FaceTime.
15
How Graphical User Interface Works?
•Graphical user interface design principles follow the
model-view-controller software pattern, which separates internal
representations of information from how information is presented to
the user, resulting in a platform where users are shown which
functions are possible rather than requiring the input of command
codes.

16
How Graphical User Interface Works?
•Users interact with information by manipulating visual widgets, designed to
respond by the type of data they hold and support the actions necessary to
complete the user's task.
•The appearance of an operating system or application software may be
redesign due to the nature of graphical user interfaces independent of
application functions.
•Applications typically implement their own unique graphical user interface
display elements and already present on the existing operating system.
•A typical graphical user interface also includes standard formats for
representing graphics and text, sharing data between applications running
under common graphical user interface design software.
•Graphical user interface testing refers to the systematic process of generating
test cases to evaluate the system's functionality and design elements.
•Graphical user interface testing tools (manual or automated) are available
under various licenses and supported by various platforms. 17
Advantages of Graphical User Interfaces
•Graphical user interfaces operating system has the following most
common advantages:
•The advantage of a graphical user interface is a stark improvement in
usability for the average user.
•The features of a graphical user interface familiar leverage metaphors,
such as drag-and-drop for transferring files, and use familiar icons,
such as a trash bin for deleted files, creating an environment in which
computer operations are intuitive and easy to use without any prior
practise or knowledge of computing machinery or languages.
•Graphical user interface applications are self-descriptive, feedback is
typically immediate, and visual cues encourage and steer
discoverability.

18
Difference between GUI and CUI Operating System
•The interface can be understood as the medium we communicate with
each other. In the same way, to communicate with the computer, we
have interfaces. There are two types of interface one is the graphical
user interface, and the other one is the command-line interface.

19
Difference between GUI and CUI Operating System
•A command user interface (CUI) processes commands to a computer
program in the form of lines of text. The program which handles the
interface is called a command-line interpreter or command-line
processor. Operating systems implement a command-line interface in a
shell for interactive access to operating system functions or services.
•The graphical user interface (GUI) allows users to interact with
electronic devices through graphical icons and audio indicators such as
primary notation instead of text-based user interfaces, typed command
labels, or text navigation. Below are some more differences between
GUI and CUI Operating System, such as:

20
Difference between GUI and CUI Operating System
Terms GUI CUI
Interaction A user interacts with the A user interacts with a
computer using Graphics like computer using commands
images, icons. like text.
Navigation Navigation is easy. Navigation is difficult.
Precision GUI has low precision. CUI has high precision.
Usage GUI is easy to use. CUI is difficult to use and
requires expertise.
Speed GUI is of low speed. CUI is of high speed.
Peripherals used GUI operating system CUI operating system
requires a keyboard, mouse or requires the only keyboard.
any other pointing device.
Memory requirement It required high memory. It required low memory.
Flexibility Highly flexible user interface. Less flexible user interface.

Customize GUI is highly customizable. CUI appearance is not easily


21
changeable.
Technology of GUI
•Event-Driven Programming: GUIs are typically built using
event-driven programming, where the flow of the program is
determined by user actions (events) like mouse clicks, key presses, or
screen touches.

•UI Frameworks and Libraries:


•Desktop GUIs: Frameworks like Qt, GTK, and Windows Presentation
Foundation (WPF) are used to create cross-platform desktop
applications. They provide tools and widgets to build and manage
windows, buttons, text fields, and more.

22
Technology of GUI

•Web GUIs: Technologies like HTML, CSS, and JavaScript are


used to create web-based GUIs. Libraries and frameworks like
React, Angular, and [Link] enhance the development of dynamic
and responsive user interfaces.
•Mobile GUIs: For mobile applications, platforms like iOS and
Android offer their own UI frameworks (UIKit for iOS, Jetpack
Compose for Android) to build interfaces optimized for touch
screens.

23
Technology of GUI
•Rendering Engines: GUI elements are rendered on the screen
using rendering engines. For web GUIs, browsers use engines
like WebKit, Blink, or Gecko to interpret and display HTML,
CSS, and JavaScript. Desktop GUIs use native OS rendering or
frameworks' rendering engines.
• Graphics Libraries: Libraries like OpenGL, DirectX, and

Vulkan are used to render complex graphics, animations, and


visual effects in GUIs, especially in games and multimedia
applications.

24
Technology of GUI

•Object-Oriented Programming (OOP): GUIs often use OOP


principles, where elements like windows, buttons, and menus
are treated as objects with properties and behaviors. This
approach allows for better organization and reusability of code.
•Cross-Platform Development: Tools like Electron and JavaFX
allow developers to create GUIs that can run on multiple
operating systems with a single codebase, providing consistent
user experiences across platforms.

25
Technology of GUI
•Accessibility Tools: Modern GUI technologies include
accessibility features like screen readers, keyboard navigation,
and voice control to make applications usable by people with
disabilities.

26
Java Foundation Classes (JFC)

[Link]

27
JFC Java
• In 1997, Sun Microsystems and IBM decided to solve the problem of
access-enabling software.
• Their aim was to develop such an accessibility API that application
developers could implement on Java class libraries to make
applications accessible.
• As the result, Sun Microsystems wrote the accessibility API and
IBM has tested their API and implemented it on their foundation
classes.
• Sun Microsystems integrated JFC/ Swing directly into Java 1.2 and
later versions.
• In this, discuss the JFC, its components, services, and the
difference between JFC and AWT.

28
JFC Java

JFC (Java Foundation Classes) is a part of the Java Development Kit


(JDK) that provides libraries for building graphical user interfaces
(GUIs) in Java.
The most common part of JFC is Swing, which is used to create
window-based applications.

29
What is JFC?
• JFC stands for Java Foundation Classes.
• It is a rich and comprehensive set of GUI components and services
that simplify the development and deployment of desktop,
client-side, and internet applications.
• It is a superset that contains AWT. JFC extends AWT by adding
many components and services that are as follows:

API/ Feature Description


Swing GUI Components It includes everything from buttons to split panes to
tables. Many components are capable of sorting,
printing, and drag and drop, to name a few of the
supported features.

30
What is JFC?
API/ Feature Description
Pluggable Look-and-Feel The look and feel of Swing applications is
Support pluggable, allowing a choice of look and feel. For
example, the same program can use either the
Java or the Windows look and feel. Additionally,
the Java platform supports the GTK + look and
feel, which makes hundreds of existing looks and
feels available to Swing programs. Many more
look-and-feel packages are available from various
sources.
Accessibility API It is a part of JFC that works with alternate input
and output devices. It enables assistive
technologies, such as screen readers, screen
magnifiers, and Braille terminals, to get
information from the UI.

31
What is JFC?
API/ Feature Description
Java 2D API It enables developers to easily incorporate high-quality 2D
graphics, text, and images in applications and applets. Java 2D
includes extensive APIs for generating and sending
high-quality output to printing devices.
Internationalization It allows developers to build applications that can interact with
users worldwide in their own languages and cultural
conventions. With the input method framework, developers
can build applications that accept text in languages that use
thousands of different characters, such as Japanese, Chinese,
or Korean.
Drag and Drop Drag and Drop is one of the more common metaphors used in
(DnD) GUI. The user is allowed to click and "hold" a GUI object,
moving it to another window or frame in the desktop with
predictable results. It allows users to implement droppable
elements that transfer information between Java applications
and native applications. Although DnD is not part of Swing,
32 it
is crucial to a commercial-quality application.
What is JFC?
• Sun Microsystems added a mechanism to the JFC is called
Multiplexed UI. It is an extension to the PLAF (Pluggable Look &
Feel). A key feature of the PLAF is that it enables developers to
swap the Look and Feel of the UI which can be directly accessible. It
was developed for application developers to provide a common
interface.

The Multiplexed UI mechanism allows the developer to:

• Add multiple alternative UI to the JFC component library while


JVM starts.
• Makes multi-modal access (such as hands-free voice access) features
possible with a smaller amount of development effort.
33
JFC architecture
• The following figure describes the JFC architecture.

• In the above figure, we see that Swing sits atop of part, but not all, of
the AWT components. Swing sits on a number of the APIs that
implement the various parts of AWT, including Java2D,
Drag-and-Drop, and the Accessibility API. 34
Advantage of JFC
• Its components are pluggable and require few lines
of code.
• It retains Java qualities.
• An application that runs flawlessly on one OS runs
flawlessly on another OS.
• It offers an open architecture.
• Accordingly, the exhibition of a GUI made through
JFC is unsurprising. Therefore, JFC are widely
used in client-side applications.
35
Components of JFC
• The JFC components includes all
the JavaBeans and swing Components that offer
re-usability, interoperability, and portability.
• Note that Swing is an enhancement of AWT, not a
replacement.

36
Difference Between JFC and WFC
JFC WFC
It supports robust and portable It runs only on the Windows
interfaces. (32-bit) user interface that
require Microsoft extension.
It provides greater control over The number of controls available
user interface because of for WFC is probably smaller than
object-oriented nature. for JFC.
It requires more memory. It requires less memory.
Its performance is slower than Its performance is faster than
WFC. JFC.
It is used to build complex user It is used to build simple user
interface. interface. 37
JFC Event Model
JFC follows the Model-View-Controller (MVC) design pattern, which
separates the data model, user interface, and control logic into three
interconnected components:
Model: Represents the data and business logic of the component.
For instance, a TableModel in a JTable holds the data displayed in the
table.
View: The visual representation of the component. In Swing, the view is
often automatically handled by the component itself, rendering the data as
specified by the model.
Controller: Manages user input and interaction, updating the model and
view accordingly. In Swing, event listeners act as controllers, responding
to user actions like clicks and key presses.

38
JavaBean

[Link]

39
Introduction

•A JavaBean is a Java class that should follow


the following conventions:
–It should have a no-arg constructor.
–It should be Serializable.
–It should provide methods to set and get the
values of the properties, known as getter and
setter methods.

40
Why use JavaBean?

•According to Java white paper, it is a reusable


software component.
•A bean encapsulates many objects into one
object so that we can access this object from
multiple places.
•Moreover, it provides easy maintenance.

41
Simple example of JavaBean class

//[Link]

package mypack;
public class Employee implements [Link]{
private int id;
private String name;
public Employee(){}
public void setId(int id){[Link]=id;}
public int getId(){return id;}
public void setName(String name){[Link]=name;}
public String getName(){return name;}
}
42
How to access the JavaBean class?
•To access the JavaBean class, we should use getter and setter methods.

package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
[Link]("Arjun");//setting value to the object
[Link]([Link]());
}}

•Note: There are two ways to provide values to the object.


One way is by constructor and second is by setter method.
43
JavaBean Properties

• A JavaBean property is a named feature that


can be accessed by the user of the object. The
feature can be of any Java data type, containing
the classes that you define.
• A JavaBean property may be read, write,
read-only, or write-only. JavaBean features are
accessed through two methods in the
JavaBean's implementation class:

44
JavaBean Properties

1. getPropertyName ()
For example, if the property name is firstName,
the method name would be getFirstName() to read
that property. This method is called the accessor.
2. setPropertyName ()
For example, if the property name is firstName,
the method name would be setFirstName() to
write that property. This method is called the
mutator.
45
Advantages of JavaBean
•The following are the advantages of JavaBean:/p>
– The JavaBean properties and methods can be exposed to
another application.
– It provides an easiness to reuse the software components.

Disadvantages of JavaBean
The following are the disadvantages of JavaBean:
– JavaBeans are mutable. So, it can't take advantages of
immutable objects.
– Creating the setter and getter method for each property
separately may lead to the boilerplate code.

46
Handling Events in JavaBeans

47
Introduction

• Enables Beans to communicate and connect together.


Beans generate events and these events can be sent to other
objects. Event means any activity that interrupts the current
ongoing activity.
• Example: mouse clicks, pressing key…
• User-defined JavaBeans interact with the help of
user-defined events, which are also called custom events.
You can use the Java event delegation model to handle
these custom events.

48
Introduction
• The components of the event delegation model are:

✓ Event Source: Generates the event and informs all the


event listeners that are registered with it
✓ Event Listener: Receives the notification, when an
event source generates an event
✓ Event Object: Represents the various types of events
that can be generated by the event sources.

49
Creating Custom Events
• The classes and interfaces that you need to define to create
the custom JavaBean events are:
– An event class to define a custom JavaBean event.
– An event listener interface for the custom JavaBean
event.
– An event handler to process the custom JavaBean
event.
• A target Java application that implements the custom event.

50
Creating the Event Class
• The event class that defines the custom event extends the
EventObject class of the [Link] package.
• Example
public class NumberEvent extends EventObject
{
publicint number1,number2;
publicNumberEvent(Object o,int number1,int number2)
{
super(o); this.number1=number1;
this.number2=number2;
}}
• Beans can generate events and send them to other objects. 51
Creating Event Listeners
• When the event source triggers an event, it sends a notification to
the event listener interface.
• The event listener interface implements the [Link]
interface.
– Syntax
– public void addTListener(TListenereventListener);
– public void addTListener(TListenereventListener)throws
TooManyListeners;
– public void removeTListener(TListenereventListener);
• The target application that uses the custom event implements the
custom listener.
• Example
• public interface NumberEnteredListener extends EventListener{
• public void arithmeticPerformed(NumberEventmec); } 52
Creating Event Handler
• Custom event handlers should define the following methods:
– addXXListener(): Registers listeners of a JavaBean event.
– fireXX(): Notifies the listeners of the occurrence of a
JavaBean event.
– removeXXListener(): Removes a listener from the list of
registered listeners of a JavaBean.
• The code snippet to define an event handler for the custom event
NumberEvent
public class NumberBean extends JPanel implements ActionListener{
publicNumberBean(){}
NumberEnteredListenermel;
public void addNumberListener(NumberEnteredListenermel){
[Link] = mel; } }
53
END

54

You might also like