|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java A String Bean: Making JavaBeans With VAJ
A String Bean: Making JavaBeans With VAJ
By: Mike Fichtelman
Jul. 1, 1998 12:00 AM
Visual Age for Javaª provides a good IDE for object oriented development. One of the strong points of Visual Age for Java is its support for JavaBeans. As everyone in the world knows by now JavaBeans are components, or software parts, from which applications can be built. This has been the holy grail of object-oriented development, and Visual Age for Java supports it well. It provides a convenient, connect-the-dots metaphor for assembling an application from parts. One of the obvious questions one might ask, of course, is, "Where do the parts come from?" IBM does supply some JavaBeans with Visual Age for Java but, naturally, they won't do everything. If you need parts, and they're not available, Visual Age for Java makes the process easy. Once you've made the new parts they are reusable in any application. They can even be exported for use outside VAJ. I'll illustrate this with the String Bean. Let's say you've developed a number of string handling functions that could be used in various situations or programs. You could simply include them when they're needed in a new application but JavaBeans provides a better alternative. You can reuse Beans without changing, or even knowing about, the code itself. You can then expose the methods or properties of the Bean and connect your application to them. For example, you might need to parse an input string into its component words. If a method for this was included in a String Bean, you could design the input applet or application frame visually in Visual Age for Java using the supplied design beans, adding an instance of the String Bean to your frame and then connecting the JavaBeans. These are the steps you need to take to create a simple application that will allow input of a text string, and then display the individual words of the string in a list box at the click of a button. First, open Visual Age for Java and create a new applet. When prompted for project and package names, use StringBeanProj and StringBeanPack. The hierarchy should look something like that in Figure1. Next, create a new class and name it StringBean, but choose not to design it visually. Be sure to import the java.awt.* and java.util.* packages when prompted. Then, create a new method for the StringBean class called stringToken with a signature of public static void stringToken (String Instring, List fillList). Add the code block in Listing 1 and save it. Now, if you select the BeanInfo tab you see a list of all available features and properties for the new StringBean bean. It should look like Figure 2. Next, select the StringBeanPack package in the hierarchy and click the C button to build a new class. Call this class StringBeanView and select the choice to design it visually, or go to the Visual Composition editor. After the blank applet panel is displayed, you'll need to add the appropriate beans from those supplied with Visual Age for Java to complete the application/applet. First, add a TextField bean. Then, add a List bean. Finally, add a Button bean and change the label property of the bean to String Bean. You're ready to add the StringBean JavaBean we just created. From the main menu select Options, then Add Bean. In the dialog box displayed, you can either type StringBean or choose to Browse through the available beans and select it off the list. Click OK, then click again anywhere outside the frame boundary and the jigsaw part icon for StringBean1 to appear. Now that all the beans are in place it's time to connect them together. For the first connection click the right mouse button on the String Bean Button, then select Connect and then actionPerformed(java.awt.event.ActionEvent) from the drop down menus. Move the mouse pointer to the StringBean1 icon and click mouse button 1, then select All Features. From the bean Features list, select the stringToken() method created earlier. For the second connection to the TextField bean click mouse button 2 on the first connection line and select Connect. From the list displayed, select inString. Drag the mouse pointer to the TextField bean and click mouse button 1. From the list displayed, select text.For the third connection to the List bean click mouse button 2 on the first connection line and select Connect. From the list displayed, select fillList. Drag the mouse pointer to the List bean and click mouse button 1. From the list displayed, select "this." Now that all the connections between the beans are complete the design panel should look like Figure 3. You can test the StringBean applet by selecting Tools->Test->Run from the main menu of Visual Age for Java. Type some text into the TextField(be sure to leave some blanks between words), click the String Bean button and a list of the words in the input string will be displayed in the list box. I'm sure you can see how this humble foundation could be used to build more extensive applications. Other string handling and manipulation functions can be added to the StringBean bean at any time, making their addition to a new application a simple drag and drop operation. You could even add a StringBean feature that changes string text colors from black to green! Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||