Javabeans+Components

=JAVABEANS = Java is all about OOD and reusability. JavaBeans makes this even easier. In its essence, JavaBeans are Java classes which adhere to certain coding conventions. Some of which I will mention later in the paper.


 * JavaBeans** are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.

Once you have built a bean, users of //any// environment enabled for JavaBeans technology can readily use it. Developers can even, through the magic of the ActiveX bridge technology, deploy JavaBeans in Visual Basic applications and Microsoft Office. Java-Bean in essence is a way for JAVA to compete with Visual Basic which was created by Microsoft. It enables vendors to create Visual Basic-style development environments. These environments make it possible to build user interfaces with not a lot of programming. People can readily program and share or sell beans to other programmers. Because of this it became very popular and has a very strong support and following.

=Definition: =



=Conventions: =



=Key Concepts: =

· Builder tools discover a bean's features (that is, its properties, methods, and events) by a process known as //introspection//. Beans support introspection in two ways: -- By adhering to specific rules, known as //design patterns//, when naming bean features. -- By explicitly providing property, method, and event information with a related //bean information// class · //Properties// are the appearance and behavior characteristics of a bean that can be changed at design time. · Beans expose properties so they can be customized at design time. · Beans use //events// to communicate with other beans. · //Persistence// enables beans to save and restore their state. · A bean's //methods// are no different from Java methods, and can be called from other beans or a scripting environment.


 * This allows the environment to instantiate new beans and to save beans between sessions. However, these are just practical recommendations, not requirements.

Some examples of Beans in use can be found in the following: 

JavaBeans specification allows four types of properties. A bean //property// is a named attribute of a bean that can affect its behavior or appearance.



= CODE: =

The following code is a modified version of a Bean introduced in the Core java book. The intent of the code is to allow the user to get a picture and display it on the screen. It is a very simple and yet power demonstration of how JavaBean can be utilized.Most of the code is auto-generated and the programmer only has to modify what he/she wants through the properties editor. All the programmer has to do is make a frame and then add the Beans that was needed and configure how the events are handled. In this example two beans were used, ImageViewerBean and FileNameBean, both of which were readily available and provided by the book.

FILENAME: ImageViewerFram.java

code format="java" public class ImageViewerFrame extends javax.swing.JFrame {

/** Creates new form ImageViewerFrame */ public ImageViewerFrame { initComponents; }

/** This method is called from within the constructor to    * initialize the form. * WARNING: Do NOT modify this code. The content of this method is    * always regenerated by the Form Editor. */   @SuppressWarnings("unchecked") //  private void initComponents {

imageViewerBean1 = new com.horstmann.corejava.ImageViewerBean; fileNameBean1 = new com.horstmann.corejava.FileNameBean;

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

imageViewerBean1.setForeground(new java.awt.Color(0, 153, 51)); imageViewerBean1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); imageViewerBean1.setText("SimpleBean"); imageViewerBean1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

fileNameBean1.addPropertyChangeListener(new java.beans.PropertyChangeListener {           public void propertyChange(java.beans.PropertyChangeEvent evt) {                loadImage(evt);            }        });

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane); getContentPane.setLayout(layout); layout.setHorizontalGroup(           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)                   .addComponent(fileNameBean1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)                    .addComponent(imageViewerBean1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)) .addContainerGap)       ); layout.setVerticalGroup(           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup .addComponent(imageViewerBean1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(fileNameBean1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(62, Short.MAX_VALUE))       );

pack; }// 

private void loadImage(java.beans.PropertyChangeEvent evt) { // TODO add your handling code here: imageViewerBean1.setFileName(fileNameBean1.getFileName); }

/**   * @param args the command line arguments */   public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable {           public void run {                new ImageViewerFrame.setVisible(true);            }        }); }

// Variables declaration - do not modify private com.horstmann.corejava.FileNameBean fileNameBean1; private com.horstmann.corejava.ImageViewerBean imageViewerBean1; code Below is a picture of the Project View in NetBean. As you can see the ImageViewerBean and FileNameBean are both added to the Libraries directory as JAR files. This enable them to be used in the program.



After they are added, you can see that they are available to be selected from the Palette and dragged to the design screen.



After they are dragged or picked to be placed in the program the user has several options or properties to change to configure the bean to do what he wants. Some of the properties are listed below as examples. For instance, if you wanted to change the name of the default text just simply go to the text property and change "SimpleBean" to whatever text you want. This is the same with Color, border or whatever options that comes with that particular bean.



This is what the program looks like after it is run with the two beans added. It asks the user to pick a picture from a file to display.



Afterwards the user picks the file that he/she wants and gets ready to display it with the imageviewerbean.



Voila, when the user press Open the picture is displayed and everything worked as planned. For the purpose of this project I picked out the picture that was the logo for this wiki.



Here is a GUI for a more advance Bean development project. It is important to note how versatile Bean development because of the components can be pulled from practically anywhere and placed in your code.



The example Code below illustrates how NetBean works with jsp and through a web browser. It is basic restaurant asking the user what beer and dessert they would like. The most important part of the code is the JSP file used to output all of the information (output.jsp). The request parameter is needed to pull the data from the original form. Notice that anything enclosed in the <%= and %> delimiters are JSP expressions. By using the limiters the programmer has access to the following objects:
 * EXAMPLE #2 **


 * application** This object can have attributes attached that are visible across user sessions.
 * config** Typically used to obtain initialization parameters
 * jspContext** Provides access to the exception that was thrown that led to the page being invoked
 * out** Used to output text on page
 * page** Returns a reference to the current JSP
 * pageContext** Provides all functionality provided by jspContext plus additional methods specific to a servlet environment.

= CODE: = =Filename: Index.jsp=

code format="javascript" <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> Toeun's Restaruant Welcome to My Online Restaruant! Which Beer do you want to drink? <form name="example" action="output.jsp"> <input type="submit" value="Order"/>

code

= FILENAME: output.jsp =

code format="javascript" <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> Beer <%= request.getParameter("name") %> , You choose the following!

<%       String[] selectedBeers = request.getParameterValues("beers"); if (selectedBeers != null) { for (int i = 0; i < selectedBeers.length;        i++)   { %>   <li><%= selectedBeers[i]    %>

<%}      }     %>

<%       String[] selectedDessert = request.getParameterValues("dessert"); if (selectedBeers != null) { for (int i = 0; i < selectedDessert.length;        i++)   { %>   <li><%= selectedDessert[i]    %>

<%}      }     %>        Your Order will come out shortly!

code

= OUTPUT =



<span style="font-family: 'Calibri','sans-serif';">



XML **JAVABEANS**  Database


 * Chapters **