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

Slides:



Advertisements
Similar presentations
Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
Advertisements

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.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
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.
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.”
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
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.
2. Introduction to the Visual Studio.NET IDE 2. Introduction to the Visual Studio.NET IDE Ch2 – Deitel’s Book.
Component-Based Software Engineering Introduction to Java Beans Paul Krause and Sotiris Moschoyiannis.
Java Beans.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 10 Introduction to Components. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
IE 411/511: Visual Programming for Industrial Applications
Introduction to Matlab & Data Analysis
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
Tutorial 121 Creating a New Web Forms Page You will find that creating Web Forms is similar to creating traditional Windows applications in Visual Basic.
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
1 Object-Oriented Software Engineering CS GUI building with NetBeans Contents User Interaction JButtons JTextFields JLabels JForms in NetBeans Practical.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
SWE 316: Software Design and Architecture Objectives Lecture # 18 Introduction to Components SWE 316: Software Design and Architecture To learn:  benefits.
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.”
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 21 JavaBeans, Bean Events, and MVC Architecture.
1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,
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.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
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.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
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.
JavaServer Faces framework Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Beans THETOPPERSWAY.COM. Contents What is a Java Bean? Advantages Properties Bean Developers Kit (BDK) What makes Bean possible? Support for Java.
Java Beans - Basics CIS 421 Web-based Java Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Visual Programming Borland Delphi. Developing Applications Borland Delphi is an object-oriented, visual programming environment to develop 32-bit applications.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Chapter 32 JavaBeans and Bean Events
Chapter 36 JavaBeans and Bean Events
Chapter 2 – Introduction to the Visual Studio .NET IDE
Java Beans Sagun Dhakhwa.
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Component-Based Software Engineering
Java Programming: Advanced Topics
Presentation transcript:

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

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

3 Outline Introduction Making a Bean class Jar files Bound Properties Custom events Bean Builder GUI and usage

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!

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

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

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

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

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 i th entry in the array –A bean class implements Serializable … so that it can be stored A simple example... Making a Bean Class

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

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

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 Jars

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

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

15 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

16 JavaBean Properties Property: Specific attribute of a JavaBean –Read/Write property Defined as a set/get method pair of the form public void set PropertyName ( DataType value ) public DataType get PropertyName () 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()

17 Example Lets add animationDelay property to LogoAnimator We extend LogoAnimator to create LogoAnimator2 class New code has setAnimationDelay and getAnimationDelay

18

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

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

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

22 Indexed Properties Indexed property –Similar to standard property –Array of primitives of objects public void set PropertyName ( DataType [] data ) public void set PropertyName ( int index, DataType data) public DataType [] get PropertyName () public DataType get PropertyName ( int index )

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

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

25 The Bean Builder GUI

26 The Bean Builder GUI The Design Panel

27 The Demo Application

28 The Palette

29 Instantiating the First Component: The JScrollPane

30 Instantiating Other Components

31 The Property Inspector

32 Editing Properties of Objects

33 Creating a Non-visual Bean

34 Setting an Object as a Property of Another

35 Creating Event Adapters

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

37 Using SpringLayout

38 WWW Resources –Look at the links from – The Bean Builder is (besides Forte for Java and Sun Java Studio) another bean-based visual application developer. – –The API reference Look up package java.beans The Deitel et al textbook references cited above. Further Reading