Java Reference
In-Depth Information
public class MovieBeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor [] getPropertyDescriptors()
{
try
{
PropertyDescriptor [] pds = {
new PropertyDescriptor(“title”, video.store.Movie.class),
new PropertyDescriptor(“length”, video.store.Movie.class),
new PropertyDescriptor(“customerName”,
video.store.Movie.class,
null, “setCustomerName”),
new PropertyDescriptor(“rented”,
video.store.Movie.class, “isRented”, null)};
return pds;
}catch(IntrospectionException e)
{
e.printStackTrace();
return null;
}
}
public int getDefaultPropertyIndex()
{
return 0;
}
}
The MovieBeanInfo class needs to appear in the JAR file with the Movie
bean, and builder tools automatically look for and invoke the methods on
MovieBeanInfo to determine this additional information about the bean.
Lab 19.1: A Stock Bean
To become familiar with writing a JavaBean. In this lab, you will write a
bean to represent a stock traded on the stock exchange.
1. Write a bean class named Stock that implements Serializable and
contains a no-argument constructor. Have the class extend JPanel so
that it will be a visual bean.
2. Add the necessary set and get methods so that the Stock bean has
a property named symbol of type String, price of type int, and
sharesTraded of type double.
3. Override the paint() method inherited from JPanel, and use the
drawString() method of the Graphics class to display the symbol,
price, and sharesTraded properties within the JPanel.
Search WWH ::




Custom Search