1. Trang chủ
  2. » Giáo án - Bài giảng

Java Beans

48 1,1K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 48
Dung lượng 380 KB

Nội dung

JAVA BEANS 2 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM Overview  The Bean-Writing Process  Using Beans to Build an Application  Naming Patterns for Bean Properties and Events  Bean Property Types  Beaninfo Classes  Property Editors  Customizers  Javabeans Persistence continued 3 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM What is a JavaBean?  A JavaBean is a component:  a self-contained, reusable software unit  it can be visually composed with other beans to make bigger compo nents, applets, applications, etc. continued 4 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM  A bean is designed to expose its features:  e.g. public methods, properties (data), events  Exposure allows beans to be used inside visual building tools (e.g. JBuilder)  Exposure is based on coding rules that a programmer must follow when writing a bean  these rules are mostly standard naming conventions for methods continued 5 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM  Beans are persistent  their data is automatically stored when the program using them finishes  the data is restored when the program next starts  persistence is implemented in JavaBeans by using Java's object seriali zation features 6 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM How Builder Tools Use Beans  Builder tools (e.g JBuilder, Bean Builder) know a bean's features by looking at its publi c methods, properties and events  called introspection  implemented using Java's reflection classes  A builder can also examine a bean's information object (BeanInfo), if the progra mmer has included one. continued 7 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM  A builder will have visual support for setting/ reading a bean's properties, and for accessing its methods.  A builder will be able to 'link' beans based on events  a source bean can send an event to a listener bean 8 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM From Package to JAR  A JAR file must contain a manifest  it lists the contents of the JAR file  a programmer writes the manifest in the manifest.tmp file  When the JAR file is being created, the jar utility converts manifest.tmp into a MANIF EST.MF file stored in the META_INF director y of the JAR file. 9 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM 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 '.' continued 10 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM  There may be many classes (and other resources) in a JAR file  each has its own Name and Java-Bean line  Not all the resources in a JAR file need to be beans; if a class is a be an then the Java-Bean line is set to t rue (otherwise false)  beans will appear in the Toolbox window [...]... listed Create by Võ Văn Hải-Khoa CNTT- 12 Run the JAR file  c:> java -jar LogoAnimator.jar    run the application in the bean java uses the manifest file to execute main() of Main-Class java the name of the class to execute: c:> java -cp myJar.jar myPackage.myClass Or give Create by Võ Văn Hải-Khoa CNTT- 13 Kinds of Properties  JavaBeans have 4 kinds of properties, which are supported in different... Example SliderFieldPanel Bean JSlider and a JTextField  A  A change to either should affect the other, and any listener objects should be notified Create by Võ Văn Hải-Khoa CNTT- 25 Listening Beans Format  Beans that are registered listeners for property changes must implement the PropertyChangeListener interface  The interface has one method:  void propertyChange(PropertyChangeEvent e)  it is... i) { return values[i]; } public void setValues(int i, double v) { values[i] = v; } Create by Võ Văn Hải-Khoa CNTT- 22 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 Create by Võ Văn Hải-Khoa CNTT- continued... properties, which are supported in different ways:     simple properties indexed properties bound properties constrained properties Create by Võ Văn Hải-Khoa CNTT- 14 Naming Patterns  The designers of the Java specification decided not to add keywords to the language to support vis ual programming  Builder tool could analyze a bean to learn its properties or events  There are two alternative mechanisms... changeSupport.firePropertyChange( ) vetoSupport is used in a similar way to changeSupport:  vetoable listeners must be registered with it via public add/remove methods Create by Võ Văn Hải-Khoa CNTT- 32 Vetoable Listener Code  Beans that are registered vetoable listeners for property changes must implement the Vetoa bleChangeListener interface  The interface has one method:  void vetoableChange (PropertyChangeEvent e)  it . restored when the program next starts  persistence is implemented in JavaBeans by using Java's object seriali zation features 6 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM How Builder Tools Use. Properties and Events  Bean Property Types  Beaninfo Classes  Property Editors  Customizers  Javabeans Persistence continued 3 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM What is a JavaBean?  A. myJar.jar myPackage.myClass 14 Create by Võ Văn Hải-Khoa CNTT- ĐHCN Tp.HCM Kinds of Properties  JavaBeans have 4 kinds of properties, which are supported in different ways:  simple properties  indexed

Ngày đăng: 13/05/2014, 11:00

Xem thêm