|
Most of the rest of this chapter shows you the techniques that you use to write beans. Before we go into details, we give an overview of the process. First, we want to stress that writing a bean is not technically difficultthere are only a few new classes and interfaces for you to master. In particular, the simplest kind of bean is really nothing more than a Java class that follows some fairly strict naming conventions for its methods. NOTE
Example 8-1 at the end of this section shows the code for an ImageViewer bean that could give a Java builder environment the same functionality as the Visual Basic image control we mentioned in the previous section. When you look at this code, notice that the ImageViewerBean class really doesn't look any different from any other class. For example, all accessor methods begin with get, and all mutator methods begin with set. As you will soon see, builder tools use this standard naming convention to discover properties. For example, fileName is a property of this bean because it has get and set methods. Note that a property is not the same as an instance field. In this particular example, the fileName property is computed from the file instance field. Properties are conceptually at a higher level than instance fieldsthey are features of the interface, whereas instance fields belong to the implementation of the class. One point that you need to keep in mind when you read through the examples in this chapter is that real-world beans are much more elaborate and tedious to code than our brief examples, for two reasons.
A good example of a bean with rich behavior is CalendarBean by Kai Tödter (see Figure 8-2). The bean and its source code are freely available from http://www.toedter.com/en/jcalendar. This bean gives users a convenient way of entering dates, simply by locating them in a calendar display. This is obviously pretty complex and not something one would want to program from scratch. By using a bean such as this one, you can take advantage of the work of others, simply by dropping the bean into a builder tool. Figure 8-2. A calendar beanFortunately, you need to master only a small number of concepts to write beans with a rich set of behaviors. The example beans in this chapter, although not trivial, are kept simple enough to illustrate the necessary concepts. Example 8-1. ImageViewerBean.java1. package com.horstmann.corejava; 2. 3. import java.awt.*; 4. import java.io.*; 5. import javax.imageio.*; 6. import javax.swing.*; 7. 8. /** 9. A bean for viewing an image. 10. */ 11. public class ImageViewerBean extends JLabel 12. { 13. 14. public ImageViewerBean() 15. { 16. setBorder(BorderFactory.createEtchedBorder()); 17. } 18. 19. /** 20. Sets the fileName property. 21. @param fileName the image file name 22. */ 23. public void setFileName(String fileName) 24. { 25. try 26. { 27. file = new File(fileName); 28. setIcon(new ImageIcon(ImageIO.read(file))); 29. } 30. catch (IOException e) 31. { 32. file = null; 33. setIcon(null); 34. } 35. } 36. 37. /** 38. Gets the fileName property. 39. @return the image file name 40. */ 41. public String getFileName() 42. { 43. if (file == null) return null; 44. else return file.getPath(); 45. } 46. 47. public Dimension getPreferredSize() 48. { 49. return new Dimension(XPREFSIZE, YPREFSIZE); 50. } 51. 52. private File file = null; 53. private static final int XPREFSIZE = 200; 54. private static final int YPREFSIZE = 200; 55. } |
|