Download presentation
Presentation is loading. Please wait.
Published byLesley Dalton Modified over 9 years ago
1
CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering & Information Sciences
2
2 References Several slides in this lecture have made use of material with modifications provided by Mark Davidson: The Bean Builder Tutorial. Deitel, Deitel & Santry: Advanced Java 2 Platform: How To Program 2002, Chapter 6 JavaBeans Component Model
3
3 Outline Introduction Making a Bean class Jar files Bound Properties Custom events Bean Builder GUI and usage
4
4 Introduction: JavaBeans (beans) Java’s reusable software component model rapid application development by assembling predefined software components i.e. reuse builder tools/IDEs: support beans applications/applets or even new beans! “component assembler” _ connecting the dots!
5
5 The JavaBeans Component model –aims to make classes reusable components –does this by means of standard structure and interface –such a class may then be used by many common application builder tools it can be added to the "palette" of a tool bar it can be used as a component of a Java Server Page via the tag etc –a Bean class can be made to trigger or "fire" events –examples: all the swing components JButton, JCheckbox, JScrollbar, JTextField, JTextArea,... are JavaBeans Introduction
6
6 In an IDE such as Sun's Forte or the more recent SunONE Studio or the free Bean Builder product from Sun, –beans can be added to a palette along with other graphical components, controls, etc and –their events are tied to event handlers by means of "wizards" –the code is "generated". Environment
7
7 Example Consider a component assembler (CA) that has an animation bean bean has methods startAnimation() stopAnimation() Say, CA requires Beans can simply ‘connect’ the buttons to the methods! Builder tool does all the work (event-handling etc) Programmer: just needs to cover logistics! Start AnimationStop Animation
8
8 Benefits The ‘Animation bean’ and the ‘button beans’ do not need to know each other before assembly Button action is well known in Java CA uses the pre-existing button component to provide operation to the user Beans interact via defined services/methods www.java.sun.com/beans/
9
9 So what is it about a class that makes it a bean? –The protected (or private) data, are a subset of them, make up the bean properties – For each bean property Type prop there is a Type getProp() method and –(Unless the property is read-only) a method void setProp(Type x) –An indexed property is a data member which is an array of some data type. –In this case there are two get methods A parameterless Type[] getProp () returns (a reference to) the array A method Type getProp(int i) returns the i th entry in the array –A bean class implements Serializable … so that it can be stored A simple example... Making a Bean Class
10
10 public class Student implements Serializable { protected String name; protected Date dob; protected boolean fullTime; protected int[] marks; //an indexed property public Student(String i, String n, Date d) { //constructor //.... } public String getName() { return name; } public void setName(String n) { name = n; } public Date getDob() { return dob; } public void setDob(Date d) { dob = d; } public boolean isFullTime() { return fullTime; } public void setFullTime(boolean b) { fullTime = b; } public int[] getMarks() { return marks; } public int getMarks(int i) { return marks[i]; } public void setMarks(int[] m) { marks = m; } public void setMarks(int i, int m) { marks[i] = m; } } Example
11
11 the class LogoAnimator listed with these slides is another example: this time, a graphical JavaBean … a swing component public class LogoAnimator extends JPanel... class LogoAnimator implements the LogoAnimator JavaBean LogoAnimator is a JavaBean containing an animated logo. examine this class LogoAnimator extends JPanel implements interface serializable the class animationTimer (a Timer object) provides get and set methods for the animation time delay. Another Example
12
12 JAR Stands for Java ARchive A Jar is a kind of "zipped" set of Java classes and supporting files To create LogoAnimator.jar –jar cvf LogoAnimator.jar LogoAnimator.class To create myJar.jar containing all classes &.dat files in current directory –jar cvf myjar.jar *.class *.dat –The file specifications can also contain absolute or relative directory paths "v" is verbose option To see contents of a jar –jar tf myjar.jar To extract a file or files from a jar –jar xf myjar.jar file-spec To update a jar –jar uvf myjar.jar file-spec To get a reminder of the main command-line options –jar See ref http:java.sun.com/docs/books/tutorial/jar/index.html Jars
13
13 A jar may contain a manifest in directory META-INF as file MANIFEST.MF –You do not need one if you are simply archiving a whole lot of files in compressed format. –You do need one if your jar is a complete Java application and you wish it to be “executable”, e.g. by double-clicking in windows –You do need one if your jar houses a bean which you plan to deploy in an Integrated Development Tool You can create/edit the manifest: e.g. for LogoAnimator.jar, –Save in manifest.tmp the text Main-Class: LogoAnimator Name: LogoAnimator.class Java-Bean: True Jars
14
14 –... then (if LogoAnimator.jar already exists) run jar uvfm LogoAnimator.jar manifest.tmp –Or if you are creating the jar, jar cvfm LogoAnimator.jar manifest.tmp LogoAnimator.class manifest.tmp has the text Jar creates MANIFEST.MF –The optional Main-Class clause should refer to a class with a main method: it allows you to run the application by simply clicking the jar –The Name: filename.class and Java-Bean: True lines (which must be preceded by a blank line) declare the jar to be a bean. –These are used by "bean savvy" integrated development tools such as Forte or Sun Java Studio. Jars
15
15 To execute LogoAnimator from its JAR file java –jar LogoAnimator.jar Interpreter looks at the manifest file Java Archive File (jars) –contain JavaBeans –contains a manifest file describes JAR-file contents MANIFEST.MF –placed in the META-INF directory in JAR file
16
16 JavaBean Properties Property: Specific attribute of a JavaBean –Read/Write property Defined as a set/get method pair of the form public void set PropertyName ( DataType value ) public DataType get PropertyName () Builder tool inspects bean methods for these pairs –Process known as introspection –Builder exposes that pair of methods as a property e.g., to expose the background property: public void setBackground( Color color ) public Color getBackground()
17
17 Example Lets add animationDelay property to LogoAnimator We extend LogoAnimator to create LogoAnimator2 class New code has setAnimationDelay and getAnimationDelay
18
18
19
19 Bound Properties Bound property –JavaBean notifies other objects when property value changes Accomplished via standard Java event-handling features java.beans.PropertyChangeListener »Listens for java.beans.PropertyChangeEvents
20
20 An important optional feature of a Java bean is the ability to fire events. Java core package java.beans includes –class PropertyChangeEvent –interface PropertyChangeListener –class PropertyChangeSupport with methods firePropertyChange addPropertyChangeListener removePropertyChangeListener A bean equipped with a PropertyChangeSupport object can fire a PropertyChangeEvent by using the method firePropertyChange –Registered listeners receive these events, in the form of PropertyChangeEvent objects A listener is any object implementing the PropertyChangeListener –It must implement void propertyChange(PropertyChangeEvent e) Bound Properties
21
21 Example: the SliderFieldPanel –A graphical “control” for input of numerical data –Features a slider and a text box either can be used for input: on input, the other one updates its appearance –The object as a whole fires a PropertyChangeEvent. LogoAnimatorTst1.java links this graphical bean to a LogoAnimator Bound Properties
22
22 Indexed Properties Indexed property –Similar to standard property –Array of primitives of objects public void set PropertyName ( DataType [] data ) public void set PropertyName ( int index, DataType data) public DataType [] get PropertyName () public DataType get PropertyName ( int index )
23
23 You can design your own events –An event is an object of class extending class java.util.EventObject –It requires an interface extending interface java.util.EventListener A java bean can then work with events of this custom type –Add/remove listeners –Fire events Example: ColorSliderPanel.java bean –Assembles THREE SliderFieldPanel s into a JPanel –Uses these to set the red, green, blue components of the “colour property” indexed bean property int[] redGreenBlue –Uses custom “ColorEvent” events, defined by class ColorEvent and interface ColorListener –Maintains a set of listeners for these events Using java.util. HashSets –Fires a ColorEvent event when any of the three SliderFields is adjusted. Custom Events
24
24 Example: ColorSliderPanel.java (ctd) –Each SliderField component has a PropertyChangeListener which handles the PropertyChange by calling method setRedGreenBlue(...) – setRedGreenBlue(...) updates the indexed bean property int[] redGreenBlue and then fires a ColorChanged event. LogoAnimatorTst2.java links this graphical bean to a LogoAnimator adjusts the logo animator’s background colour in response to the ColorSliderPanel Custom Events
25
25 The Bean Builder GUI
26
26 The Bean Builder GUI The Design Panel
27
27 The Demo Application
28
28 The Palette
29
29 Instantiating the First Component: The JScrollPane
30
30 Instantiating Other Components
31
31 The Property Inspector
32
32 Editing Properties of Objects
33
33 Creating a Non-visual Bean
34
34 Setting an Object as a Property of Another
35
35 Creating Event Adapters
36
36 Testing the Runtime Application The live object graph can be tested by taking it out of Design Mode and putting it into Runtime Mode The two states are switchable In the Runtime Mode the objects become live The application behaves exactly as the way it was designed Type some entries in the text field: Press ‘Remove All
37
37 Using SpringLayout
38
38 WWW Resources –Look at the links from http://java.sun.com/products/javabeans/ –http://java.sun.com/products/javabeans/beanbuilder/index.html The Bean Builder is (besides Forte for Java and Sun Java Studio) another bean-based visual application developer. –http://java.sun.com/products/javabeans/docs/spec.html –The API reference http://java.sun.com/j2se/1.4.1/docs/api/ Look up package java.beans The Deitel et al textbook references cited above. Further Reading
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.