Java Beans 2
Java Beans 2
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:
WFC
JFC
• 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
import java.awt.*;
import java.awt.event.*;
TextField textField;
GFGTop()
{
// Component Creation
textField = new TextField();
// set visibility
setVisible(true);
}
The events that occur due to the user interaction with the
MouseEvent MouseListener & MouseMotionListener
mouse (Pointing Device).
• 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?
• 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
• import java.util.EventListener;
• import java.util.EventObject;
• import javax.swing.event.EventListenerList;
• }
• }
SimpleBeanInfo
public class SimpleBeanInfo
extends Object
implements BeanInfo
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.