0% found this document useful (0 votes)
19 views

Java Beans 2

Uploaded by

jindalankit2466
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)
19 views

Java Beans 2

Uploaded by

jindalankit2466
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/ 39

Common Elements of Graphical

User Interfaces
• 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.
• The below diagram provides the position of the graphical user
interface with respect to the computer system −
Elements in Graphical User Interface

• 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 around.In 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.
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.
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.
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.
Features of a GUI include:

• Much easier to use for beginners


• Enable an easy exchange of information between software using cut
and paste, or 'drag and drop'
• Use a lot of memory and processing power (can be slower than
command line interfaces operated by expert users)
• Can be irritating to experienced users when simple tasks require a
number of operations
Technology of GUI:
• To create the GUI,
• Java Foundation Classes/Swing (JFC/Swing) and
• Abstract Window Toolkit (AWT) AP
The Java foundation classes
• 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.
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.
Pluggable Look-and-Feel Support The look and feel of Swing applications is 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.
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 (DnD) Drag and Drop is one of the more common metaphors
used in 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
JFC
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.
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.
Difference Between JFC and WFC

WFC
JFC

It supports robust and It runs only on the Windows


portable interfaces. (32-bit) user interface that
require Microsoft extension.
It provides greater control The number of controls
over user interface because available for WFC is probably
of object-oriented nature. smaller than for JFC.
It requires more memory. It requires less memory.
Its performance is slower Its performance is faster than
than WFC. JFC.
It is used to build complex It is used to build simple user
user interface. interface.
Event Model in Java

The Delegation Event model is defined to handle events in GUI programming


languages. The GUI stands for Graphical User Interface, where a user
graphically/visually interacts with the system.

• The GUI programming is inherently event-driven; whenever a user initiates an


activity such as a mouse activity, clicks, scrolling, etc., each is known as an event
that is mapped to a code to respond to functionality to the user. This is known as
event handling.

• In this section, we will discuss event processing and how to implement the
delegation event model in Java. We will also discuss the different components of
an Event Model
• Basically, an Event Model is based on the following three
components:
• Events
• Events Sources
• Events Listeners
• Events
• The Events are the objects that define state change in a source. An
event can be generated as a reaction of a user while interacting with
GUI elements. Some of the event generation activities are moving the
mouse pointer, clicking on a button, pressing the keyboard key,
selecting an item from the list, and so on. We can also consider many
other user operations as events.
• The Events may also occur that may be not related to user interaction,
such as a timer expires, counter exceeded, system failures, or a task is
completed, etc. We can define events for any of the applied actions.
• Event Sources
• A source is an object that causes and generates an event. It generates
an event when the internal state of the object is changed. The sources
are allowed to generate several different types of events.
• Event Listeners
• An event listener is an object that is invoked when an event triggers.
The listeners require two things; first, it must be registered with a
source; however, it can be registered with several resources to receive
notification about the events. Second, it must implement the
methods to receive and process the received notifications.
Types of Events

• The events are categories into the following two categories:


• The Foreground Events:
• The foreground events are those events that require direct interaction of
the user. These types of events are generated as a result of user interaction
with the GUI component. For example, clicking on a button, mouse
movement, pressing a keyboard key, selecting an option from the list, etc.
• The Background Events :
• The Background events are those events that result from the interaction of
the end-user. For example, an Operating system interrupts system failure
(Hardware or Software)
• A source must register a listener to receive notifications for a specific
event. Each event contains its registration method. Below is an
example:public void addTypeListener (TypeListener e1)
• Some listeners allow only one listener to register. Below is an
example:
• public void addTypeListener(TypeListener e2) throws java.util.TooMan
yListenersException
// Java program to demonstrate the
// event handling within the class

import java.awt.*;
import java.awt.event.*;

class GFG extends Frame implements ActionListener {

TextField textField;

GFGTop()
{
// Component Creation
textField = new TextField();

// setBounds method is used to provide


// position and size of the component
textField.setBounds(60, 50, 180, 25);
Button button = new Button("click Here");
button.setBounds(100, 120, 80, 30);

// Registering component with listener


// this refers to current instance
button.addActionListener(this);
// add Components
add(textField);
add(button);

// set visibility
setVisible(true);
}

// implementing method of actionListener


public void actionPerformed(ActionEvent e)
{
// Setting text to field
textField.setText("GFG!");
}

public static void main(String[] args)


{
new GFGTop();
}
}
Event Class Listener Interface Description
An event that indicates that a component-defined action
ActionEvent ActionListener occurred like a button click or selecting an item from the
menu-item list.

The adjustment event is emitted by an Adjustable object


AdjustmentEvent AdjustmentListener
like Scrollbar.

An event that indicates that a component moved, the size


ComponentEvent ComponentListener
changed or changed its visibility.

ContainerEvent ContainerListener When a component is added to a container (or) removed


from it, then this event is generated by a container object.

These are focus-related events, which include focus,


FocusEvent FocusListener focusin, focusout, and blur.

An event that indicates whether an item was selected or


ItemEvent ItemListener
not.

An event that occurs due to a sequence of keypresses on


KeyEvent KeyListener
the keyboard.

The events that occur due to the user interaction with the
MouseEvent MouseListener & MouseMotionListener
mouse (Pointing Device).

An event that specifies that the mouse wheel was rotated


MouseWheelEvent MouseWheelListener
in a component.

TextEvent TextListener An event that occurs when an object’s text changes.

An event which indicates whether a window has changed


WindowEvent WindowListener
its status or not
Java Beans
• 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.
• A Java Bean is a software component. This component has been designed to be reusable in a variety of
different environments.
• .A Java Bean is a reusable software component that can be manipulated visually in a builder tool (Visual
Age, Delphi, Visual Basic, PowerBuilder etc.)Developers can use software components written by others
without having to understand their inner workings.
• To understand why software components are useful, think of a worker assembling a car. Instead of building
a radio from scratch, for example, he simply obtains a radio and hooks it up with the rest of the car.There is
no restriction on the capability of a Bean.
• It may perform a simple function, such as spelling check of a document, or a complex function such as
forecasting the performance of a stock portfolio.
• A Bean may be visible to an end user (ex. A button)
• A Bean may be invisible to a user (ex. Software to decode a stream of multimedia information in a real time)
• Beans are dynamic in such a way that they can be changed or customized.
• The classes that define the beans, referred to as JavaBeans components or simply components
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.
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
• package mypack;
• public class Employee implements java.io.Serializable{
• private int id;
• private String name;
• public Employee(){}
• public void setId(int id){this.id=id;}
• public int getId(){return id;}
• public void setName(String name){this.name=name;}
• public String getName(){return name;}
• }
Components of JavaBeans

• Components of Java Beams are nothing but the class that contains the
definition of beans. These classes have specific design convections. It
consists of events, methods, persistence, properties. Non-GUI based and
GUI based components are the two types of components. The classes are
discussed below:
• Properties: This attribute consists of a label, font, color, size, display size,
etc. It determines behavior, appearance, and the state of Beam.
• Methods: Methods in JavaBeans are similar to the normal Java methods. It
does have any specific naming convention.
• Events: Events in JavaBeans is similar to Swing event handling.
• Persistence: It helps in storing the state of JavaBean.
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
• e.setName("Arjun");//setting value to the object
• System.out.println(e.getName());
• }}
Beans Development Kit (BDK)

• The Beans Development Kit (BDK) is intended to support the early development of
JavaBeansTM components and to act as a standard reference base for both bean
developers and tool vendors. The BDK provides a reference bean container, the "BeanBox"
and a variety of reusable example source code (in the demo and beanbox subdirectories)
for use by both bean developers and tools developers.
• The BDK is not intended for use by application developers, nor is it intended to be a full-
fledged application development environment. Instead application developers should
check the various Java application development environments supporting JavaBeans.
• System Requirements
• This BDK 1.0 release requires you to have installed the Java Development Kit (JDK) 1.1. We
recommend using JDK 1.1.4 (or later) as this fixes some GUI related bugs.
• The BDK is qualified for Solaris 2.5, Windows 95, and Windows NT 4.0. However the BDK is
"pure Java" and should run on any JDK 1.1 enabled system.
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:
• 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.
Creating a Custom Event

• Creating a Custom Event

• import java.util.EventListener;
• import java.util.EventObject;

• import javax.swing.event.EventListenerList;

• class MyEvent extends EventObject {


• public MyEvent(Object source) {
• super(source);
• }
• }
• interface MyEventListener extends EventListener {
• public void myEventOccurred(MyEvent evt);
• }
• class MyClass {
• protected EventListenerList listenerList = new EventListenerList();
• public void addMyEventListener(MyEventListener listener) {
• listenerList.add(MyEventListener.class, listener);
• }
• public void removeMyEventListener(MyEventListener listener) {
• listenerList.remove(MyEventListener.class, listener);
• }
• void fireMyEvent(MyEvent evt) {
• Object[] listeners = listenerList.getListenerList();
• for (int i = 0; i < listeners.length; i = i+2) {
• if (listeners[i] == MyEventListener.class) {
• ((MyEventListener) listeners[i+1]).myEventOccurred(evt);
• }
• }
• }
• }
• public class Main {
• public static void main(String[] argv) throws Exception {
• MyClass c = new MyClass();
• c.addMyEventListener(new MyEventListener() {
• public void myEventOccurred(MyEvent evt) {
• System.out.println("fired");
• }
• });

• }
• }
SimpleBeanInfo
public class SimpleBeanInfo
extends Object

implements BeanInfo

• This is a support class to make it easier for people to


provide BeanInfo classes.
• It defaults to providing "noop" information, and can be
selectively overriden to provide more explicit information
on chosen topics. When the introspector sees the "noop"
values, it will apply low level introspection and design
patterns to automatically analyze the target bean.
Fields inherited from interface java.beans.BeanInfo
ICON_COLOR_16x16, ICON_COLOR_32x32,
ICON_MONO_16x16, ICON_MONO_32x32
Method Summary
BeanInfo[] getAdditionalBeanInfo()
Claim there are no other relevant BeanInfo objects.
BeanDescriptor getBeanDescriptor()
Deny knowledge about the class and customizer of the bean.
intgetDefaultEventIndex()
Deny knowledge of a default event.
intgetDefaultPropertyIndex()
Deny knowledge of a default property.
EventSetDescriptor[]
getEventSetDescriptors()
Deny knowledge of event sets.

ImagegetIcon(int iconKind)
Claim there are no icons available.
MethodDescriptor[]
getMethodDescriptors()
Deny knowledge of methods.
PropertyDescriptor[]
getPropertyDescriptors()
Deny knowledge of properties.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Method Detail

• getBeanDescriptor
• public BeanDescriptor getBeanDescriptor()
• Deny knowledge about the class and customizer of the bean. You can
override this if you wish to provide explicit info.
• Specified by:
• getBeanDescriptor in interface BeanInfo
• Returns:
• A BeanDescriptor providing overall information about the bean, such as
its displayName, its customizer, etc. May return null if the information
should be obtained by automatic analysis.
• getPropertyDescriptors
• public PropertyDescriptor[] getPropertyDescriptors()
• Deny knowledge of properties. You can override this if you wish to provide explicit property
info.
• Specified by:
getPropertyDescriptors in interface BeanInfo
• Returns:
• An array of PropertyDescriptors describing the editable properties supported by this bean.
May return null if the information should be obtained by automatic analysis.
• If a property is indexed, then its entry in the result array will belong to the
IndexedPropertyDescriptor subclass of PropertyDescriptor. A client of getPropertyDescriptors
can use "instanceof" to check if a given PropertyDescriptor is an IndexedPropertyDescriptor.
getDefaultPropertyIndex
public int getDefaultPropertyIndex()
Deny knowledge of a default property. You can override this if you wish to define a default property for the
bean.
Specified by:
getDefaultPropertyIndex in interface BeanInfo

Returns:
Index of default property in the PropertyDescriptor array returned by getPropertyDescriptors.
Returns -1 if there is no default property
getEventSetDescriptors
public EventSetDescriptor[] getEventSetDescriptors()
Deny knowledge of event sets. You can override this if you wish to provide explicit event set info.
Specified by:
getEventSetDescriptors in interface BeanInfo

Returns:
An array of EventSetDescriptors describing the kinds of events fired by this bean.
May return null if the information should be obtained by automatic analysis.

You might also like