Web Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  Web Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Web Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Web Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Web Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
Web Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python Web Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
Web Hosting Web Hosting Sign-Up Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  Web Hosting Resellers web hosting, website hosting, web site hosting Web Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Web Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Web Hosting Welcome Web Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Web Hosting Fund Raising, Fundraising, web hosting Web Hosting Resellers, web Hosting Web Hosting Web Design, web Hosting Web Hosting Extra Services,  web Hosting Web Hosting Traffic Booster, web hosting Web Hosting Traffic Booster, web hosting Web Hosting Technical Support,  web Hosting Web Hosting webmaster tips,  web Hosting Web Hosting 30 Day Money Back, web hosting Web Hosting Legal Notices for Web Hosting Web Hosting Glossary Computer Terms for web Hosting Web Hosting Contact Information - web hosting

Site Map
Web Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP Web Hosting Web Hosting Web Hosting Web Hosting Web Hosting Lesson: Writing a Simple Bean (The Java™ Tutorials > JavaBeans(TM))
Trail: JavaBeans(TM)
Lesson: Writing a Simple Bean
In this section you will learn more about beans by performing the following actions:
  • Creating a simple bean
  • Compiling the bean
  • Generating a Java Archive (JAR) file
  • Loading the bean into the GUI Builder of the NetBeans IDE
  • Inspecting the bean's properties and events

Your bean will be named SimpleBean. Here are the steps to create it:

  1. Write the SimpleBean code. Put it in a file named SimpleBean.java, in the directory of your choice. Here's the code:
    import java.awt.Color;
    import java.beans.XMLDecoder;
    import javax.swing.JLabel;
    import java.io.Serializable;
    
    public class SimpleBean extends JLabel 
                            implements Serializable {
        public SimpleBean() {
            setText( "Hello world!" );
            setOpaque( true );
            setBackground( Color.RED );
            setForeground( Color.YELLOW );
            setVerticalAlignment( CENTER );
            setHorizontalAlignment( CENTER );
        } 
    }
    

    SimpleBean extends the javax.swing.JLabel graphic component and inherits its properties, which makes the SimpleBean a visual component. SimpleBean also implements the java.io.Serializable interface. Your bean may implement either the Serializable or the Externalizable interface.

  2. Create a manifest, the JAR file, and the class file SimpleBean.class. Use the Apache Ant tool to create these files. Apache Ant is a Java-based build tool that enables you to generate XML-based configurations files as follows:
     <?xml version="1.0" encoding="ISO-8859-1"?>
    
     <project default="build">
    
      <dirname property="basedir" file="${ant.file}"/>
    
      <property name="beanname" value="SimpleBean"/>
      <property name="jarfile" value="${basedir}/${beanname}.jar"/>
    
      <target name="build" depends="compile">
          <jar destfile="${jarfile}" basedir="${basedir}" includes="*.class">
              <manifest>
                  <section name="${beanname}.class">
                     <attribute name="Java-Bean" value="true"/>
                  </section>
              </manifest>
          </jar>
      </target>
    
      <target name="compile">
          <javac destdir="${basedir}">
              <src location="${basedir}"/>
          </javac>
      </target>
    
      <target name="clean">
          <delete file="${jarfile}">
              <fileset dir="${basedir}" includes="*.class"/>
          </delete>
      </target>
    
    </project> 
    

    It is recommended to save an XML script in the build.xml file, because Ant recognizes this file name automatically.

  3. Load the JAR file. Use the NetBeans IDE GUI Builder to load the jar file as follows:
    1. Start NetBeans.
    2. From the File menu select "New Project" to create a new application for your bean. You can use "Open Project" to add your bean to an existing application.
    3. Create a new application using the New Project Wizard.
    4. Select a newly created project in the List of Projects, expand the Source Packages node, and select the Default Package element.
    5. Click the right mouse button and select New|JFrameForm from the pop-up menu.
    6. Select the newly created Form node in the Project Tree. A blank form opens in the GUI Builder view of an Editor tab.
    7. Open the Palette Manager for Swing/AWT components by selecting Palette Manager in the Tools menu.
    8. In the Palette Manager window select the beans components in the Palette tree and press the "Add from JAR" button.
    9. Specify a location for your SimpleBean JAR file and follow the Add from JAR Wizard instructions.
    10. Select the Palette and Properties options from the Windows menu.
    11. Expand the beans group in the Palette window. The SimpleBean object appears. Drag the SimpleBean object to the GUI Builder panel.

    The following figure represents the SimpleBean object loaded in the GUI Builder panel:

    This figure represents the SimpleBean object loaded in the GUI Builder panel,

  4. Inspect Properties and Events. The SimpleBean properties will appear in the Properties window. For example, you can change a background property by selecting another color. To preview your form, use the Preview Design button of the GUI Builder toolbar. To inspect events associated with the SimpleBean object, switch to the Events tab of the Properties window. You will learn more about bean properties and events in the lessons that follow.

Previous page: Previous Lesson
Next page: Properties
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting