First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.

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

Introduction to Java 2 Programming
Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
By Dr. Jiang B. Liu 12. The Java Beans. Java Beans n JavaBeans is a portable, platform-independent software component model written in Java. It enables.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.”
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.
Io package as Java’s basic I/O system continue’d.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.
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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.”
Introduction to Java Beans CIS 421 Web-based Java Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
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.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM.
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.
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 Beans - Basics CIS 421 Web-based Java Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
CompSci 230 S Programming Techniques
Introduction to Graphics
Javabeans for dummies.
JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine
Ellen Walker Hiram College
Introduction to Event Handling
Constructors, GUI’s(Using Swing) and ActionListner
Pre-assessment Questions
Chapter 5 Processing Input with Applets
Knowledge Byte In this section, you will learn about:
Java Programming: Advanced Topics
Presentation transcript:

First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean

import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ //Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); }

All beans must implement Serializable Interface Sets background color and size Compile the Bean Create manifest file Create Jar file Load Jar in BeanBox Drop SimpleBean instance in BeanBox

Manifest File Describes entries in jar archive Made up of attribute/value pairs Format is “attribute: value” Name: SimpleBean.class Java-Bean: True

JAR Java archive file format Bundle any number of files ZIP compression Manifest describes content jar part of the jdk jar {ctx} [vfm0M] [jar-file] [manifest-file] files … jar cfm SimpleBean.jar manifest.tmp SimpleBean.class

Jar Command Options

Additional Jar Capabilities Security with Digital Signatures Decreased Applet Download Time Packaging of extensions Portability Package Sealing and Versioning Jar tutorial

Properties Properties are attributes of bean’s appearance and behavior that can be changed at design time. Private values accessed through getter and setter methods Encapsulation, Design Pattern Can be inherited (Canvas for SimpleBean)

Builder Tools and Properties Discover Bean’s Properties Determine properties’ read/write attributes Determine property types Locate property editors Display property sheet Alter properties

package sunw.demo.simple; import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ private Color color = Color.green; //property getter method public Color getColor(){ return color; } //property setter method. Sets new SimpleBean //color and repaints. public void setColor(Color newColor){ color = newColor; repaint(); } public void paint(Graphics g) { g.setColor(color); g.fillRect(20, 5, 20, 30); } //Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); }

Events in the BeanBox Uses either design pattern introspection or BeanInfo class to discover events For a bean to be a source for an event, it must implement methods that add or remove listeners for that event For example, if a source Bean registers ActionListener objects, it will fire events at those objects by calling the actionPerformed method on those listeners. public void add ( a) public void remove ( a)

Bound Properties When property changes, other objects may need to be notified and react. When bound property changes, notification is sent to interested listeners. Bean with bound property must maintain list of property listeners and fire PropertyChangeEvent objects PropertyChangeSupport

A Bean with bound property maintains list of property change listeners Alerts those listeners when bound property changes. Convenience class PropertyChangeSupport implements methods that add and remove PropertyChangeListener objects, and fires PropertyChangeEvent objects at those listeners when the bound property changes. Beans can inherit from this class, or use it as an inner class. By implementing the PropertyChangeListener interface the listener can be added to the list maintained by the bound property Bean, and because it implements the PropertyChangeListener.propertyChange method, the listener can respond to property change notifications. The PropertyChangeEvent class encapsulates property change information, and is sent from the property change event source to each object in the property change listener list via the propertyChange method.

Inherit or use inner class Objects interested in event implement PropertyChangeListener interface Beanbox handles events with event hookup adapter Tutorial

Import java.beans package Instantiate a PropertyChangeSupport object Implement methods to maintain list of property change event listeners Modify a property's setter method to fire a property change event when the property is changed Listeners implement PropertyChangeListener interface with propertyChange (PropertyChangeEvent) method

1.Drop OurButton and ChangeReporter instances on the BeanBox. 2.Select the OurButton instance and choose the Edit|Events|propertyChange|propertyChange menu item. 3.Connect the rubber band line to the ChangeReporter instance. The EventTargetDialog will be displayed. 4.Choose reportChange from the EventTargetDialog. The event hookup adapter source will be generated and compiled 5.Select OurButton and change some of it's properties. You will see change reports in ChangeReporter.

package sunw.demo.misc; import java.awt.*; import java.beans.*; import java.io.Serializable; /** * A simple extension of TextField that handles PropertyChangeEvents. * This Bean handles PropertyChange events by displaying the name of * the property and the new property value. A Java Bean that displays * bound properties can be connected to a ChangeReporter with the * BeanBox. To do so programatically is straightforward: * * import sunw.demo.misc.OurButton; * import sunw.demo.misc.ChangeReporter; * import java.awt.*; * import java.beans.*; *

* public class DemoChangeReporter * { * OurButton button = new OurButton(); * ChangeReporter reporter = new ChangeReporter(); * PropertyChangeAdapter adapter = new PropertyChangeAdapter(); * * DemoChangeReporter() * { * button.addPropertyChangeListener(adapter); * button.setLabel("Report This"); * * Frame f = new Frame("Demo Change Reporter"); * f.setLayout(new FlowLayout()); * f.add(button); * f.add(reporter); * f.pack(); * f.show(); * } * * class PropertyChangeAdapter implements PropertyChangeListener * { * public void propertyChange(PropertyChangeEvent e) * { * reporter.reportChange(e); * } *

* public static void main(String[] argv) * { * new DemoChangeReporter(); * } * * In the example above, the button is connected to the ChangeReporter with * a (nested) adpater class. When the DemoChangeReporter object is constructed * the buttons label field is set, and the adapters propertyChange method * runs. * */ public class ChangeReporter extends TextField implements Serializable { public ChangeReporter() { super("", 35); setEditable(false); } public void reportChange(PropertyChangeEvent evt) { String text = evt.getPropertyName() + " := " + evt.getNewValue(); int width = getSize().width - 10; Font f = getFont(); if (f != null) { // Trim the text to fit. FontMetrics fm = getFontMetrics(f); while (fm.stringWidth(text) > width) { text = text.substring(0, text.length()-1); } setText(text); }}

package sunw.demo.buttons; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.Serializable; import java.util.Vector; /** * A simple Java Beans button. OurButton is a "from-scratch" * lightweight AWT component. It's a good example of how to * implement bound properties and support for event listeners. * * Parts of the source are derived from sun.awt.tiny.TinyButtonPeer. */ public class OurButton extends Component implements Serializable, MouseListener, MouseMotionListener { /** * Constructs a Button with the a default label. */ public OurButton() { this("press"); }

// // Methods for registering/deregistering event listeners /** * The specified ActionListeners actionPerformed method will * be called each time the button is clicked. The ActionListener * object is added to a list of ActionListeners managed by * this button, it can be removed with removeActionListener. * Note: the JavaBeans specification does not require ActionListeners * to run in any particular order. * #removeActionListener l the ActionListener */ public synchronized void addActionListener(ActionListener l) { pushListeners.addElement(l); } /** * Remove this ActionListener from the buttons internal list. If the * ActionListener isn't on the list, silently do nothing. * #addActionListener l the ActionListener */ public synchronized void removeActionListener(ActionListener l) { pushListeners.removeElement(l); }

/** * The specified PropertyChangeListeners propertyChange method will * be called each time the value of any bound property is changed. * The PropertyListener object is addded to a list of PropertyChangeListeners * managed by this button, it can be removed with removePropertyChangeListener. * Note: the JavaBeans specification does not require PropertyChangeListeners * to run in any particular order. * #removePropertyChangeListener l the PropertyChangeListener */ public void addPropertyChangeListener(PropertyChangeListener l) { changes.addPropertyChangeListener(l); } /** * Remove this PropertyChangeListener from the buttons internal list. * If the PropertyChangeListener isn't on the list, silently do nothing. * #addPropertyChangeListener l the PropertyChangeListener */ public void removePropertyChangeListener(PropertyChangeListener l) { changes.removePropertyChangeListener(l); }

// /** * This method has the same effect as pressing the button. * #addActionListener */ public void fireAction() { if (debug) { System.err.println("Button " + getLabel() + " pressed."); } Vector targets; synchronized (this) { targets = (Vector) pushListeners.clone(); } ActionEvent actionEvt = new ActionEvent(this, 0, null); for (int i = 0; i < targets.size(); i++) { ActionListener target = (ActionListener)targets.elementAt(i); target.actionPerformed(actionEvt); } private PropertyChangeSupport changes = new PropertyChangeSupport(this); private Vector pushListeners = new Vector();

Other Properties Constrained Properties: Change to property can be vetoed by other objects. Indexed Properties: Properties with multiple values (e.g. like a list of choices) See Tutorial for additional informationTutorial