pre-assessment questions

26
Working with JavaBean Properties and Events Lesson 2B / Slide 1 of 26 JDBC and JavaBeans Pre-assessment Questions 1. You need to create an application that validates the username and password of various employees in a company using the entries in the employee database. The application validates the username and password every time the employee makes a request for the database records, reports, or company resources. Select the type of JavaBean that should be used to create such an application. a. Control JavaBean b. Container JavaBean c. Invisible runtime JavaBean d. Hidden JavaBean

Upload: haru

Post on 24-Jan-2016

16 views

Category:

Documents


0 download

DESCRIPTION

Pre-assessment Questions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 1 of 26JDBC and JavaBeans

Pre-assessment Questions1. You need to create an application that validates the username and password

of various employees in a company using the entries in the employee database. The application validates the username and password every time the employee makes a request for the database records, reports, or company resources. Select the type of JavaBean that should be used to create such an application.a. Control JavaBeanb. Container JavaBeanc. Invisible runtime JavaBeand. Hidden JavaBean

Page 2: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 2 of 26JDBC and JavaBeans

Pre-assessment Questions (Contd.)2. Which of the file enables you to start the BDK in the Windows environment?

a. run.bat

b. run.sh

c. bdkrun.bat

d. bdkrun.sh

3. What is the name of the BDK window that lists the pre-defined events for a sample JavaBean?a. EventListenerb. EventTargetDialog c. actionPerformedDialogd. Properties

Page 3: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 3 of 26JDBC and JavaBeans

Pre-assessment Questions (Contd.)4. The compressed file for a JavaBean application are stored as:

a. Manifest fileb. Event filec. JAR filed. BeanInfo file

5. How can you modify the public properties of a JavaBean in the BDK environment?a. Using property editorsb. Using property sheetsc. Using properties palleted. Using BeanBox window

Page 4: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 4 of 26JDBC and JavaBeans

Solutions to Pre-assessment Questions

1. a. Control JavaBean2. a. run.bat3. b. EventTargetDialog4. c. JAR file5. b. Using property sheets

Page 5: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 5 of 26JDBC and JavaBeans

Objectives

In this lesson, you will learn about:

• Different types of JavaBean properties

• Creating custom events

• Using JavaBeans in applications

Page 6: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 6 of 26JDBC and JavaBeans

JavaBean Properties• JavaBean properties:

• Are the private data members of the JavaBean classes.

• Are used to accept input from an end user in order to customize a JavaBean.

• Can retrieve and specify the values of various attributes, which determine the behavior of a JavaBean.

Page 7: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 7 of 26JDBC and JavaBeans

JavaBean Properties (Contd.)• Types of JavaBeans Properties

• Simple properties

• Boolean properties

• Indexed properties

• Bound properties

• Constrained properties

Page 8: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 8 of 26JDBC and JavaBeans

JavaBean Properties (Contd.)• Simple Properties:

• Refer to the private variables of a JavaBean that can have only a single value.

• Are retrieved and specified using the get and set methods respectively.

• The syntax of get method is:public return_type get<PropertyName>()

• The syntax of set method is:public void set<PropertyName>(data_type value)

Page 9: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 9 of 26JDBC and JavaBeans

JavaBean Properties (Contd.)• Boolean Properties:

• Have either of the two values, TRUE or FALSE.

• Are retrieved and specified using the following methods: • public boolean is<PropertyName>() • public boolean get<PropertyName>() • public void set<PropertyName>(boolean value)

Page 10: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 10 of 26JDBC and JavaBeans

JavaBean Properties (Contd.)• Indexed Properties:

• Are indexed under a single name, with each index being an integer value.

• Enable you to set or retrieve the values from an array of property values.

• Are retrieved using the following get methods:• public int[] get<PropertyName>()

• public property_datatype get<PropertyName>(int index)

• Are specified using the following set methods:• public void set<PropertyName>(int index,

property_datatype value) • public void set<PropertyName>(property_datatype[]

property_array)

Page 11: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 11 of 26JDBC and JavaBeans

JavaBean Properties (Contd.)• Bound Properties:

• Are the properties of a JavaBean that inform its listeners about changes in its values.

• Are implemented using the PropertyChangeSupport class and its methods.

• Are always registered with an external event listener.

Page 12: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 12 of 26JDBC and JavaBeans

JavaBean Properties (Contd.)• Constrained Properties:

• Are the properties that are protected from being changed by other JavaBeans.

• Are implemented using the VetoableChangeSupport class.

• Are registered with an external event listener that has the ability to either accept or reject the change in the value of a constrained property.

• Can be retrieved using the get method. The prototype of the get method is:public string get<ConstrainedPropertyName>()

• Can be specified using the set method. The prototype of the set method is:public string set<ConstrainedPropertyName>(String str)throws PropertyVetoException

Page 13: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 13 of 26JDBC and JavaBeans

Handling Events in JavaBeans• 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.

• 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.

Page 14: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 14 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)

• 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.

Page 15: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 15 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)

• Creating the Event Class

• The event class that defines the custom event extends the EventObject class of the java.util package. For example, public class NumberEvent extends EventObject

{

public int number1,number2;

public NumberEvent(Object o,int number1,int number2)

{

super(o);

this.number1=number1;

this.number2=number2;

}

}

Page 16: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 16 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)

• 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 java.util.EventListener interface.

• The target application that uses the custom event implements the custom listener. For example,public interface NumberEnteredListener extends EventListener

{

public void arithmeticPerformed(NumberEvent mec);

}

Page 17: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 17 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)

• 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.

Page 18: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 18 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)• The code snippet to define an event handler for the custom event

NumberEvent is:public class NumberBean extends JPanel implements

ActionListener

{

public NumberBean()

{}

NumberEnteredListener mel;

public void addNumberListener(NumberEnteredListener mel)

{

this.mel = mel;

}

Page 19: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 19 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)/* The following method is used to notify all listeners about the completion of data entry */

NumberEvent mec;

public void fireNumberEvent(NumberEvent mec)

{

mel.arithmeticPerformed(mec);

}

public void actionPerformed(ActionEvent ae)

{

..

..

}

}

Page 20: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 20 of 26JDBC and JavaBeans

Handling Events in JavaBeans (Contd.)

• Using Custom Event in a Target Application

• A target application, which uses the JavaBean that fires the custom event, must first register itself as a listener of the event.

• A target application must define the code to specify what has to be done when the custom event is fired.

• For example, a sample target application that uses the NumberEvent custom event accepts two numbers as input from an end user, fires the custom event, and displays the result of adding two numbers.

Page 21: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 21 of 26JDBC and JavaBeans

Demonstration-Creating User-Defined JavaBean

• Problem Statement

• A JavaBean has to be created to accept the login information from a customer. The JavaBean should accept the login name and the password. The login information will be used by Earnest Bank for several purposes, such as to store the login details in the database of Customer Call Center and ATM Centers of the bank. You need to create the login JavaBean that does the following:

Page 22: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 22 of 26JDBC and JavaBeans

Demonstration-Creating User-Defined JavaBean (Contd.)

• Provides a user-interface to accept the login name and password.

Page 23: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 23 of 26JDBC and JavaBeans

Demonstration-Creating User-Defined JavaBean (Contd.)

• Creates an event object that stores the login information and fires an event every time a user logs on.

• You should verify the JavaBean functionality by creating a simple target application that checks that the name and password are not same.

Page 24: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 24 of 26JDBC and JavaBeans

Demonstration-Creating User-Defined JavaBean (Contd.)• Solution

• To create the login JavaBean that uses the custom event to check that the login name and password are not same, the following tasks need to be performed:1. Creating an event class2. Creating an event listener3. Creating an event handler4. Using the custom event in a target Java application5. Verifying the execution of custom event

Page 25: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 25 of 26JDBC and JavaBeans

SummaryIn this lesson, you learned:

• The properties of a JavaBean are the private data members of the JavaBean class.

• Properties are used to define the behavior of JavaBean components.

• The properties of a JavaBean that can have only one value at a time are called simple properties.

• The boolean properties of a JavaBean can have one of the two values, true or false.

• The JavaBean properties that are indexed under a single name are called indexed properties. The index in the property array of an indexed property is an integer value.

• The bound properties of a JavaBean are registered with an external event listener.

Page 26: Pre-assessment Questions

Working with JavaBean Properties and Events

Lesson 2B / Slide 26 of 26JDBC and JavaBeans

Summary (Contd.)• The constrained properties of a JavaBean are also registered with an

external listener. The external event listener for the constrained property can revert the changes in the value of JavaBean property, if the change is invalid.

• The event delegation model of Java is used to handle custom events.

• The event source generates an event.

• The event listener interfaces are notified, when an event is generated.

• The event classes define the various events that can be generated by the event source.

• The steps to create a custom event and implement it in a target application are:

• Create an event class.

• Create an event listener interface.

• Create an event handler JavaBean to process the event.

• Create a target application to implement the custom event. `