Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
JavaBeans: Introspection and Customization Umer Farooq CS6704: Design Patterns & Component FrameworksJanuary 30, 2002.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
1 org.eclipse.ui.texteditor-api l Provides a framework for text editors obeying to the desktop rules. org.eclipse.ui.texteditor.
By Dr. Jiang B. Liu 12. The Java Beans. Java Beans n JavaBeans is a portable, platform-independent software component model written in Java. It enables.
JAVA BEANS By Madhuri Kakumanu. What is a Java Bean? “ A Java Bean is a reusable software component that can be visually manipulated in builder tools.”
Think about Lecture 11. When you write code Follow the Java conventions. JavaDocs should also include text written by YOU. Semantic information beside.
M.Sc. Course, Dept. of Informatics and Telecommunications, University of Athens S.Hadjiefthymiades “Web Application Servers” Basics on WAS WAS are necessary.
JAVA BEANS (Unit-V) K.Phani Sirisha.
Introducing JavaBeans Lesson 2A / Slide 1 of 30 JDBC and JavaBeans Pre-assessment Questions 1.Which of the given symbols is used as a placeholder for PreparedStatement.
Java Beans.  Java offers software component development through java Beans  Java Beans are based on a software component model for java.  The model.
Component-Based Software Engineering Introduction to Java Beans Paul Krause and Sotiris Moschoyiannis.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
Last update October 18, 2004 Advanced Programming 2004 Java Beans.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Components Components are specialized self contained Software entities that can be replicated, Customized and inserted into applications. Components come.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
1 Chapter 21 JavaBeans, Bean Events, and MVC Architecture.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM.
JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean.
Review of Previous Classes Declaring Variables - var myVar:DataType = value Data Types – Number, uint, String, Boolean Functions – parameters, return.
Creating a GUI Class An example of class design using inheritance and interfaces.
Introduction to Java Beans From Anders Børjesson.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Lorenz: Visitor Beans: An Aspect-Oriented Pattern Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Java Beans THETOPPERSWAY.COM. Contents What is a Java Bean? Advantages Properties Bean Developers Kit (BDK) What makes Bean possible? Support for Java.
Java Beans - Basics CIS 421 Web-based Java Programming.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Chapter 32 JavaBeans and Bean Events
Internet Made Easy! Make sure all your information is always up to date and instantly available to all your clients.
Chapter 36 JavaBeans and Bean Events
Javabeans for dummies.
JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine
Java Beans Sagun Dhakhwa.
Ellen Walker Hiram College
Introduction to Computing Using Java
Persistence & Bean Properties
Programming in Java Text Books :
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Component-Based Software Engineering
Java Programming: Advanced Topics
Presentation transcript:

Introduction to Java Beans by Anders Børjesson

Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many classes in J2SE are JavaBeans –Like the Java Swing classes

Introduction to JavaBeans3 Characteristics Visibility –Some JavaBeans are visual Buttons, TextFields, etc. –Some JavaBeans are “invisible” Model components (worker classes) Size (granularity) –Some JavaBeans are small (in terms of features) Buttons, TextFields, etc. –Some JavaBeans are big (in terms of features) SMTP bean, entire applications

Introduction to JavaBeans4 Requirements for JavaBeans classes Must have a no-arg (no parameters) constructor –More constructors are allowed –Remember: If you add a constructor with parameters then the default no-arg constructor is gone. Must implements java.io.Serializable Data should not be public –Data should be accesses via get and set methods Should be thread safe –Use synchronized carefully

Introduction to JavaBeans5 Non-requirements for JavaBeans An applet must extend java.lang.Applet A servlet must extend javax.servlet.Servlet A thread must implement java.lang.Runnable Beans does not need to extend or implement anything special –Except for java.io.Serializable Which is only a marker interface

Introduction to JavaBeans6 JavaBeans terms Properties –To be read or set by other objects Get and set methods Methods –To be called by other objects Ordinary Java methods Events –Sent to other objects when something happens to the bean object Like a property with a new value This is the interesting part!

Introduction to JavaBeans7 Access levels 3 access levels –Read-only Make only a get method –Write-only Make only a set method –Quite rare –Read / write Make get and set methods Most common

Introduction to JavaBeans8 Different kinds of properties –Simple or indexed (array) properties –Bound properties Event after a new value is set on the property To update other components (e.g. visual components) –Constrained properties Event before a new value is set on the property To check if the new value is legal according to the current business rules

Introduction to JavaBeans9 Simple properties Represents a single value A simple property has –1 name –1 type Primitive data typeor Class based data type Example: size –Name: sizeType: int –public void setSize(int size) –public int getSize() –AccountSimple.java

Introduction to JavaBeans10 Example bean: JButton Some properties of javax.swing.JButton –Text: setText(String), getText() –Font: setFont(Font), getFont() –Icon: setIcon(Icon), getIcon() –Enabled: setEnabled(boolean), isEnabled() Properties of type boolean has special naming conventions –setXx, isXx

Introduction to JavaBeans11 Programming JavaBeans JavaBeans can be programmed using –Ordinary text editors TextPad, even Notepad –Builder tools NetBeans, Eclipse, JBuilder, etc. Builder tools often have special features that makes programming JavaBeans easier.

Introduction to JavaBeans12 Exercise: Car bean You are supposed to program parts of a system for car registration –Program a Car bean Properties: Registration number, color, etc. Methods: toString, equals Constructors: No-arg constructor + maybe other constructors Implement Serializable Thread safety –Make a JUnit test to test the Car bean

Introduction to JavaBeans13 Indexed properties Represents an array of values Example: Property: address Type: Address –Public void setAddress(int index, Address addr) –Public Address getAddress(int index) –Bulk operations: Public void setAddresses(Address[] addr) Public Address[] getAddresses() –AccountIndexedOwner.java

Introduction to JavaBeans14 Exercise: Owner bean Additional classes for the car registration system –Program a (car) owner bean Simple properties: Name, address, etc. Indexed property: Car –One owner may have several cars –The type of this property is Car (from the previous exercise), not String. –Make a JUnit test to test the Owner and Car beans.

Introduction to JavaBeans15 Bound properties Sends an event to another object after the property is assigned a new value. –The other object is sometimes called: Observer, listener, etc. The other object may react on the event –Update its own state Content of an event –propertyName, oldValue, newValue, source

Introduction to JavaBeans16 Examples AccountBound.java ScreenLogListener.java AccountBoundApp.java

Introduction to JavaBeans17 Observer pattern

Introduction to JavaBeans18 Sequence diagram: Firing a property change event

Introduction to JavaBeans19 Sequence diagram: Adding a property change listener to a bean

Introduction to JavaBeans20 Classes and interfaces Package java.beans –Class PropertyChangeEvent extends EventObject –Interface PropetyChangeListener Extends EventListener –An empty (aka. Marker) interface »Like java.io.Serializable –Class PropertyChangeSupport Helps you to make bound properties

Introduction to JavaBeans21 PropertyChangeListener Interface PropertyChangeListener extends EventListener –EventListener is a marker interface No methods to implement PropertyChangeListener methods –void propertyChange(PropertyChangeEvent evt)PropertyChangeEvent

Introduction to JavaBeans22 PropertyChangeEvent After changing the value of a property –firePropertyChange(propertyChangeName, oldValue, newValue) Listener called –propertyChange(PropertyChangeEvent ev) PropertyChangeEvent methods: –String getPropertyName() –Object getOldValue() –Object getNewValue() –Object getSource()

Introduction to JavaBeans23 PropertyChangeSupport Helps you to make bound properties Constructor –PropertyChangeSupport(Object sourceBean) Some methods –addPropertyChangeListener(PropertyChangeListener listener) –removePropertyChangeListener(PropertyChangeListener listener) –addPropertyChangeListener(String propertyName, PropertyChangeListener listener) –removePropertyChangeListener(String propertyName, PropertyChangeListener listener) –firePropertyChange(String propertyName, Object oldValue, Object newValue) –firePropertyChange(String propertyName, int oldValue, int newValue)

Introduction to JavaBeans24 Exercises: Car bean with bound properties Program a car bean with bound properties –Don’t try to change the Car from the previous exercise. Make a new class with a different name. Program a PropertyChangeListener –LogListener implements PropertyChangeListener –Every time a property changes the listener writes to the screen the data in the propertyChangeEvent Source, property name, old value, new value. Make a Main to try the new Car bean with a propertyChangeListener. Make a JUnit test to test the bound properties. –Take a look at the test in my BeanBank example before you try to make your own test. Extra (if you have time): Make a more general log listener that writes to a general stream.

Introduction to JavaBeans25 Event loops Problem –2 beans send propertyChangeEvents to each other Example: User interface objects events a model object, and vice versa –The 2 beans might event each other forever. Solution –Check if the new value is really a new value i.e. newValue != oldValue PropertyChangeSupport.firePropertyChange() does the checking

Introduction to JavaBeans26 Example bean: JButton Javax.swing.JButton is a bean –Does not use PropertyChangeListener, etc. –addActionListener(ActionListener l) Listens for button presses –addChangeListener(ChangeListener l) Listens for changes in the button’s state –Size, font, text, etc.

Introduction to JavaBeans27 Constrained properties A bean object asks one (or more) observers if an update of a property is legal –Observer may throw PropertyVetoException –Example: account.setBalance(-100) –A property is often bound and constrained Used to implement pluggable business rules Examples –AccountConstrained.java –MinimumBalanceChecker.java –MaximumWithdrawChecker.java –AccountConstrainedTest.java –AccountFrame.java + AccountGuiApp.java

Introduction to JavaBeans28 Exercise: Car with constrained properties Program a car bean with constrained properties –You may either change your “old” car bean or make a new car bean. Program a few VetoableChangeListeners –ColorChecker Checks if a color is illegal –Pink, orange, etc. are considered illegal colors of a car –RegistrationNumberChecker Checks if the registration number is legal –A proper registration number must have the structure AB12345, i.e. 2 letter followed by 5 digits. Make a JUnit test to test the Car bean with the new listeners. Extra (if you have time) –Program a configurable ColorChecker as a bean + VetoableChangeListener Illegal colors is an indexed property

Introduction to JavaBeans29 Dynamic vs. static characteristics on properties Static characteristic –Follows the object for its life time. Dynamic characteristic –Changes over time during the objects life time –Characteristics may be (de)aggregated using constrained properties. –Example: AccountAdminFrame.java

Introduction to JavaBeans30 Low coupling The event mechanism in JavaBeans provides low coupling –The bean object doesn’t know much about the listeners Except that listeners must implement PropertyChangeListener or PropertyVetoListener –The listeners doesn’t know much about the bean It’s just an Object –Which we sometimes have to typecast to the Bean class (which is very bad)

Introduction to JavaBeans31 Using JavaBeans in builder tools JavaBeans can be used in a builder tool like –NetBeans –Eclipse –Borland JBuilder –etc. The bean must be “installed” in the builder –The builder usually helps you to during installation

Introduction to JavaBeans32 Packing JavaBeans in JAR files JAR = Java Archive –The standard package format in Java –JAR = ZIP file with a manifest –A JAR file may contain Class files Icons, etc. Manifest file describing the contents of the JAR file

Introduction to JavaBeans33 Introspection The builder tool unzip’s the JAR file to get the class files. The builder tool “learns” about the beans’ properties, methods, and events using the introspection API –Package java.beans Class Introspector Interface BeanInfo Class PropertyDescriptor Class MethodDescriptor Class EventSetDescriptor –Example: beanbank/introspection/IntrospectionApp.java

Introduction to JavaBeans34 Using JavaBeans JavaBeans can be used as ordinary classes in –Java applications, applets, servlets –JSF(JavaServer Faces)

Introduction to JavaBeans35 JavaBeans vs. Enterprise JavaBeans JavaBeans –Are relatively simple –Part of Java Standard Edition (J2SE) –Can be used in an ordinary Java application / applet Enterprise JavaBeans –More advanced –Part of Java Enterprise Edition (J2EE) –Middle tier: Between (web)application and database. –Can only be used in an enterprise Java application Requires an J2EE server to run the application

Introduction to JavaBeans36 Job types using JavaBeans and other types of components Designing components Programming components Documenting components –Technical writer Design using components Programming using components Marketing / selling components Testing components –Correctness, performance, security [Trojan horses, etc.]

Introduction to JavaBeans37 References Niemeyer & Knudsen Learning Java, 3 rd edition, O’Reilly 2005 –22. JavaBeans, page