Java Beans


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Java Beans

  1. #1
    Join Date
    Dec 2005
    Posts
    8

    Java Beans

    Hai all,
    I have just started to learn Java beans . Actually I am not able to understand its basics. I searched in net and found many tutorials but not able to capture the starting point. I got the following code from net and when I tried to run it it showed an information box "The bean does not have a main method". When I added a main() methode to it it is running but not executing the paint method so nothing is displayed in the output! Can anyone help me?

    import java.awt.*;
    import java.io.Serializable;

    public class SimpleBean extends Canvas
    implements Serializable{

    private Color color = Color.green;

    //property getter method
    public Color getColor(){
    return color;
    }

    //property setter method. Sets new SimpleBean
    //color and repaints.
    public void setColor(Color newColor){
    color = newColor;
    repaint();
    }

    public void paint(Graphics g) {
    g.setColor(color);
    g.fillRect(20, 5, 20, 30);
    }

    //Constructor sets inherited properties
    public SimpleBean(){
    setSize(60,40);
    setBackground(Color.red);
    }
    }

    Also if anyone knows a site for learning Java beans programming in simple steps please let me know.
    Thanks in advance,
    Arya.

  2. #2
    Join Date
    Dec 2005
    Posts
    19
    hello,

    Javabean classes are not meant to have main method inside them. A proper Javabean class should be implementing Serializable, which is meant to break up data chunks into smaller pieces for transmission purposes.

    Also, all the variables are defined private variables. You can only have access to them using setters and getters. Setters are to initialize or assign values to variables. A typical setter should look like this:

    private String name;

    public void setName(String name){
    this.name = name;
    }

    A getter is to return the value of variable to the caller. For example:

    public String getName() {
    return name;
    }

    the main purpose to have Javabeans is for encapsulation purposes / information hiding. You can go serach why encapsulation is important

    And btw to use a Javabean class you need to call it from another class which is inside the same package with the Javabean class. Just create an object of type Javabean then you can call the setters and getters by using the object created.
    Last edited by viviensiu; 12-13-2005 at 03:52 AM.

  3. #3
    Join Date
    Dec 2005
    Posts
    8
    Thanks for your guidance. Actually now only I understood the real meaning of "reusable component". This been I can call as and when needed. Now I got a grip and I will go for it. Thank u again.
    Regards,
    Arya.

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  4. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 04:50 AM
  5. Java Beans
    By Manoj in forum Java
    Replies: 2
    Last Post: 04-08-2000, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center