1 / 38

CG0165: Advanced Applications Development in Java

CG0165: Advanced Applications Development in Java. Week 5: JavaBeans. Michael Brockway Sajjad Shami. Northumbria University School of Computing, Engineering & Information Sciences. References. Several slides in this lecture have made use of material with modifications provided by

berget
Download Presentation

CG0165: Advanced Applications Development in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CG0165: Advanced Applications Development in Java Week 5: JavaBeans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences

  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. Outline • Introduction • Making a Bean class • Jar files • Bound Properties • Custom events • Bean Builder GUI and usage

  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. Introduction • 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 <jsp:useBean> 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

  6. Environment • In an IDE such as Sun's Forte or the more recent SunONEStudioor 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".

  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 Animation Stop Animation

  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. Making a Bean Class • 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 TypegetProp(int i) returns the ith entry in the array • A bean class implements Serializable • … so that it can be stored • A simple example...

  10. Example 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; } }

  11. Another Example • 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.

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

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

  14. Jars • ... 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.tmpLogoAnimator.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.

  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. JavaBean Properties • Property: Specific attribute of a JavaBean • Read/Write property • Defined as a set/get method pair of the form public voidsetPropertyName(DataTypevalue)publicDataTypegetPropertyName() • 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 backgroundproperty: public void setBackground(Color color)publicColor getBackground()

  17. Example • Lets add animationDelay property to LogoAnimator • We extend LogoAnimator to create LogoAnimator2 class • New code has setAnimationDelay and getAnimationDelay

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

  19. Bound Properties • 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)

  20. Bound Properties • 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

  21. Indexed Properties • Indexed property • Similar to standard property • Array of primitives of objects public voidsetPropertyName(DataType[]data)public voidsetPropertyName(intindex, DataType data)publicDataType[]getPropertyName()publicDataTypegetPropertyName( intindex )

  22. Custom Events • 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 SliderFieldPanels 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.

  23. Custom Events • 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

  24. The Bean Builder GUI

  25. The Bean Builder GUI The Design Panel

  26. The Demo Application

  27. The Palette

  28. Instantiating the First Component: The JScrollPane

  29. Instantiating Other Components

  30. The Property Inspector

  31. Editing Properties of Objects

  32. Creating a Non-visual Bean

  33. Setting an Object as a Property of Another

  34. Creating Event Adapters

  35. Testing the Runtime Application • The live object graph can be tested by taking it out of Design Mode and putting it into RuntimeMode • 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

  36. Using SpringLayout

  37. Further Reading • 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.

More Related