Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 55 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
55
Dung lượng
179,5 KB
Nội dung
Môn Lập trình Java NC Môn Lập trình Java NC Chương 5: JavaBeansChương 5: JavaBeans 2 Object Oriented Programming 2 - Chapter 6: JavaBeans Content Content Introduction Introduction The Bean-Writing process The Bean-Writing process Using Bean to build an Application Using Bean to build an Application Naming Patterns for Bean Properties and Naming Patterns for Bean Properties and Events Events Bean Property Types Bean Property Types Adding Custom Bean Events Adding Custom Bean Events Property Editors Property Editors Going Beyond Naming Patterns Going Beyond Naming Patterns Customizers Customizers The Bean Context The Bean Context 3 Object Oriented Programming 2 - Chapter 6: JavaBeans 1. Introduction 1. Introduction What is a JavaBean? What is a JavaBean? A JavaBean is a A JavaBean is a component component : : a self-contained, reusable software unit a self-contained, reusable software unit it can be it can be visually composed visually composed with other beans to with other beans to make bigger components, applets, applications, etc. make bigger components, applets, applications, etc. 4 Object Oriented Programming 2 - Chapter 6: JavaBeans 1. Introduction (cont.) 1. Introduction (cont.) A bean is designed to A bean is designed to expose expose its features: its features: e.g. public methods, properties (data), events e.g. public methods, properties (data), events Exposure allows beans to be used inside visual Exposure allows beans to be used inside visual building tools (e.g. JBuilder) building tools (e.g. JBuilder) Exposure is based on coding rules that a Exposure is based on coding rules that a programmer must follow when writing a bean programmer must follow when writing a bean these rules are mostly these rules are mostly standard naming standard naming conventions for methods conventions for methods 5 Object Oriented Programming 2 - Chapter 6: JavaBeans 1. Introduction (cont.) 1. Introduction (cont.) Beans are Beans are persistent persistent their data is automatically stored when the program their data is automatically stored when the program using them finishes using them finishes the data is restored when the program next starts the data is restored when the program next starts persistence is implemented in JavaBeans by using persistence is implemented in JavaBeans by using Java's object Java's object serialization serialization features features 6 Object Oriented Programming 2 - Chapter 6: JavaBeans 1. Introduction (cont.) 1. Introduction (cont.) Simple Java object must support the following Simple Java object must support the following five features for it to become a JavaBean: five features for it to become a JavaBean: Properties Properties Customization Customization Persistence Persistence Communication Communication Introspection Introspection 7 Object Oriented Programming 2 - Chapter 6: JavaBeans 1. Introduction (cont.) 1. Introduction (cont.) You can create three types of beans: You can create three types of beans: Control beans Control beans Container beans Container beans Invisible run-time beans Invisible run-time beans 8 Object Oriented Programming 2 - Chapter 6: JavaBeans 2. The Bean-Writing process 2. The Bean-Writing process How Builder Tools Use Beans How Builder Tools Use Beans ? ? Builder tools (e.g JBuilder, Bean Builder) know Builder tools (e.g JBuilder, Bean Builder) know a bean's features by looking at its public a bean's features by looking at its public methods, properties and events methods, properties and events called called introspection introspection implemented using Java's reflection classes implemented using Java's reflection classes A builder can also examine a bean's information A builder can also examine a bean's information object ( object ( BeanInfo BeanInfo ), if the programmer has ), if the programmer has included one. included one. 9 Object Oriented Programming 2 - Chapter 6: JavaBeans 2. The Bean-Writing process (cont.) 2. The Bean-Writing process (cont.) How Builder Tools Use Beans How Builder Tools Use Beans ? ? A builder will have visual support for setting/ A builder will have visual support for setting/ reading a bean's properties, and for accessing reading a bean's properties, and for accessing its methods. its methods. A builder will be able to 'link' beans based on A builder will be able to 'link' beans based on events events a a source bean source bean can send an event to a can send an event to a listener bean listener bean 10 Object Oriented Programming 2 - Chapter 6: JavaBeans 2. The Bean-Writing process (cont.) 2. The Bean-Writing process (cont.) Using Java's Bean Builder Using Java's Bean Builder Bean Builder Website: Bean Builder Website: https://bean-builder.dev.java.net/ https://bean-builder.dev.java.net/ there's a "readme" link, and a good tutorial there's a "readme" link, and a good tutorial Download Bean Builder v. Download Bean Builder v. 1 1 . . 0 beta 0 beta from the "Documents and Files" link under "Project from the "Documents and Files" link under "Project Tools" Tools" also at our Website on fivedots also at our Website on fivedots [...]... process (cont.) JavaBeans – Development Phases: Construction phase Build phase Involves creation of a JavaBean and its user interface Involves placing the JavaBean into the target container Execution Phase Involves execution of the container application in which the JavaBean is placed Object Oriented Programming 2 - Chapter 6: Java 11 2 The Bean-Writing process (cont.) JavaBeans – Development... Programming 2 - Chapter 6: Java 12 2 The Bean-Writing process (cont.) JavaBeans – Development Phases (cont.): The Manifest file: Is used by the target application to recognize a JavaBean Is saved with a mft extension Contains a list of all class files that make up a JavaBean Object Oriented Programming 2 - Chapter 6: Java 13 2 The Bean-Writing process (cont.) JavaBeans The JAR – Development... Oriented Programming 2 - Chapter 6: Java 22 4 Naming Patterns for Bean Properties and Events Object Oriented Programming 2 - Chapter 6: Java 23 5 Bean Property Types JavaBeans have 4 kinds of properties, which are supported in different ways: simple properties indexed properties bound properties constrained properties Object Oriented Programming 2 - Chapter 6: Java 24 5 Bean Property Types (cont.)... used to package a JavaBean for distribution Contains the manifest file and all other files such as the class files and picture files of the bean Has a jar extension Object Oriented Programming 2 - Chapter 6: Java 14 3 Using Bean to build an Application in JAR Files Packaging Beans Composing Beans in a Builder Environment Object Oriented Programming 2 - Chapter 6: Java 15 3 Using Bean to build... Change Listeners Object Oriented Programming 2 - Chapter 6: Java 31 5 Bean Property Types (cont.) Example SliderFieldPanel Bean A JSlider and a JTextField A change to either should affect the other, and any listener objects should be notified Object Oriented Programming 2 - Chapter 6: Java 32 5 Bean Property Types (cont.) Listening Beans Format Beans that are registered listeners for property changes... Object Oriented Programming 2 - Chapter 6: Java 25 5 Bean Property Types (cont.) Example A fileName property (a String) is represented by: public String getFileName(); public void setFileName(String fn); A running property (a boolean) is: public boolean isRunning(); public void setRunning(boolean b); Object Oriented Programming 2 - Chapter 6: Java 26 5 Bean Property Types (cont.) Indexed Properties... Oriented Programming 2 - Chapter 6: Java 29 5 Bean Property Types (cont.) Bound Properties A bean with a bound property will notify other listening objects /beans when the property changes The programmer must add the listeners to a collection of PropertyChangeListeners sometimes called registering the listeners Object Oriented Programming 2 - Chapter 6: Java 30 5 Bean Property Types (cont.) When... other (cont.) There may be resources) in a JAR file each has its own Name and Java- Bean line Not all the beans; if a line is set resources in a JAR file need to be class is a bean then the Java- Bean to true (otherwise false) beans will appear in the Toolbox window Object Oriented Programming 2 - Chapter 6: Java 18 3 Using Bean to build an Applicationfile (cont.) Create the JAR c:> jar cfm LogoAnimator.jar... and new values Object Oriented Programming 2 - Chapter 6: Java 33 5 Bean Property Types (cont.) Listening Beans Format (cont.) class Listener implements PropertyChangeListener { void propertyChange(PropertyChangeEvent ev) { Object newVal = ev.getNewValue(); Object oldVal = ev.getOldValue(); } } Object Oriented Programming 2 - Chapter 6: Java 34 5 Bean Property Types (cont.) Constrained Properties A... directory of the JAR file Object Oriented Programming 2 - Chapter 6: Java 16 3 Using Bean to build an Application (cont.) manifest.tmp example Name: myPackage/myBeanClass.class Java- Bean: True A Name line says where a bean is located in the JAR file '/' is used instead of Java' s '.' Object Oriented Programming 2 - Chapter 6: Java 17 3 Using Bean to build an Applicationmany classes (and other (cont.) . restored when the program next starts persistence is implemented in JavaBeans by using persistence is implemented in JavaBeans by using Java's object Java's object serialization serialization . Beans 2. The Bean-Writing process (cont.) 2. The Bean-Writing process (cont.) JavaBeans – Development Phases: JavaBeans – Development Phases: Construction phase Construction phase Involves. Beans 2. The Bean-Writing process (cont.) 2. The Bean-Writing process (cont.) JavaBeans – Development Phases (cont.): JavaBeans – Development Phases (cont.): Construction Phase involves creation