Download presentation
Presentation is loading. Please wait.
Published byGwenda Sims Modified over 9 years ago
1
Introduction to Java Beans by Anders Børjesson
2
Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM / ActiveX are components in the Microsoft environment Many classes in J2SE are JavaBeans –Like the Java Swing classes
3
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 beans, entire applications
4
Introduction to JavaBeans4 Requirements for JavaBeans 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 no be public –Data should be accesses via get and set methods Should be thread safe –Use synchronized carefully
5
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
6
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 A property with a new value
7
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
8
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
9
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
10
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()
11
Introduction to JavaBeans11 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 –Make a JUnit test to test the Car bean
12
Introduction to JavaBeans12 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
13
Introduction to JavaBeans13 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.
14
Introduction to JavaBeans14 Exercise: Owner bean Additional classes for the car registration system –Program a (car) owner bean Properties: Name, address, etc. Indexed property: Cars –One owner may have several cars –Make a JUnit test to test the Owner and Car beans.
15
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
16
Introduction to JavaBeans16 Examples AccountBound.java ScreenLogListener.java AccountBoundApp.java
17
Introduction to JavaBeans17 Observer pattern
18
Introduction to JavaBeans18 Sequence diagram: Firing a property change event
19
Introduction to JavaBeans19 Sequence diagram: Adding a property change listener to a bean
20
Introduction to JavaBeans20 Classes and interfaces Package java.beans –Class PropertyChangeEvent extends EventObject –Interface PropetyChangeListener Extends EventListener –Class PropertyChangeSupport Helps you to make bound properties
21
Introduction to JavaBeans21 PropertyChangeListener Interface PropertyChangeListener extends EventListener –EventListener is a marker interface No methods to implement PropertyChangeListener methods –void propertyChange(PropertyChangeEvent evt)PropertyChangeEvent
22
Introduction to JavaBeans22 PropertyChangeEvent Af 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()
23
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)
24
Introduction to JavaBeans24 Exercises: Car bean with bound properties Program a car bean with bound properties –You may either change your “old” car bean or make a new car bean. 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 JUnit test to test the bound properties. Extra (if you have time): Make a more general log listener that writes to a general stream.
25
Introduction to JavaBeans25 Event loops Problem –2 beans sends propertyChangeEvents to each other Example: User interface objects events a model objec, 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
26
Introduction to JavaBeans26 Example bean: JButton Javax.swing.JButton is a bean –Doesn’t use ProperChangeEvent, etc. –addActionListener(ActionListener l) Listens for button presses –addChangeListener(ChangeListener l) Listens for changes in the button’s state –Size, font, text, etc.
27
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 Examples –AccountConstrained.java –MinimumBalanceChecker.java –MaximumWithdrawChecker.java –AccountConstrainedTest.java –AccountFrame.java + AccountGuiApp.java
28
Introduction to JavaBeans28 Exercise: Car with constrained properties Program a car bean with bound 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
29
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.
30
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)
31
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
32
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
33
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 Interface PropertyDescriptor Interface MethodDescriptor Interface EventSetDescriptor –Example: IntrospectionApp.java
34
Introduction to JavaBeans34 Using JavaBeans JavaBeans can be used as ordinary classes in –Java applications, applets, servlets –JSP (JavaServer Pages) special tags to manipulated JavaBeans –
35
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
36
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.]
37
Introduction to JavaBeans37 References Niemeyer & Knudsen Learning Java, 3 rd edition, O’Reilly 2005 –22. JavaBeans, page 751-786
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.