Java Beans - Basics CIS 421 Web-based Java Programming.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
Exceptions and Exception Handling Carl Alphonce CSE116.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Chapter 71 Inheritance Chapter 7. 2 Reminders Project 4 was due last night Project 5 released: due Oct 10:30 pm Project 2 regrades due by midnight.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
JAVA BEANS By Madhuri Kakumanu. What is a Java Bean? “ A Java Bean is a reusable software component that can be visually manipulated in builder tools.”
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
JAVA BEANS (Unit-V) K.Phani Sirisha.
Introducing JavaBeans Lesson 2A / Slide 1 of 30 JDBC and JavaBeans Pre-assessment Questions 1.Which of the given symbols is used as a placeholder for PreparedStatement.
Introducing JavaBeans Identify the features of a JavaBean Create and add a bean to an application Manipulate bean properties using accessor and mutator.
Java Beans.  Java offers software component development through java Beans  Java Beans are based on a software component model for java.  The model.
Java Beans Component Technology Integrated, Visual Development Environments Reusable Platform-Independent Modular.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 2, Week 4 Design by Contract, Java Exceptions, Eventing, Finding the Classes,
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Last update October 18, 2004 Advanced Programming 2004 Java Beans.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Introduction to Component-Based Engineering Howard Abrams
Components Components are specialized self contained Software entities that can be replicated, Customized and inserted into applications. Components come.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
1 Chapter 21 JavaBeans, Bean Events, and MVC Architecture.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Introduction to Java Beans CIS 421 Web-based Java Programming.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean.
Introduction to Java Beans From Anders Børjesson.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Lorenz: Visitor Beans: An Aspect-Oriented Pattern Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Chapter 32 JavaBeans and Bean Events
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 36 JavaBeans and Bean Events
Javabeans for dummies.
Lecture 09 Applets.
JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine
Java Beans Sagun Dhakhwa.
Persistence & Bean Properties
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Java Programming: Advanced Topics
Presentation transcript:

Java Beans - Basics CIS 421 Web-based Java Programming

2 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

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

4 Properties  Simple Properties: –Used to represent single attribute, –Single value at an instance of time

5 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); }

6 Java Bean (Example 3 – Simple Properties: Color & Label) 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; } //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

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

8 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

9 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);

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

11 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

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

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

14 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 ( … ) { … }

15 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); }