Beans - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Beans

Description:

Class must be serializable (able to be stored in binary format) Facilitate rapid application development by assembling ... You should have created a .jar file. ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 13
Provided by: cyndi8
Category:
Tags: ajar | beans

less

Transcript and Presenter's Notes

Title: Beans


1
Beans
2
Java Beans
  • Reusable software component model 
  • Class that follows a few coding standard
    (properties with getters/setters, includes
    default constructor)
  • Class must be serializable (able to be stored in
    binary format)
  • Facilitate rapid application development by
    assembling software components
  • IDE is used to connect beans together... such as
    connecting animation bean to button bean
  • JavaBeans are stored in Java Archive file (jar)
  • Can be executed from jar, if includes main
  • java -jar LogoAnimator.jar
  • Commonly used with JSP and servlets

3
Properties
  • Instance variables can be used as JavaBean
    properties by defining set/get methods.
  • For a variable propertyName, the methods should
    be
  • public void setPropertyName(DataType value)
  • public DataType getPropertyName()
  • Note that the first letter is capitalized.
  • Properties can be read-only (only a get method)
    or write-only (only a set method)

4
Bound Properties
  • A bound property causes the JavaBean that owns
    the property to notify other objects when the
    property's value changes. 
  • Bean notifies its PropertyChangeListeners when
    the value changes.
  • java.beans includes interface PropertyChangeListen
    er to facilitate
  • Class PropertyChangeSupport provides listener
    registration and notification services.
  • note property listeners/event handling not used
    with jsp

5
SliderField Code
  • public SliderFieldPanel()
  • // create PropertyChangeSupport for bound
    properties
  • changeSupport new PropertyChangeSupport(
    this )
  • boxContainer.add( slider )
  • slider.addChangeListener(
  • new ChangeListener()
  • // handle state change for JSlider
  • public void stateChanged( ChangeEvent
    changeEvent )
  • setCurrentValue( slider.getValue()
    )
  • // end anonymous inner class

6
SliderField Code
  • SliderFieldPanel.java public void
    addPropertyChangeListener(
  • PropertyChangeListener listener )
  • changeSupport.addPropertyChangeListener(
    listener )

7
SliderField Code
  • public void setCurrentValue( int current )
  • throws IllegalArgumentException
  • if ( current lt 0 )
  • throw new IllegalArgumentException()
  • int oldValue currentValue
  • // set currentValue property
  • currentValue current
  • // change slider and textfield values
  • slider.setValue( currentValue )
  • field.setText( String.valueOf( currentValue
    ) )
  • // fire PropertyChange
  • changeSupport.firePropertyChange(
    "currentValue",
  • new Integer( oldValue ), new Integer(
    currentValue ) )

8
Using the SliderField
  • public class LogoAnimator3 extends LogoAnimator
  • implements PropertyChangeListener
  • public void
  • propertyChange(PropertyChangeEvent e)
  • setAnimationDelay(Integer.parseInt(
  • e.getNewValue().toString()))

9
Using the Slider Field
  • // create new LogoAnimator2
  • LogoAnimator3 animation new
    LogoAnimator3()
  • // create new JFrame and add LogoAnimator3 to it
  • JFrame application new JFrame( "Animator test"
    )
  • application.getContentPane().add( animation,
  • BorderLayout.CENTER )
  • SliderFieldPanel slider new SliderFieldPanel()
  • application.getContentPane().add(slider,
    BorderLayout.SOUTH)
  • slider.addPropertyChangeListener(animation)

10
Creating a JavaBean in Eclipse
  • NOTE This does not seem to work with Eclipse
    3.2?
  • Create your bean
  • From the File Menu select the Export
  • Select JAR file.  Press Next.
  • Select the source file and your image directory. 
    Defaults should be "Export generated class files
    and resources" and "Compress files".  Enter a
    name for your jar file.  Press Next.
  • I recommend that you don't export if you have
    compiler errors, but warnings are probably ok.
    Press Next.
  • Generate the manifest and Seal the jar (for
    security, assures reader all files from same
    location).
  • Select the class that contains main (e.g.,
    LogoAnimator.java).  Press Finish.
  • You should have created a .jar file.
  • Try to execute your file from the command line
    using the command java -jar yourfilename (e.g.,
    java -jar LogoAnimator.jar).  OR double-click
    from a Windows explorer.

11
Creating a JavaBean jar from the command line
  • Create your bean
  • Create a manifest  which specifies the name and
    that it is a Java-Bean, and if it contains main
    (see example below)
  • Name in manifest must use forward slashes (/),
    even if on Windows system
  • Must specify Java-Bean True
  • Note that Main-Class uses dots as separators
  • Create the jar file jar cfm JarName.jar
    manifest.tmp package\.
  • where c is for create, f says next argument is
    name of file, m indicates next parameter is the
    manifest name.
  • The directory listed should include the class
    file(s) and any images needed.
  • Example jar cfm LogoAnimator.jar manifest.tmp
    com\deitel\advjhtp1\beans\.
  • If you have an images or other directory, it
    should be included in your directory structure
    above (i.e., an images directory under beans). 
    In the above example, you would have a class that
    would be in package com.deitel.advjhtp1.beans,
    which would have been compiled so that the class
    file is in the proper directory.
  • Can verify with jar tvf JarName.jar
  • where t is table of contents, v is verbose, f
    specifies next argument is name of jar file
  • Try to execute your file from the command line
    using the command java -jar yourfilename (e.g.,
    java -jar LogoAnimator.jar). 

12
Example manifest
  • Main-Class com.deitel.advjhtp1.beans.LogoAnimator
    3
  • Name com/deitel/advjhtp1/beans/LogoAnimator3.clas
    s
  • Java-Bean True
Write a Comment
User Comments (0)
About PowerShow.com