Java beans basics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

Java Beans - Basics PowerPoint PPT Presentation


  • 132 Views
  • Uploaded on
  • Presentation posted in: General

Java Beans - Basics. CIS 421 Web-based Java Programming. Very Simple Java Bean (Example 1 – Inherited Property). import java.awt.*; public class VerySimpleBean extends Canvas { //constructor to set inherited properties public VerySimpleBean() {setSize(60,40);

Download Presentation

Java Beans - Basics

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java beans basics

Java Beans - Basics

CIS 421

Web-based Java Programming


Very simple java bean example 1 inherited property

Very Simple Java Bean(Example 1 – Inherited Property)

import java.awt.*;

public class VerySimpleBean extends Canvas {

//constructor to set inherited properties

public VerySimpleBean()

{setSize(60,40);

setBackground(Color.red);

} //end of the constructor VerySimpleBean

} // end of the class VerySimpleBean


Creating a very simple java bean

Creating a Very Simple Java Bean

  • Compile the source code for the Bean.

    • Javac VerySimpleBean.java

  • Create a manifest file.

    Name: VerySimpleBean.class

    Java-Bean: True

  • Create a JAR file.

    • jar cfm VerySimpleBean.jar VerySimpleBean.mf VerySimpleBean.class

  • Load the jar file in the builder (e.g. beanbox).

    • Now we can use it (but it doesn’t do anything)


Properties

Properties

  • Simple Properties:

    • Used to represent single attribute,

    • Single value at an instance of time


Java bean example 2 simple property color

Java Bean (Example 2 – Simple Property: Color)

import java.awt.*;

import java.io.Serializable;

public class SimpleBean extends Canvas implements Serializable

{

private Color color = Color.green;

//Constructor: sets inherited properties

public SimpleBean()

{setSize(60,40);

setBackground(Color.red);

}

//set method

public void setColor(Color newColor)

{color = newColor;

repaint();

}

//get method

public Color getColor()

{return color;

}

//override paint method

public void paint (Graphics g)

{g.setColor(color);

g.fillRect(20,5,20,30);

}

}


Java bean example 3 simple properties color label

Java Bean(Example 3 – Simple Properties: Color & Label)

//set method for label

public void setLabel (String newLabel)

{ String oldLabel=label;

label=newLabel;

}

//get method for label

public String getLabel()

{ return label; }

//override paint method

public void paint (Graphics g)

{ g.setColor(color);

g.fillRect(20,5,20,30);

g.fillArc(5, 5, 30, 30, 0, 360);

g.fillArc(25, 5, 30, 30, 0, 360);

g.setColor(Color.blue);

int width = size().width;

int height = size().height;

FontMetrics fm = g.getFontMetrics();

g.drawString(label,

(width - fm.stringWidth(label)) / 2,

(height + fm.getMaxAscent()

- fm.getMaxDescent()) / 2);

}

} // class

import java.awt.*;

public class SimpleBean1 extends Canvas

{ private Color color = Color.green;

private String label;

//Constructor: sets inherited properties

public SimpleBean1()

{setSize(60,40);

setBackground(Color.red);

this.label="Bean";

setFont(new Font("Dialog", Font.PLAIN,12));

}

}

//set method

public void setColor(Color newColor)

{ color = newColor;

repaint();

}

//get method

public Color getColor()

{return color; }


Properties1

Properties

  • Bound Properties:

    • A component can choose to provide a change notification service for some or all of its properties. Such properties are commonly known as bound properties .

    • The PropertyChangeListener event listener interface is used to report updates to simple bound properties. If a bean supports bound properties then it should support a normal pair of multicast event listener registration methods for PropertyChangeListeners:

      public void addPropertyChangeListener (PropertyChangeListener x);

      public void removePropertyChangeListener (PropertyChangeListener x);

    • When a property change occurs on a bound property the bean should call the PropertyChangeListener.propertyChange method on any registered listeners, passing a PropertyChangeEvent object that encapsulates the locale-independent name of the property and its old and new values.


Properties2

Properties

  • Bound Properties contd…

    • The event source should fire the event after updating its internal state

    • For programming convenience, a utility class PropertyChangeSupport is provided that can be used to keep track of PropertyChangeListeners and to fire PropertyChange events.

    • Example: BoundProperty.java

      • Font is bound


Properties3

Properties

  • Constrained Properties:

    • Sometimes when a property change occurs some other bean may wish to validate the change and reject it if it is inappropriate. The properties that undergo this kind of checking are referred as constrained properties.

    • Constrained Property set methods are required to support the PropertyVetoException. This documents to the users of the constrained property that attempted updates may be vetoed.

    • The VetoableChangeListener event listener interface is used to report updates to constrained properties.

    • If a bean supports constrained properties then it should support a normal pair of multicast event listener registration methods for VetoableChangeListeners:

      public void addVetoableChangeListener (VetoableChangeListener x);

      public void removeVetoableChangeListener(VetoableChangeListener x);


Properties4

Properties

  • Constrained Properties contd…

    • When a property change occurs on a constrained property the bean should call the VetoableChangeListener.vetoableChange method on any registered listeners, passing a PropertyChangeEvent object that encapsulates the locale-independent name of the property and its old and new values.

    • If the event recipient does not wish the requested edit to be performed it may throw a PropertyVetoException.

    • It is the source bean’s responsibility to catch this exception, revert to the old value, and issue a new VetoableChangeListener.vetoableChange event to report the reversion.


Properties5

Properties

  • Constrained Properties contd…

    • For programming convenience a utility class VetoableChangeSupport is provided that can be used to keep track of VetoableChangeListeners and to fire VetoableChange events.

      Example – Bound and Constrained Property

      JellyBean.java


Events

Events

  • Events:

    • One of the core features of the Java Beans architecture.

    • Provide a convenient mechanism for allowing components to be plugged together in an application builder by allowing some components to act as sources for event notifications that can then be caught and processed by either scripting environments or by other components.

  • Events overview:

    • Event notifications are propagated from sources to listeners by Java method invocations on the target listener objects.

    • Each distinct kind of event notification is defined as a distinct Java method. These methods are then grouped in EventListener interfaces that inherit from java.util.EventListener.


Events1

Events

  • Event listener classes identify themselves as interested in a particular set of events by implementing some set of EventListeners interfaces.

  • The state associated with an event notification is normally encapsulated in an event state object that inherits from java.util.EventObject and which is passed as the sole argument to the event method.

  • Event sources identify themselves as sourcing particular events by defining registration methods that conform to a specific design pattern and accept (?) references to instances of particular EventListener interfaces.

  • In circumstances where listeners cannot directly implement a particular interface, or when some additional behavior is required, an instance of a custom adaptor class may be interposed between a source and one or more listeners in order to establish the relationship or to augment behavior.


Events examples code segment

Events(Examples – code segment)

  • Registering event listeners:

    • Consider a typical button Bean that generates events when pressed.

      • If the button bean wants to be event source, it must provide two methods that can be called by interested objects. One method adds the caller to the list of listeners who are notified when the event occurs. The other method removes the caller from the list of interested listeners.

      • public synchronized void addActionListener (ActionListener 1) { … }

      • public synchronized void removeActionListener (ActionListener 1) { … }

      • Action can be something like: Mouse, MouseMotion, etc.

        addMouseListener ( … ) { … }, addMouseMotionListener ( … ) { … }


Events examples code segment1

Events(Examples – code segment)

  • Button Bean contd…

    • The button bean maintains a list (or Vector) of listeners. Thus the source bean declares the following line:

      • private Vector listeners = new Vector();

        Now,

        public synchronized void addActionListener (ActionListener 1) {

        listeners.addElement(1);

        }

        public synchronized void removeActionListener(ActionListener 1) {

        listeners.removeElement(1);

        }


  • Login