Why Beans?


Programmers coming from a Windows background (specifically, Visual Basic or C#) will immediately know why beans are so important. Programmers coming from an environment in which the tradition is to "roll your own" for everything may not understand at once. In our experience, programmers who do not come from a Visual Basic background often find it hard to believe that Visual Basic is one of the most successful examples of reusable object technology. One reason for the popularity of Visual Basic becomes clear if you consider how you build a Visual Basic application. For those who have never worked with Visual Basic, here, in a nutshell, is how you do it:

  1. You build the interface by dropping components (called controls in Visual Basic) onto a form window.

  2. Through property sheets, you set properties of the components such as height, color, or other behavior.

  3. The property sheets also list the events to which components can react. For some of those events, you write short snippets of event handling code.

For example, in Volume 1, Chapter 2, we wrote a program that displays an image in a frame. It took over a page of code. Here's what you would do in Visual Basic to create a program with pretty much the same functionality:

  1. Add two controls to a window: an Image control for displaying graphics and a Common Dialog control for selecting a file.

  2. Set the Filter properties of the CommonDialog control so that only files that the Image control can handle will show up. This is done in what Visual Basic calls the Properties window, as shown in Figure 8-1.

    Figure 8-1. The Properties window in Visual Basic for an image application


Now, we need to write the four lines of Visual Basic code that will be activated when the project first starts running. The following code pops up the file dialog boxbut only files with the right extension are shown because of how we set the filter property. After the user selects an image file, the code then tells the Image control to display it. All the code you need for this sequence looks like this:

 Private Sub Form_Load()   CommonDialog1.ShowOpen   Image1.Picture = LoadPicture(CommonDialog1.FileName) End Sub 

That's it. The layout activity, combined with these statements, gives essentially the same functionality as a page of Java code. Clearly, it is a lot easier to learn how to drop down components and set properties than it is to write a page of code.

We do not want to imply that Visual Basic is a good solution for every problem. It is clearly optimized for a particular kind of problemUI-intensive Windows programs. The JavaBeans technology was invented to make Java technology competitive in this arena. It enables vendors to create Visual Basic-style environments for developing Java user interfaces with a minimum of programming.



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net