+ All Categories
Home > Documents > CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans...

CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans...

Date post: 13-Jan-2016
Category:
Upload: lesley-dalton
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
38
CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering & Information Sciences
Transcript
Page 1: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

CG0165: Advanced Applications Development in Java

Michael BrockwaySajjad Shami

Week 5: JavaBeans

Northumbria University

School of Computing, Engineering & Information Sciences

Page 2: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 3: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

3

Outline

• Introduction

• Making a Bean class

• Jar files

• Bound Properties

• Custom events

• Bean Builder GUI and usage

Page 4: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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!

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

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

Introduction

Page 6: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 7: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 8: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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/

Page 9: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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 ith entry in the array

– A bean class implements Serializable• … so that it can be stored

• A simple example...

Making a Bean Class

Page 10: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 11: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 12: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 13: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 14: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 15: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

15 2002 Prentice Hall.All rights reserved.

Outline

Fig. 6.36 Method file manifest.tmpfor the Logo-Animator bean.

Line 1

Line 4

1 Main-Class: com.deitel.advjhtp1.beans.LogoAnimator2 3 Name: com/deitel/advjhtp1/beans/LogoAnimator.class4 Java-Bean: True

Class LogoAnimator contains method main, so virtual machines can execute the

application in this JAR file directly

Specify that LogoAnimatorshould be treated as a JavaBean

• 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

Page 16: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

16

JavaBean Properties

• Property: Specific attribute of a JavaBean

– Read/Write property

• Defined as a set/get method pair of the form public void setPropertyName( DataType value ) public DataType getPropertyName()

• 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()

Page 17: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

17

Example

• Lets add animationDelay property to LogoAnimator

• We extend LogoAnimator to create LogoAnimator2 class

• New code has setAnimationDelay and getAnimationDelay

Page 18: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

18 2002 Prentice Hall.All rights reserved.

Outline

Fig. 6.39 Logo-Animator2 with property animationDelay(part 1).

Line 13

Lines 16-25

1 // Fig. 6.39: LogoAnimator2.java2 // LogoAnimator2 extends LogoAnimator to include 3 // animationDelay property and implements ColorListener4 package com.deitel.advjhtp1.beans;5 6 // Java core packages7 import java.awt.*;8 import java.awt.event.*;9 10 // Java extension packages11 import javax.swing.*;12 13 public class LogoAnimator2 extends LogoAnimator {14 15 // set animationDelay property16 public void setAnimationDelay( int delay )17 {18 animationTimer.setDelay( delay );19 }20 21 // get animationDelay property22 public int getAnimationDelay()23 {24 return animationTimer.getDelay();25 }26 27 // launch LogoAnimator in JFrame for testing28 public static void main( String args[] )29 {30 // create new LogoAnimator231 LogoAnimator2 animation = new LogoAnimator2();32

Extend LogoAnimator to enable user to modify the rate of animation

Builder tool will expose animationDelay property

Page 19: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 20: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 21: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 22: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

22

Indexed Properties

• Indexed property

– Similar to standard property

– Array of primitives of objects public void setPropertyName( DataType[] data ) public void setPropertyName(int index, DataType data)

public DataType[] getPropertyName()public DataType getPropertyName( int index )

Page 23: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Custom Events

Page 24: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 25: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

25

The Bean Builder GUI

Page 26: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

26

The Bean Builder GUI

The Design Panel

Page 27: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

27

The Demo Application

Page 28: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

28

The Palette

Page 29: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

29

Instantiating the First Component: The JScrollPane

Page 30: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

30

Instantiating Other Components

Page 31: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

31

The Property Inspector

Page 32: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

32

Editing Properties of Objects

Page 33: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

33

Creating a Non-visual Bean

Page 34: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

34

Setting an Object as a Property of Another

Page 35: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

35

Creating Event Adapters

Page 36: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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

Page 37: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

37

Using SpringLayout

Page 38: CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.

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


Recommended