Let's talk about exceptions ...
How do you handle exceptions? Do you think upfront about the type of exceptions that you want to catch or do you just let the outside world handle it?

-- Jeroen van Bergen in JW Blogs

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

A fistful of values

Add indexed properties and complex property editors to your JavaBeans

If you're a JavaBeans developer, you already know that a JavaBeans property is a named access to part of the internal state of a JavaBean. (If you don't know about properties, read all about them in my previous JavaBeans column, "Double Shot, Half Decaf, Skinny Latte -- Customize your Java," listed in this column's Resources.) You also know that a property editor can be used to configure and/or customize JavaBeans at design time. (To get up to speed on property editors and customization, check out "The trick to controlling bean customization" and "Soup up your Java classes with customization," also listed in the Resources section.)

What you may not know is that JavaBeans properties are not limited to scalar (that is, single-valued) values. A JavaBeans property can also be an array of values, accessed by an integer index, called an indexed property.

This month, we're going to tackle creating a shiny new JavaBean with an indexed property, and a custom property editor that allows the developer to manipulate the indexed property at design time. We'll start by creating a basic URLFileLoader bean that downloads a file from an arbitrary URL on the Web, and stores it in a disk file on demand (taking a quick side excursion into boolean properties, a simple property type). Then we'll improve the URLFileLoader class loader, enabling it to fetch files using HTTP GET with parameters maintained by a new indexed property. (We'll go over GET later on in the text.) Next, we'll create a custom editor (so we can access the property graphically), and see how to specify this custom editor with the builder tool (in our case, Sun's BeanBox.) We'll wrap up with a discussion of possible extensions to this project.

Resources
  • A simple introduction to the BeanBox test container is "The BeanBoxSun's JavaBeans test container" http://www.javaworld.com/javaworld/jw-09-1997/jw-09-beanbox.html
  • Event listener interfaces (of which PropertyChangeListener, mentioned above, is one) are explained thoroughly in "Keep listening for upcoming events," available at http://www.javaworld.com/jw-10-1997/jw-10-beans.html
  • For specific usage information on PropertyChangeListener, see "Double Shot, Half Decaf, Skinny Latte -- Customize your Java" at http://www.javaworld.com/jw-09-1997/jw-09-beans.html
  • A good online reference for creating and manipulating jar files is in the Java Tutorial on Sun's Java Web site at http://java.sun.com/docs/books/tutorial/jar/index.html
  • You can find a tutorial introduction to BeanInfo objects and property sheets in the JavaWorld article "The trick to controlling bean customization" http://www.javaworld.com/jw-11-1997/jw-11-beans.html
  • More advanced customization, including how to create and register a customizer class, appears in "Soup up your Java classes with customization" http://www.javaworld.com/jw-12-1997/jw-12-beans.html
  • If you want to try the exercise of creating a JavaBean that performs an HTTP POST and saves the results (based on URLDataLoader, of course,) check out JavaWorld's "Java Tip 34POSTing via Java" http://www.javaworld.com/javatips/jw-javatip34.html
  • Also see "Java Tip 41POSTing via Java revisited" http://www.javaworld.com/javatips/jw-javatip41.html
  • For a quick introduction to firewalls and proxies, see the relevant discussion at the World Wide Web Security FAQ at http://www.w3.org/Security/Faq/wwwsf2.html#Q14
  • Download the source code for this article in Windows zip format from http://www.javaworld.com/jw-11-1998/beans/URLLoader.zip or in Unix tar format at http://www.javaworld.com/jw-11-1998/beans/URLLoader.tar
  • The jar file containing the class files and manifest for this article appear at http://www.javaworld.com/jw-11-1998/beans/URLLoader.jar